Is this page static or dynamic with cache components?

HEAD
09851e2Revert "Ensure _middleware is consistent in adapter for name/id" (#89624)
This post might have stale content, as HEAD is 4331 commits ahead.

With cache components enabled, would this page be static or dynamic?

// src/app/page.tsx

import {defineQuery} from 'next-sanity' import {getDynamicFetchOptions, sanityFetch, type DynamicFetchOptions} from '@/sanity/lib/live' import {Suspense} from 'react'

const PRODUCTS_QUERY = defineQuery( *[_type == "product" && defined(slug.current)][0...$limit]{_id,slug,title}, )

export default function Page() { return ( <Suspense fallback={

Loading…
}> ) }

async function DynamicProductsList() { const {perspective, stega} = await getDynamicFetchOptions() return }

async function CachedProductsList({perspective, stega}: DynamicFetchOptions) { 'use cache'

const {data: products} = await sanityFetch({ query: PRODUCTS_QUERY, params: {limit: 10}, perspective, stega, })

return (

{products.map((product) => (
<a href={/product/${product.slug}}>{product.title}
))}
) }

Avatar of sgalanb
Santiago Galán
commented

Here is getDynamicFetchOptions:

// src/sanity/lib/live.ts

import {createClient} from 'next-sanity' import {defineLive, resolvePerspectiveFromCookies, type LivePerspective} from 'next-sanity/live' import {cookies, draftMode} from 'next/headers'

const client = createClient({ projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID, dataset: process.env.NEXT_PUBLIC_SANITY_DATASET, useCdn: true, apiVersion: 'v2025-03-04', stega: {studioUrl: '/studio'}, })

const token = process.env.SANITY_API_READ_TOKEN if (!token) { throw new Error('Missing SANITY_API_READ_TOKEN') }

export const {sanityFetch, SanityLive} = defineLive({ client, serverToken: token, browserToken: token, })

// Resolves perspective and stega outside 'use cache' boundaries export interface DynamicFetchOptions { perspective: LivePerspective stega: boolean } export async function getDynamicFetchOptions(): Promise { const {isEnabled: isDraftMode} = await draftMode() if (!isDraftMode) { return {perspective: 'published', stega: false} }

const jar = await cookies() const perspective = await resolvePerspectiveFromCookies({cookies: jar}) return {perspective: perspective ?? 'drafts', stega: true} }

Avatar of sgalanb
Santiago Galán
commented

So pages can be fully static even when they inlclude a Suspense? If the page is fully static and deployed, how would it detect when draft mode is enabled? That detection is client or server side? If it is server side, it would mean that even fully static pages trigger a function every time they are visited?

Avatar of sgalanb
Santiago Galán
commented

Wow, didn’t know about that CDN routing option. And since it isn’t a normal serverless function, is it billed by platforms like Vercel, or is the compute usage so small they don’t even care?

Avatar of sgalanb
Santiago Galán
commented

Great. In this thread (https://github.com/brunorzn/nextjs-draftmode-devmode-example/pull/1) the proposed solution by a member of the Next.js team is to wrap things in a suspense when draft mode is enabled so we can access cookies. But since adding the suspense doesn't make the page dynamic, can't we just always have the suspense?

Avatar of sgalanb
Santiago Galán
commented

So the Sanity solution doesn't make sense because it demands a lot of thinking by the end user and fetches the cookies and draft mode outside the cached component and then pass the values to it. Where a solution like wrapping the body in a suspense when draft is enabled only needs to be setup once and then it works for all routes. The key is that if the cache component is wrapped in a suspense, we can access cookies directly inside the CMS fetch wrapper (always validating that the draft mode is active first)

Avatar of sgalanb
Santiago Galán
commented

Right. Create a plan/prompt so other agent can propose a fix so Next.js allows accessing cookies inside a cache component when draft mode is enabled. It should also analize any implications, side-effects or things that may broke by doing this (because it's weird the Next.js team didn't fix it yet if it is so simple).


END OF POST

Is this page static or dynamic with cache components? — vercel/next.js