An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): restore the playground preview cookie guard (#6897)
Kenton Quatman76d2fbdf5722
diff --git a/apps/docsite/e2e/playground-preview-isolation.spec.ts b/apps/docsite/e2e/playground-preview-isolation.spec.tsindex 1c589c457c..201d1f2344 100644--- a/apps/docsite/e2e/playground-preview-isolation.spec.ts+++ b/apps/docsite/e2e/playground-preview-isolation.spec.ts@@ -153,6 +153,58 @@ function exampleRendering(text: string) {}test.describe('playground preview isolation', () => {+ test('survives a deployment-injected cookie probe', async ({page}) => {+ // Vercel adds its Toolbar cookie probe to Next's shared main-app bundle.+ // Insert the same probe immediately after our bootstrap guard but before+ // the Webpack runtime, making this stricter than the deployed ordering.+ await page.route(+ /\/_next\/static\/chunks\/main-app-[^/]+\.js/,+ async route => {+ const response = await route.fetch();+ const body = await response.text();+ const runtimeStart = body.indexOf('(self.webpackChunk_N_E=');+ expect(runtimeStart).toBeGreaterThan(0);+ const cookieProbe = [+ "if (location.pathname === '/playground/preview') {",+ ' void document.cookie;',+ '}',+ ].join('\n');+ await route.fulfill({+ response,+ body: [+ body.slice(0, runtimeStart),+ cookieProbe,+ body.slice(runtimeStart),+ ].join('\n'),+ });+ },+ );++ await page.goto('/playground');++ await expectPreviewToRender(page, 'Welcome');+ await expect(+ page.getByText('Build error', {exact: true}),+ ).not.toBeVisible();+ expect(+ await currentPreviewFrame(page).evaluate(() => {+ const blocked = (probe: () => unknown) => {+ try {+ probe();+ return false;+ } catch {+ return true;+ }+ };+ return {+ cookie: document.cookie,+ localStorage: blocked(() => window.localStorage),+ parentDocument: blocked(() => window.parent.document),+ };+ }),+ ).toEqual({cookie: '', localStorage: true, parentDocument: true});+ });+test('reports a compiler bootstrap failure instead of building forever', async ({page,}) => {diff --git a/apps/docsite/next.config.mjs b/apps/docsite/next.config.mjsindex 15d48f568c..a4d0f839ed 100644--- a/apps/docsite/next.config.mjs+++ b/apps/docsite/next.config.mjs@@ -2,14 +2,24 @@/*** @file Configure the docsite's routes, response headers, and theme resolution.- * @input Next.js build configuration and staged preview-only static exports.+ * @input Next.js build configuration, the early playground cookie guard, and+ * staged preview-only static exports.* @output Docsite routes plus Storybook and Sandbox at /storybook/ and /sandbox/.+ * Client main-app chunks start with the preview's cookie compatibility guard.* @position Next.js configuration for the existing Vercel docsite deployment.*/-import {readdirSync} from 'node:fs';+import {readFileSync, readdirSync} from 'node:fs';import {resolve} from 'node:path';+const playgroundCookieCompatibility = readFileSync(+ resolve(+ import.meta.dirname,+ 'src/app/playground/preview/cookieCompatibility.js',+ ),+ 'utf8',+);+const stagesStaticApps =process.env.VERCEL_ENV === 'preview' ||process.env.VERCEL_ENV === 'production';@@ -101,7 +111,7 @@ const nextConfig = {},];},- webpack: config => {+ webpack: (config, {isServer, webpack}) => {// Webpack's CSS @import resolver doesn't follow package.json "exports".// Map each theme's /theme.css subpath to the actual dist file.const themesDir = resolve(import.meta.dirname, '../../packages/themes');@@ -116,6 +126,21 @@ const nextConfig = {);}+ // Vercel can add Toolbar instrumentation to Next's shared main-app entry.+ // Its cookie probe throws before React starts in the playground's opaque+ // frame, so prepend our route-scoped guard to the final client asset. A+ // layout Script is too late: Next emits the async main-app tag first.+ if (!isServer) {+ config.plugins.push(+ new webpack.BannerPlugin({+ banner: playgroundCookieCompatibility,+ entryOnly: true,+ include: /main-app-/,+ raw: true,+ }),+ );+ }+return config;},};diff --git a/apps/docsite/src/app/playground/preview/cookieCompatibility.js b/apps/docsite/src/app/playground/preview/cookieCompatibility.jsnew file mode 100644index 0000000000..753732bce4--- /dev/null+++ b/apps/docsite/src/app/playground/preview/cookieCompatibility.js@@ -0,0 +1,24 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Browser bootstrap guard for deployment tooling that probes cookies.+ * @input The current document and its pathname.+ * @output An inert cookie property only for the opaque-origin playground frame.+ * @position Prepended to the client main-app entry by next.config.mjs so it runs+ * before framework and deployment-injected modules.+ */+(() => {+ if (globalThis.location.pathname !== '/playground/preview') {+ return;+ }++ try {+ void globalThis.document.cookie;+ } catch {+ Object.defineProperty(globalThis.document, 'cookie', {+ configurable: true,+ get: () => '',+ set: () => {},+ });+ }+})();