Skip to content

fix(blocks): match draft override blocks by percent-decoded key - #450

Merged
guitavano merged 1 commit into
mainfrom
guitavano/casaevideo-draft-preview
Aug 7, 2026
Merged

guitavano merged 1 commit into
mainfrom
guitavano/casaevideo-draft-preview

Conversation

@guitavano

@guitavano guitavano commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The pull-based ?__draft= preview (TanStack #442, Next #409) merges the Studio sandbox decofile on top of the site's published blocks in loadBlocks(). But the two decofiles disagree on how block keys spell special characters:

/ (home) block key
Published (/.decofile, the base) pages-Home%20(principal)-287364 (percent-encoded)
Studio draft sandbox (_sandbox/decofile, the override) pages-Home (principal)-287364 (raw)

The merge was a literal key-merge:

const merged = { ...blockData };   // base key: "...%20..."
merged[key] = value;               // override key: "... ..." → ADDS, never REPLACES

So the raw-keyed draft block was added alongside the encoded published block, leaving two pages- blocks with the same .path. findPageByPath returns the first match in getAllPages() order — the published block was already in blockData, the draft was appended after, so ties resolve to published. The draft edit silently never rendered: preview badge showed, no-store/noindex/cookie/edge-cache-bypass were all correct, but the page content stayed published.

Impact

Measured against casaevideo's live published vs sandbox decofiles: 78 of 107 pages (~73%) were affected — every page whose block name contains a space or accent (Home (principal), Afiliados - Performance, …). The 29 pages with plain ASCII keys (disney, login, natal, ofertas) worked, which is exactly why it looked intermittent. All 70 special-char blocks in the sandbox URL-decode to the published key 1:1 — a systematic encoding contract mismatch, not a fluke.

Fix

Canonicalise base keys with decodeURIComponent when applying an override, so an override block finds and displaces its differently-encoded twin (a null override removes it too). Defensive: a malformed % sequence falls back to the raw key rather than throwing. Exact-match keys — including the admin withBlocksOverride partial-render path — are byte-for-byte unchanged.

Tests

3 new regression tests in loader.test.ts (home encoded-vs-raw twin collapses to one block rendering the draft; plain keys still replace; null draft removes the encoded twin). Full @decocms/blocks suite green: 1396/1396, typecheck clean.

Follow-up (not here, cross-repo)

The root contract mismatch is that preview-studio emits raw block keys while the published decofile encodes them. Aligning the source is worth doing separately; this framework-side canonicalisation is the defensive fix at the deterministic merge chokepoint (and an override should always replace, never duplicate).

🤖 Generated with Claude Code


Summary by cubic

Fixes draft preview merging by matching override blocks to base blocks using percent-decoded keys, so raw Studio keys replace their encoded published twins. Restores draft rendering for pages with spaces/accents without changing exact-match override behavior.

  • Bug Fixes
    • Decode base block keys when merging ?__draft= overrides; fall back to raw key on decode errors.
    • Overrides replace or delete the encoded twin to avoid duplicate pages- entries.
    • Added regression tests for home (encoded vs raw), plain keys, and null delete.

Written for commit e2f66ec. Summary will update on new commits.

Review in cubic

The pull-based `?__draft=` preview merges the Studio sandbox decofile on
top of the site's published blocks in `loadBlocks()`. The two decofiles
disagree on how block keys spell special characters: the published
decofile percent-encodes them (`pages-Home%20(principal)-…`) while the
Studio draft sandbox emits them raw (`pages-Home (principal)-…`).

The old key-merge is literal, so a raw-keyed draft block was ADDED
alongside the encoded published block instead of REPLACING it — leaving
two `pages-` blocks with the same `.path`. `findPageByPath` returns the
first (published) one, so the draft edit silently never rendered: the
preview badge showed, headers/cookie/cache-bypass were all correct, but
the page content stayed published.

Measured against casaevideo's live decofiles this hit 78 of 107 pages
(~73%) — every page whose block name contains a space or accent, home
and afiliados included. Pages with plain ASCII keys (disney, login,
natal, ofertas) were unaffected, which is why it looked intermittent.

Fix: canonicalise base keys by `decodeURIComponent` when applying an
override so an override block finds and displaces its differently-encoded
twin (and a `null` override removes it). Exact-match keys — the admin
`withBlocksOverride` partial-render path — are unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@guitavano
guitavano requested a review from a team August 7, 2026 11:36
@guitavano
guitavano merged commit 319b4ed into main Aug 7, 2026
1 check passed
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 7.34.2 🎉

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