Skip to content

Scope the site preview cookie to the preview route so customization/theme persists on navigation (RND-12191) - #4481

Open
zenoachtig wants to merge 5 commits into
mainfrom
claude/tender-allen-wjt5jb
Open

Scope the site preview cookie to the preview route so customization/theme persists on navigation (RND-12191)#4481
zenoachtig wants to merge 5 commits into
mainfrom
claude/tender-allen-wjt5jb

Conversation

@zenoachtig

@zenoachtig zenoachtig commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes RND-12191: unsaved customization settings (and previewed theme) are lost as soon as you navigate to another page inside the site customization preview.

Note: the first version of this PR changed the cookie's SameSite to None. That was a misdiagnosis (thanks @chatgpt-codex-connector for catching it): app.gitbook.com and sites.gitbook.com are both under the gitbook.com registrable domain, so the preview iframe is cross-origin but same-site, and a SameSite=Lax host-only cookie was already eligible on in-iframe navigation. That change has been reverted; the section below is the corrected fix.

Root cause — cookie path scoping. The override is seeded onto the first request via a ?customization= query param; the middleware applies it and persists it as a preview cookie so that param-less in-iframe navigations keep it (packages/gitbook/src/middleware.ts:420-448). In production the preview is served under sites.gitbook.com/preview/<siteId>/… (mode: 'url-host'), but getPreviewCookieResponse scoped the cookie to siteURLData.siteBasePath — the site's canonical base path (e.g. /docs), which does not prefix the preview route. So:

  • First load works — the value comes from the query param, not the cookie.
  • In-iframe navigation issues a request under /preview/<siteId>/… with no param; the browser withholds the cookie because its path (/docs) doesn't match → the middleware falls back to the saved settings → the override disappears. Exactly the reported symptom (theme too).

Fix. Scope the preview cookie to the preview route prefix <previewBasePath>/<siteId> (e.g. /preview/<siteId>), derived from the request URL, so it is resent on every in-preview navigation regardless of the site's canonical base path. This mirrors getVisitorAuthBasePath (lib/data/visitor.ts), which already derives the path from the request identifier for proxy requests for the same reason. Falls back to siteBasePath for any preview not served under the standard preview route. SameSite stays Lax (correct for the same-site default). Dev (mode: 'url') was already correct and is unchanged.

Tests. Added preview.test.ts cases for getPreviewCookieResponse: url-host preview scopes to /preview/<siteId> (not siteBasePath), url mode scopes to /url/<host>/preview/<siteId>, and the non-preview url-host request falls back to siteBasePath. Verified the url-host case fails against the old behaviour and passes with the fix.

Still worth a browser check before merge: open customization in GBO, change a color, click to another preview page, and confirm the cookie (path=/preview/<siteId>) is resent and the override persists.

Changelog

  • [Fix] Previewed customization and theme changes now persist when navigating between pages inside the site preview.

… preview (RND-12191)

The site preview runs in a cross-site iframe. The preview cookie that carries
the unsaved customization (and theme) override was set with SameSite=Lax, so the
browser withheld it on in-iframe navigations: the first page reflected the
override (it comes from the ?customization= query param) but clicking to another
page sent no param and no cookie, so the middleware fell back to the saved
settings and the change disappeared.

Send the preview cookie with SameSite=None + Secure in production, mirroring the
visitor cookies that already have to survive this same iframe
(getResponseCookiesForVisitorAuth). Dev stays same-origin so no attributes are
needed there. SameSite=None requires Secure, so both are gated on production.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DwxstGi1QSoV6NQBdVDBye
@linear-code

linear-code Bot commented Aug 10, 2026

Copy link
Copy Markdown

RND-12191

@changeset-bot

changeset-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3fdd9de

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
gitbook Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@argos-ci

argos-ci Bot commented Aug 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
customers-v2-cloudflare (Inspect) ✅ No changes detected 3 ignored Aug 19, 2026, 10:01 AM
customers-v2-vercel (Inspect) ✅ No changes detected 3 ignored Aug 19, 2026, 10:01 AM
v2-cloudflare (Inspect) ⚠️ Changes detected (Review) 4 changed, 2 ignored Aug 19, 2026, 10:03 AM
v2-vercel (Inspect) ⚠️ Changes detected (Review) 1 changed, 5 ignored Aug 19, 2026, 10:03 AM

@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Style invalidation on a large API reference

Elements restyled by opening one popup on the Snyk API reference. A share near or above 100% means the insertion restyles the whole document.

interaction restyled page share budget
openapi-select 581 10,845 5.4% 25%
search 1,081 10,845 10.0% 125%

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c7a3c4fd4f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/gitbook/src/lib/preview.ts Outdated
…D-12191)

Re-diagnosis after review: app.gitbook.com and sites.gitbook.com are both under
the gitbook.com registrable domain, so the preview iframe is cross-origin but
same-site — a SameSite=Lax host-only cookie was already eligible on in-iframe
navigation, and the earlier SameSite=None change did not address the reported
loss in the default flow. Reverted that.

The real cause is the cookie path. In production the preview is served under
`sites.gitbook.com/preview/<siteId>/...` (url-host mode), but the cookie was
scoped to `siteURLData.siteBasePath`, the site's canonical base path (e.g.
`/docs`), which does not prefix the preview route. The browser therefore
withheld the cookie on the next in-preview navigation, so the override — seeded
on the first page via the `?customization=` query param — was lost.

Scope the cookie to the preview route prefix `<previewBasePath>/<siteId>`,
derived from the request URL, mirroring getVisitorAuthBasePath for proxy
requests. Falls back to siteBasePath for any preview not served under the
standard preview route. Added preview.test.ts cases covering url-host, url, and
the non-preview fallback.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DwxstGi1QSoV6NQBdVDBye
@zenoachtig zenoachtig changed the title Fix customization/theme preview lost when navigating inside the preview (RND-12191) Scope the site preview cookie to the preview route so customization/theme persists on navigation (RND-12191) Aug 18, 2026
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DwxstGi1QSoV6NQBdVDBye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants