Skip to content

fix(nextjs): let the app shell (Header/Footer) bind draft previews - #445

Merged
guitavano merged 1 commit into
mainfrom
guitavano/fix-next-shell-draft
Aug 6, 2026
Merged

guitavano merged 1 commit into
mainfrom
guitavano/fix-next-shell-draft

Conversation

@guitavano

@guitavano guitavano commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

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) via resolveValue/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 React cache() 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 calls loadBlocks() but never touches the resolvedLayoutCache #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_draft cookie has only just been set on the response. So neither of ensureDraft's existing sources works from a layout on entry. This forwards the pointer as a request header the layout can read:

  • draftMiddleware forwards the active pointer on x-deco-draft via NextResponse.next({ request: { headers } }). New draftRequestHeaders(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 from param → forwarded header → cookie, so it binds correctly when called from a layout — on both entry and navigation. Host gate unchanged.

prepareDraft still 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 and cache()-scoped, so calling it in both the layout and the page is free:

// app/layout.tsx
export default async function RootLayout({ children }) {
  await ensureSetup();
  if (isDraftPreviewEnabled()) await ensureDraft(); // binds from x-deco-draft
  const footer = await resolveValue({ __resolveType: "Footer" });
  return <Shell footer={<Footer {...footer} />}>{children}</Shell>;
}

Tests

  • draftMiddleware.test.ts: draftRequestHeaders forwards 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 draftSlot formatting nit on main is untouched).

Honest validation caveats

  • Verified via unit tests + code-tracing, not a full browser e2e: the nextjs-smoke example is pre-existingly broken in this repo state (unrelated — packages/blocks/src/hooks/useLoadMore.ts imports React hooks without "use client", so any server route pulling @decocms/blocks/hooks 500s). Couldn't stand up a running Next harness without first fixing that orthogonal bug.
  • Targets the pull-based draft (?__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 on 7.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-draft and let layouts bind it via ensureDraft.

  • Bug Fixes

    • draftMiddleware now forwards the pointer on x-deco-draft using draftRequestHeaders, and strips any forged header when not previewing.
    • ensureDraft reads the pointer with precedence: param → x-deco-draft → cookie; works from layouts with no searchParams. Host gate remains enforced.
    • Exported DRAFT_HEADER and added tests for header forwarding, layout binding, and gating.
  • Migration

    • In app/layout.tsx, call ensureDraft() before resolving shell sections (Header/Footer).
    • If you have custom middleware, use prepareDraft → draftRequestHeaders with NextResponse.next({ request: { headers } }) → applyDraft.

Written for commit 663ae72. Summary will update on new commits.

Review in cubic

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]>
@guitavano
guitavano requested a review from a team August 6, 2026 13:28
@guitavano
guitavano merged commit 8c39922 into main Aug 6, 2026
1 check passed
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 7.34.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant