Support dynamic functions in Draft Mode in otherwise statically rendered pages #50399
Replies: 5 comments 2 replies
-
This feature is hugely needed by Payload as well. Accessing drafts requires authentication. This is done by setting an |
Beta Was this translation helpful? Give feedback.
-
Is draft mode supposed to work as you describe when in dynamic mode? Because I can't get this to work, see #51302 |
Beta Was this translation helpful? Give feedback.
-
^^ Update: as of I was previously receiving this error: const { isEnabled: isDraftMode } = draftMode()
const page = !isDraftMode ? await fetchPage(slug) : await fetchDraftPage(slug) // `fetchDraftPage` calls `cookies()` This also means that the Parallel Routes workaround is no longer needed. This is much simpler now 🙏 |
Beta Was this translation helpful? Give feedback.
-
I support this proposal. Our application relies on 'setPreviewData()' in Preview API Route. We currently use Preview to display unpublished content by id in selected Contentful environment. 'setPreviewData()' is used to pass entry and environments ids as 'context.previewData' to the page component. |
Beta Was this translation helpful? Give feedback.
-
As a walkaround I've managed to get token from external draft class prototype, which is not good, buuut it helps keeping pages SSG and not opt out to SSR as it was before when you attempt to read token from the cookies at the page level (even not in a draft mode).
v14.0.4 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
cookies()
or adding the ability to set and read serializable data while Draft Mode is enabled in normally statically rendered pages.Non-Goals
headers()
or other router hooks (e.g.useSearchParams()
) is not covered in this request, but could be enabled as a side-effect.next export
is not expected to change as a result of this request.Background
Preview Mode in the Pages Router allows a developer to pass data from an API Route via
res.setPreviewData()
to a page'sgetStaticProps()
. This data container allows for passing arbitrary data to a page's data fetching function without the need to directly access headers, cookies, or any network request-level data.Draft Mode works similarly to Preview Mode, but removes the capability of passing arbitrary data through its system. Instead, it assumes app code can be used to accomplish the same need. This assumption does not support an app that needs to access data in
headers()
orcookies()
since it forces dynamic rendering; Draft Mode is designed for statically rendered pages, which don't allow dynamic functions.As a specific example of this need, Prismic, a headless CMS, supports unlimited content branch previews using a cookie-based system. A Prismic-specific cookie contains a token referring to a specific content branch, which is sent along with Prismic API requests to query that branch of content. Prismic needs to read that cookie in order to query preview content, which forcibly opts the app into dynamic rendering at all times.
Proposal
The following two ideas could work:
1. Support all dynamic functions while Draft Mode is enabled.
Next.js could switch to full dynamic rendering when Draft Mode is enabled. Doing so would allow all dynamic functions to be used, including
cookies()
,headers()
, anduseSearchParams()
, without forcing dynamic rendering while Draft Mode is disabled.All other existing behavior protecting against dynamic rendering would be retained while Draft Mode is disabled, including
export const dynamic = "error"
.2. Add the ability to set and read data within Draft Mode’s system when enabled.
Replicating Preview Mode's data-storing behavior would allow for sending cookie, header, or arbitrary request-level data from a Route Handler to a page.
Disabling Draft Mode would clear the stored data.
Draft Mode data could be returned from the
draftMode()
function.The data could be stored alongside the current store where Draft Mode’s enabled/disabled status is determined.
Beta Was this translation helpful? Give feedback.
All reactions