Repository navigation
fix(docs-next): fetch the markdown twin once when copying markdown - #371
Conversation
Fumadocs' MarkdownCopyButton prefixes import.meta.env.BASE_URL, which Next inlines as the site's base path, onto a URL that already carries it, so Copy Markdown on coder.com/docs requested /docs/docs/<page>.md. Since Fumadocs 16.17.9 the button throws on that 404 instead of copying the error page, so the copy fails outright. The page-actions row now uses its own copy button, which fetches the markdown twin exactly as given, the same URL the Open dropdown links to. The component tests now give fumadocs the bundle's BASE_URL, so a doubled prefix fails them. Assisted-by: AI
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Jesswinters
left a comment
There was a problem hiding this comment.
I found no P0 or P1 issues, so I'm approving.
The diagnosis matches the code. In fumadocs-ui 16.17.9, withBasePath() (layouts/shared/page-actions.js) prefixes import.meta.env.BASE_URL onto any root-relative URL, and we already pass markdownTwinUrl(…, assetBasePath). The fork keeps everything the user sees from upstream: the copied state from useCopyButton, the ClipboardItem promise for Safari, the throw on a non-OK response so Sentry still sees real failures, the production-only cache, aria-live, and the disabled state while loading. It now fetches exactly the URL the Open dropdown links to. The only other fumadocs code that reads BASE_URL is fumadocs-core's search client defaults (api/from = join(BASE_PATH, "/api/search")), and we always pass api explicitly, so search isn't affected.
The loader hook is a good addition: it makes the Node tests see the same BASE_URL the site bundle inlines. I checked that the regression test fails on main's page-actions.tsx with the production bug ('/beta-docs/beta-docs/v2.36/admin/users.md') and passes with this change.
Locally at b546b0a: docs-next 367/367, typecheck clean, oxlint (--deny-warnings) and oxfmt clean. Merged with #370, it's clean and passes 369/369; the imported fumadocs-ui/utils/use-copy-button and components/ui/button exports still exist in 16.19.0. I left two P3s inline.
This review was generated by Coder Agents on behalf of @Jesswinters.
| * Copies the page's markdown twin to the clipboard, fetching `markdownUrl` | ||
| * exactly as given. | ||
| */ | ||
| // Fork of fumadocs-ui 16.17.9 MarkdownCopyButton (layouts/shared/page-actions.tsx) without its BASE_URL prefix. |
There was a problem hiding this comment.
P3: #370 moves fumadocs-ui to 16.19.0, and the other fork notes there were rechecked against it. Whichever of the two lands second should update this provenance note to 16.19.0 after diffing against 16.19.0's MarkdownCopyButton. That way the next audit has an accurate baseline. I found no behaviour change in the copy path between the two versions.
| // Next's bundler inlines on the site (`"/docs/"` in production), so anything of | ||
| // theirs that prefixes it behaves here as it does in the browser. The value | ||
| // matches the base path the component tests put the page under. | ||
| const FUMADOCS = /\/node_modules\/fumadocs-(?:ui|core)\/dist\/.+\.js$/; |
There was a problem hiding this comment.
P3: the hook injects BASE_URL into every fumadocs-ui and fumadocs-core module for every docs-next test, not just the page-actions test. That's the right default because it matches production. But a test that needs the root-served case (previews, offlinedocs, where BASE_URL is /) has no way to get it. You could read the value from an env var with /beta-docs/ as the default, so a future test can opt into /. It isn't needed for this PR.
There was a problem hiding this comment.
Applied in 4e2949a. The hook now reads DOCS_NEXT_TEST_BASE_URL, so a test can opt into /. It defaults to /docs/ rather than /beta-docs/, because /docs/ is the base path production uses now, and the page-actions tests use /docs URLs to match. The regression test still fails on the old page-actions.tsx (/docs/docs/v2.36/admin/users.md). With DOCS_NEXT_TEST_BASE_URL=/, the old code passes, the same as a root-served build. docs-next 367/367, lint and typecheck clean.
… /docs The loader hook hard-coded /beta-docs/, a base path the site no longer uses. It now defaults to the production /docs/ and reads DOCS_NEXT_TEST_BASE_URL, so a test can run as a root-served build such as a preview or the offline export. The page-actions tests move to /docs URLs to match. Assisted-by: AI
Merge order
No required order. #370 (Fumadocs 16.19.0) touches other files in
docs-nextand does not conflict.Summary
Copy Markdown on coder.com/docs has failed on every page since the Fumadocs 16.17.9 upgrade (#298). The button requests
/docs/docs/<page>.md, which is a 404, so nothing is copied and Sentry reportsFailed to fetch /docs/docs/...md: 404(DOCS-ENGINE-R, first seen in released85dbbf05c). This PR makes the button fetch the page's Markdown twin at/docs/<page>.md, the same URL that "View as Markdown" in the Open dropdown already uses.Fix
We pass
MarkdownCopyButtona twin URL that already carries the base path (markdownTwinUrl(..., assetBasePath)returns/docs/admin/users.md). Fumadocs' button then prefixesimport.meta.env.BASE_URLonto it inwithBasePath(), and Next's bundler inlines that as the site's base path. The production chunk containsBASE_URL:"/docs/", so the request becomes/docs/docs/admin/users.md. The URL was already doubled on 16.15.14, but that version copied whatever the fetch returned, so readers silently got the 404 page's body. Starting with 16.17.9, the button throws when!res.ok, which is why the bug became visible with the upgrade.The row now renders our own
CopyMarkdownButton(packages/docs-next/src/copy-markdown-button.tsx), a minimal fork of fumadocs-ui 16.17.9'sMarkdownCopyButtonthat fetchesmarkdownUrlexactly as given. Everything else matches upstream: Fumadocs'useCopyButtondrives the copied state, the button still throws on a non-OK response so Sentry still sees real failures, and the production cache, theClipboardItempromise (needed for Safari),aria-live, the disabled state while loading, the lucide icons, and the labels are unchanged. It uses the sameghostbutton classes as the Open trigger beside it. With Tailwind's class order, that renders the same as the merged classes the Fumadocs button had (text-xs,gap-2, transparent background, no border), so nothing should shift visually. Its labels are English only, like the Open dropdown's.Removing the base path again before handing the URL to Fumadocs was also an option. It was ruled out because it depends on what each host's bundler puts in
BASE_URL. That value differs across the public site (/docs/), the per-version builds, the offline export (no base path), and a Vite host for the in-product docs. With our own button, every host fetches exactly the URL the Open dropdown links to, and no host depends onBASE_URL. The offline export (apps/offlinedocs, no base path,viewOptions: false) keeps requesting the same root-relative twin URL as before.The component tests ran in Node, where
import.meta.envdoes not exist, so they could not catch this bug.test/bundler-env.mjsis a new loader hook that gives fumadocs-ui and fumadocs-core modules theBASE_URLthe site bundle inlines. It defaults to production's/docs/, andDOCS_NEXT_TEST_BASE_URL="/"runs a test as a root-served build (a preview or the offline export). The existing Copy Markdown test is now the regression test, and the page-actions tests use/docsURLs.QA
On the preview, the bug cannot be reproduced. Previews are served from the root, where
BASE_URLis/and the URL was never doubled. So the preview only shows that copying still works; the base path behaviour is covered by the regression test and by checking production after the next publish. These steps work the same in Firefox and Chrome./admin/users/and click Copy Markdown under the page description. Expected: the label changes to "Copied Markdown" for about a second, and pasting into any text field gives the page as Markdown, starting with its# Usersheading./docs/admin/users.mdwith status 200, not/docs/docs/admin/users.mdwith 404.Pages to compare
The preview alias always serves this PR's latest deploy and needs a Vercel login; replace
<preview>with the URL from thedocs previewstatus.<preview>/admin/users/<preview>/Testing
pnpm lintandpnpm typecheckpass.pnpm --filter @coder/docs-next test: 367 of 367 pass.page-actions.tsx, with the new loader hook in place, and it fails with the production bug: it requested/docs/docs/v2.36/admin/users.mdand expected/docs/v2.36/admin/users.md. WithDOCS_NEXT_TEST_BASE_URL=/the old code passes, as a root-served build did.BASE_URL:"/docs/"inlined, thathttps://coder.com/docs/docs/admin/users.mdreturns 404, and thathttps://coder.com/docs/admin/users.mdreturns 200.apps/docsagainst the fixture corpus withDOCS_BASE_PATH=/docs, servedout/under/docs, and clicked Copy Markdown on/docs/in headless Chromium. With this branch, it requested/docs/index.md(200), the label changed to "Copied Markdown", and the clipboard held the page Markdown. With the oldpage-actions.tsx, it requested/docs/docs/index.md(404), the label never changed, and the page threwFailed to fetch /docs/docs/index.md: 404, the same error as in Sentry. Firefox was not available in the workspace, so this check is Chromium only.Upstream
Possibly yes, as an issue rather than a patch.
MarkdownCopyButtonandViewOptionsPopoverprefiximport.meta.env.BASE_URLonto any root-relative URL, which surprises a Next app that builds base-path-aware URLs, since Next inlinesBASE_URLtoo. An option to pass the URL through as is would let us drop the fork. Filing it is your call.Linear: DOCS-1800
🤖 Built with AI assistance.