Skip to content

Commit

Permalink
test fetch query
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 21, 2024
1 parent 19cb087 commit 996b946
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions next-enterprise/sanity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ export type AllSanitySchemaTypes =
export declare const internalGroqTypeReferenceTo: unique symbol
// Source: ./src/app/layout.tsx
// Variable: THEME_QUERY
// Query: *[_id == "theme"][0]{background,text}
// Query: *[_id == "theme"][0]{background,text,"fetchedAt": now()}
export type THEME_QUERYResult =
| {
background: null
text: null
fetchedAt: string
}
| {
background: string | null
text: string | null
fetchedAt: string
}
| null

Expand All @@ -190,7 +192,7 @@ export type DEMO_QUERYResult = string | null

declare module '@sanity/client' {
interface SanityQueries {
'*[_id == "theme"][0]{background,text}': THEME_QUERYResult
'*[_id == "theme"][0]{background,text,"fetchedAt": now()}': THEME_QUERYResult
'*[_type == "demo" && slug.current == $slug][0].title': DEMO_QUERYResult
}
}
1 change: 0 additions & 1 deletion next-enterprise/src/app/ThemeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client'

import type {SyncTag} from '@sanity/client'
import {useTransition} from 'react'
import {randomColorTheme} from './actions'

Expand Down
16 changes: 11 additions & 5 deletions next-enterprise/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import {SanityLive} from './SanityLive'
import {ThemeButton} from './ThemeButton'
import {TimeSince} from './TimeSince'

const THEME_QUERY = defineQuery(`*[_id == "theme"][0]{background,text}`)
const THEME_QUERY = defineQuery(`*[_id == "theme"][0]{background,text,"fetchedAt": now()}`)

export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
const {data, fetchedAt} = await sanityFetch({query: THEME_QUERY})
const {
data,
// This timestamp is inaccurate without 'use cache'
// fetchedAt,
} = await sanityFetch({query: THEME_QUERY})

return (
<html
Expand All @@ -26,9 +30,11 @@ export default async function RootLayout({
>
<body>
<div className="relative flex min-h-dvh flex-col items-center justify-evenly overflow-auto">
<Suspense>
<TimeSince label="layout.tsx" since={fetchedAt} />
</Suspense>
{data?.fetchedAt && (
<Suspense>
<TimeSince label="layout.tsx" since={data.fetchedAt} />
</Suspense>
)}
{children}
<Suspense>
<ThemeButton />
Expand Down

0 comments on commit 996b946

Please sign in to comment.