diff --git a/.changeset/static-markdown-theme-override.md b/.changeset/static-markdown-theme-override.md new file mode 100644 index 000000000..f3879764d --- /dev/null +++ b/.changeset/static-markdown-theme-override.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Fix 404 on Markdown pages (View as Markdown) in site previews and when a theme is forced with `?theme=`. diff --git a/packages/gitbook/e2e/internal.spec.ts b/packages/gitbook/e2e/internal.spec.ts index 08605450c..f5846d320 100644 --- a/packages/gitbook/e2e/internal.spec.ts +++ b/packages/gitbook/e2e/internal.spec.ts @@ -1125,6 +1125,30 @@ const testCases: TestsCase[] = [ ).toBeVisible(); }, }, + { + name: 'Markdown page with theme cookie', + url: async () => { + const data = await getSiteAPIToken( + 'https://gitbook.gitbook.io/test-gitbook-open/' + ); + + const searchParams = new URLSearchParams(); + searchParams.set('token', data.apiToken); + searchParams.set('theme', 'dark'); + + return `url/${getGitBookPreviewURL(`${data.site}/blocks?${searchParams.toString()}`)}`; + }, + screenshot: false, + run: async (page) => { + // The app opens previews with `?theme=`, which the middleware persists in a cookie. + const markdownURL = new URL(page.url()); + markdownURL.pathname = `${markdownURL.pathname.replace(/\/$/, '')}.md`; + markdownURL.search = ''; + const response = await page.request.get(markdownURL.toString()); + expect(response.status()).toBe(200); + expect(response.headers()['content-type']).toContain('text/markdown'); + }, + }, { name: 'Redirect to app for authentication when missing token', url: async () => { diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 8a75df506..e346b9c8f 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -496,7 +496,9 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { themeMode === CustomizationThemeMode.Dark ? CustomizationDefaultThemeMode.Dark : CustomizationDefaultThemeMode.Light; - } else { + } else if (routeTypeFromPathname !== 'static') { + // Markdown, llms.txt, RSS and sitemap routes only exist under `sites/static`, so + // forcing them dynamic 404s. They don't render a theme anyway. RND-12749. routeType = 'dynamic'; requestHeaders.set(MiddlewareHeaders.Theme, themeMode); }