Repository navigation
fix(nextjs): let the app shell (Header/Footer) bind draft previews - #445
Merged
Merged
Conversation
App Router renders the root layout concurrently with the page, and the draft
override is bound in the page scope (`createDecoPage` → `ensureDraft`). So a
Header/Footer resolved in the app shell (`app/layout.tsx` via
`resolveValue`/`loadBlocks`) resolves against PUBLISHED content — a Studio edit
to the Footer never reflects, even though page sections do. The shell renders
before/outside the page's draft binding.
Root cause is scope, not caching (the resolved-layout cache is not on this
path — `resolveValue` doesn't touch it). The shell needs to bind the draft
itself, but a layout gets no `searchParams`, and on the entry request the
`__deco_draft` cookie has only just been set on the response.
Fix — forward the pointer as a request header so the whole RSC tree can bind:
- `draftMiddleware` now forwards the active pointer on `x-deco-draft` via
`NextResponse.next({ request: { headers } })` (new `draftRequestHeaders`
helper for sites with their own middleware); it strips a client-forged
header when the request is not previewing.
- `ensureDraft()` reads the pointer from param → forwarded header → cookie, so
it now binds correctly when called from a layout (no searchParams) on both
entry and navigation. Host gate unchanged.
Site pattern (documented): the app shell awaits `ensureDraft()` before
resolving global Header/Footer sections — idempotent + `cache()`-scoped, so
calling it in both layout and page is free.
`prepareDraft` still decides purely from param + cookie; the header is an
OUTPUT of the gated decision, never an input to it. Not a new trust surface —
the pointer was never a secret (host-scoping bounds blast radius).
Tests: forwarding (set on preview, stripped otherwise) + `ensureDraft` binding
from the forwarded header with no searchParams (the layout case) + host gating.
80 nextjs tests pass.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
🎉 This PR is included in version 7.34.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes Header/Footer not reflecting draft previews on Next.js sites — reported on
faststore-fila("mudei o footer e não refletiu"). Replaces #444 (closed: that fixed the resolved-layout cache, which is not on this path).Root cause — scope, not caching
Sites resolve Header/Footer in the app shell (
app/layout.tsx) viaresolveValue/loadBlocks, not as page sections. In App Router the root layout renders concurrently with the page, and the draft override is bound in the page scope (createDecoPage→ensureDraft→ a Reactcache()slot). So when the shell resolves the Footer, the override isn't bound yet → it renders published content, even though page sections reflect the draft.Verified by code-tracing on
faststore-fila:resolveFooterConfig()→resolveValue({__resolveType:'Footer'}), which callsloadBlocks()but never touches theresolvedLayoutCache#444 targeted. It's the layout-vs-page binding scope, exactly the caveat #409 documented — now biting the shell.Fix — forward the pointer so the whole RSC tree can bind
A layout gets no
searchParams, and on the entry request the__deco_draftcookie has only just been set on the response. So neither ofensureDraft's existing sources works from a layout on entry. This forwards the pointer as a request header the layout can read:draftMiddlewareforwards the active pointer onx-deco-draftviaNextResponse.next({ request: { headers } }). NewdraftRequestHeaders(request, decision)helper for sites with their own middleware. A client-forged header is stripped when the request isn't previewing.ensureDraft()now reads the pointer fromparam → forwarded header → cookie, so it binds correctly when called from a layout — on both entry and navigation. Host gate unchanged.prepareDraftstill decides purely from param + cookie: the header is an output of the gated decision, never an input. Not a new trust surface — the pointer was never a secret (host-scoping bounds blast radius; a forged header ≡ typing?__draft=).Site-side pattern (required)
The app shell must
await ensureDraft()before resolving global Header/Footer sections. It's idempotent andcache()-scoped, so calling it in both the layout and the page is free:Tests
draftMiddleware.test.ts:draftRequestHeadersforwards the pointer (param + cookie cases) and strips a client-forged header when not previewing.draftShell.test.ts(new):ensureDraft()binds from the forwarded header with no searchParams (the layout case), still binds from the cookie, no-ops with no source, and stays host-gated.80 nextjs tests pass; typecheck clean; changed files pass biome (the pre-existing
draftSlotformatting nit onmainis untouched).Honest validation caveats
nextjs-smokeexample is pre-existingly broken in this repo state (unrelated —packages/blocks/src/hooks/useLoadMore.tsimports React hooks without"use client", so any server route pulling@decocms/blocks/hooks500s). Couldn't stand up a running Next harness without first fixing that orthogonal bug.?__draft=) flow. If a site previews Header/Footer through the legacy admin POST-render flow (withBlocksOverride, pre-feat: pull-based draft preview #409), that's a different mechanism this doesn't cover — the shell would need to render inside that override scope.faststore-fila's local checkout is on7.20.7(pre-feat: pull-based draft preview #409); worth confirming which preview its repro used.🤖 Generated with Claude Code
Summary by cubic
Fixes Next.js app shell so Header/Footer reflect draft previews. We forward the active draft pointer as
x-deco-draftand let layouts bind it viaensureDraft.Bug Fixes
draftMiddlewarenow forwards the pointer onx-deco-draftusingdraftRequestHeaders, and strips any forged header when not previewing.ensureDraftreads the pointer with precedence: param →x-deco-draft→ cookie; works from layouts with nosearchParams. Host gate remains enforced.DRAFT_HEADERand added tests for header forwarding, layout binding, and gating.Migration
app/layout.tsx, callensureDraft()before resolving shell sections (Header/Footer).prepareDraft→draftRequestHeaderswithNextResponse.next({ request: { headers } })→applyDraft.Written for commit 663ae72. Summary will update on new commits.