Repository navigation
Replies: 1 comment
|
I would like for this to be supported. Alternatively that you can provide your own draftProvider. To make a code example of how easier it would be code side when a boolean ain't enough - using sanity example here which stores which draft version to display as a cookie: // Current state
import {draftMode} from 'next/headers'
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 async function Page() {
const {isEnabled: isDraftMode} = await draftMode()
if (isDraftMode) {
return (
<Suspense fallback={<section>Loading…</section>}>
<DynamicProductsList />
</Suspense>
)
}
return <CachedProductsList perspective="published" stega={false} />
}
async function DynamicProductsList() {
const {perspective, stega} = await getDynamicFetchOptions()
return <CachedProductsList perspective={perspective} stega={stega} />
}
async function CachedProductsList({perspective, stega}: DynamicFetchOptions) {
'use cache'
const {data: products} = await sanityFetch({
query: PRODUCTS_QUERY,
params: {limit: 10},
perspective,
stega,
})
return (
<section>
{products.map((product) => (
<article key={product._id}>
<a href={`/product/${product.slug}`}>{product.title}</a>
</article>
))}
</section>
)
}After non-boolean values can be returned from draftMode: import {draftMode} from 'next/headers'
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 async function Page() {
const {isEnabled: isDraftMode, value: perspective} = await draftMode()
return <CachedProductsList perspective={perspective ?? "published"} stega={!!perspective} />
}
async function CachedProductsList({perspective, stega}: DynamicFetchOptions) {
'use cache'
const {data: products} = await sanityFetch({
query: PRODUCTS_QUERY,
params: {limit: 10},
perspective,
stega,
})
return (
<section>
{products.map((product) => (
<article key={product._id}>
<a href={`/product/${product.slug}`}>{product.title}</a>
</article>
))}
</section>
)
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Goals
Non-Goals
Background
In the Pages Router, developers could use previewData to pass context like user roles or request-specific details for dynamic previews. This was essential for CMS integrations and personalized content previews.
With the App Router, draftMode replaced previewData but dropped support for passing custom context. While simpler, this limits flexibility and forces developers to use workarounds for workflows that were previously straightforward.
Reintroducing context support in draftMode would restore this flexibility, enabling dynamic previews, better CMS integration, and an improved developer experience—bridging the gap between the old and new paradigms.
Proposal
My proposal is to add a single optional parameter to the
enablefunction of the draftMode object that is a string which is stored alongside the__prerender_bypasscookie in a cookie called something like__prerender_bypass_preview. This would be destroyed when preview mode was disabled. While it's possible with dynamic page rendering, to emulate this functionality in the client without updating draftMode, it becomes much more difficult when using draftMode with statically rendered pages. This approach allows us to get the same functionality regardless of the rendering method and still keeps the API very simple.For example:
route.tspage.tsxAll reactions