From 3f1a80b135a75bb99f9b07cdcaac02401e3e3768 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Thu, 21 Nov 2024 17:12:21 +0100 Subject: [PATCH] track time of fetch --- next-canary/next.config.ts | 5 +++++ next-enterprise/next.config.ts | 8 +++++++- next-enterprise/src/app/layout.tsx | 10 ++++------ next-enterprise/src/app/page.tsx | 18 +++++++++++------- next-enterprise/src/sanity/fetch.ts | 4 ++-- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/next-canary/next.config.ts b/next-canary/next.config.ts index 39dfc8b..4c17d31 100644 --- a/next-canary/next.config.ts +++ b/next-canary/next.config.ts @@ -1,6 +1,11 @@ import type {NextConfig} from 'next' const nextConfig: NextConfig = { + logging: { + fetches: { + fullUrl: true, + }, + }, experimental: { dynamicIO: true, cacheLife: { diff --git a/next-enterprise/next.config.ts b/next-enterprise/next.config.ts index 8187bec..3107703 100644 --- a/next-enterprise/next.config.ts +++ b/next-enterprise/next.config.ts @@ -1,5 +1,11 @@ import type {NextConfig} from 'next' -const nextConfig: NextConfig = {} +const nextConfig: NextConfig = { + logging: { + fetches: { + fullUrl: true, + }, + }, +} export default nextConfig diff --git a/next-enterprise/src/app/layout.tsx b/next-enterprise/src/app/layout.tsx index b018100..c85fb7c 100644 --- a/next-enterprise/src/app/layout.tsx +++ b/next-enterprise/src/app/layout.tsx @@ -6,18 +6,16 @@ import {SanityLive} from './SanityLive' import {ThemeButton} from './ThemeButton' import {TimeSince} from './TimeSince' -const THEME_QUERY = defineQuery(`*[_id == "theme"][0]{background,text,"fetchedAt": now()}`) +const THEME_QUERY = defineQuery( + `*[_id == "theme"][0]{background,text,"fetchedAt": dateTime(now())}`, +) export default async function RootLayout({ children, }: Readonly<{ children: React.ReactNode }>) { - const { - data, - // This timestamp is inaccurate without 'use cache' - // fetchedAt, - } = await sanityFetch({query: THEME_QUERY}) + const {data} = await sanityFetch({query: THEME_QUERY}) return ( { const {data} = await sanityFetch({query: DEMO_QUERY, params: {slug}}) return { - title: data || 'Next Enterprise', + title: data?.title || 'Next Enterprise', } } export default async function Home() { - const {data, fetchedAt} = await sanityFetch({query: DEMO_QUERY, params: {slug}}) + const {data} = await sanityFetch({query: DEMO_QUERY, params: {slug}}) return (

- {data || 'Next Enterprise'} + {data?.title || 'Next Enterprise'}

- - - + {data?.fetchedAt && ( + + + + )}
) } diff --git a/next-enterprise/src/sanity/fetch.ts b/next-enterprise/src/sanity/fetch.ts index abacd69..8651009 100644 --- a/next-enterprise/src/sanity/fetch.ts +++ b/next-enterprise/src/sanity/fetch.ts @@ -13,6 +13,6 @@ export async function sanityFetch({ filterResponse: false, tag: 'fetch-sync-tags', // The request tag makes the fetch unique, avoids deduping with the cached query that has tags }) - const data = await client.fetch(query, params, {next: {tags}}) - return {data, tags, fetchedAt: new Date().toJSON()} + const data = await client.fetch(query, params, {next: {revalidate: false, tags}}) + return {data, tags} }