-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script> | ||
import { navigate } from 'astro:transitions/client'; | ||
import {CorsOriginError} from '@sanity/client' | ||
import { client } from '../sanity/client'; | ||
|
||
const url = new URL(window.location.href) | ||
|
||
const subscription = client.live.events().subscribe({ | ||
next: (event) => { | ||
if (event.type === 'welcome') { | ||
console.info('Sanity is live with automatic revalidation of published content') | ||
} else if (event.type === 'message') { | ||
const url = new URL(window.location.href) | ||
url.searchParams.set('lastLiveEventId', event.id) | ||
navigate(url.toString(), {history: 'replace'}) | ||
} else if (event.type === 'restart') { | ||
const url = new URL(window.location.href) | ||
url.searchParams.delete('lastLiveEventId') | ||
navigate(url.toString(), {history: 'replace'}) | ||
} | ||
}, | ||
error: (error: unknown) => { | ||
if (error instanceof CorsOriginError) { | ||
console.warn( | ||
`Sanity Live is unable to connect to the Sanity API as the current origin - ${window.origin} - is not in the list of allowed CORS origins for this Sanity Project.`, | ||
error.addOriginUrl && `Add it here:`, | ||
error.addOriginUrl?.toString(), | ||
) | ||
} else { | ||
console.error(error) | ||
} | ||
}, | ||
}) | ||
window.addEventListener('beforeunload', () => subscription.unsubscribe()) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
--- | ||
import {defineQuery} from 'groq' | ||
import {sanityFetch} from '../sanity/fetch' | ||
import Welcome from '../components/Welcome.astro' | ||
import Layout from '../layouts/Layout.astro' | ||
export const prerender = false | ||
// Welcome to Astro! Wondering what to do next? Check out the Astro documentation at https://docs.astro.build | ||
// Don't want to use any of this? Delete everything in this file, the `assets`, `components`, and `layouts` directories, and start fresh. | ||
const url = new URL(Astro.request.url) | ||
const DEMO_QUERY = defineQuery(`*[_type == "demo" && slug.current == $slug][0].title`) | ||
const slug = 'astro' | ||
const lastLiveEventId = url.searchParams.get('lastLiveEventId') | ||
const {data} = await sanityFetch({query: DEMO_QUERY, params: {slug}, lastLiveEventId}) | ||
--- | ||
|
||
<Layout> | ||
<h1>{data || 'Astro'}</h1> | ||
<Welcome /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {createClient} from '@sanity/client' | ||
|
||
export const client = createClient({ | ||
projectId: 'hiomol4a', | ||
dataset: 'lcapi', | ||
apiVersion: '2024-09-18', | ||
useCdn: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {type QueryParams} from '@sanity/client' | ||
import {client} from './client' | ||
|
||
export async function sanityFetch<const QueryString extends string>({ | ||
query, | ||
params = {}, | ||
lastLiveEventId, | ||
}: { | ||
query: QueryString | ||
params?: QueryParams | ||
lastLiveEventId: string | null | ||
}) { | ||
const {result, syncTags} = await client.fetch(query, params, { | ||
lastLiveEventId, | ||
filterResponse: false, | ||
}) | ||
|
||
return {data: result, tags: syncTags} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.