Skip to content

fix(docs-next): fetch the markdown twin once when copying markdown - #371

Merged
nickvigilante merged 2 commits into
mainfrom
vigilante/copy-markdown-base-path
Nov 19, 2026
Merged

nickvigilante merged 2 commits into
mainfrom
vigilante/copy-markdown-base-path

Conversation

@nickvigilante

@nickvigilante nickvigilante commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Merge order

No required order. #370 (Fumadocs 16.19.0) touches other files in docs-next and 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 reports Failed to fetch /docs/docs/...md: 404 (DOCS-ENGINE-R, first seen in release d85dbbf05c). 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 MarkdownCopyButton a twin URL that already carries the base path (markdownTwinUrl(..., assetBasePath) returns /docs/admin/users.md). Fumadocs' button then prefixes import.meta.env.BASE_URL onto it in withBasePath(), and Next's bundler inlines that as the site's base path. The production chunk contains BASE_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's MarkdownCopyButton that fetches markdownUrl exactly as given. Everything else matches upstream: Fumadocs' useCopyButton drives the copied state, the button still throws on a non-OK response so Sentry still sees real failures, and the production cache, the ClipboardItem promise (needed for Safari), aria-live, the disabled state while loading, the lucide icons, and the labels are unchanged. It uses the same ghost button 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 on BASE_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.env does not exist, so they could not catch this bug. test/bundler-env.mjs is a new loader hook that gives fumadocs-ui and fumadocs-core modules the BASE_URL the site bundle inlines. It defaults to production's /docs/, and DOCS_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 /docs URLs.

QA

On the preview, the bug cannot be reproduced. Previews are served from the root, where BASE_URL is / 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.

  1. On the preview, open /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 # Users heading.
  2. Still on the preview, open Open > View as Markdown. Expected: the new tab shows the same Markdown you pasted in step 1.
  3. Compare the Copy Markdown button on the preview with production. Expected: it has the same size, colour, icon and hover as before, and nothing beside it moves when you click it.
  4. After this is published, open https://coder.com/docs/admin/users/ and click Copy Markdown. Expected: "Copied Markdown" appears and the paste is the page's Markdown. On production today, the label never changes and nothing is copied. Optional DevTools check: in the Network tab (Chrome and Firefox), the request is /docs/admin/users.md with status 200, not /docs/docs/admin/users.md with 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 the docs preview status.

What to check Production Preview
Copy Markdown copies the page https://coder.com/docs/admin/users/ <preview>/admin/users/
Copy Markdown on the home page https://coder.com/docs/ <preview>/

Testing

  • pnpm lint and pnpm typecheck pass.
  • pnpm --filter @coder/docs-next test: 367 of 367 pass.
  • I ran the regression test against the old 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.md and expected /docs/v2.36/admin/users.md. With DOCS_NEXT_TEST_BASE_URL=/ the old code passes, as a root-served build did.
  • I confirmed in the live bundle on coder.com that Fumadocs' copy button has BASE_URL:"/docs/" inlined, that https://coder.com/docs/docs/admin/users.md returns 404, and that https://coder.com/docs/admin/users.md returns 200.
  • I built apps/docs against the fixture corpus with DOCS_BASE_PATH=/docs, served out/ 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 old page-actions.tsx, it requested /docs/docs/index.md (404), the label never changed, and the page threw Failed 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. MarkdownCopyButton and ViewOptionsPopover prefix import.meta.env.BASE_URL onto any root-relative URL, which surprises a Next app that builds base-path-aware URLs, since Next inlines BASE_URL too. 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.

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
@nickvigilante nickvigilante self-assigned this Nov 18, 2026
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
coder-docs Skipped Skipped Nov 19, 2026 4:11pm UTC

Request Review

@Jesswinters Jesswinters left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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$/;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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
@nickvigilante
nickvigilante merged commit 8a3b897 into main Nov 19, 2026
32 of 34 checks passed
@nickvigilante
nickvigilante deleted the vigilante/copy-markdown-base-path branch November 19, 2026 17:15

This branch is waiting to be deployed

1 inactive and 1 waiting deployments
docs-preview — 4e2949a8 Waiting Nov 19, 2026 by coder-superblink[bot] via preview / deploy preview #53
Preview — 4e2949a8 Deployed Nov 19, 2026 by vercel[bot]
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.

2 participants