From b546b0aaf769cc062514f66a465a3f46b680c6c1 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Wed, 18 Nov 2026 22:48:07 +0000 Subject: [PATCH 1/2] fix(docs-next): fetch the markdown twin once when copying markdown 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/.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 --- .../docs-next/src/copy-markdown-button.tsx | 57 +++++++++++++++++++ packages/docs-next/src/page-actions.test.tsx | 4 +- packages/docs-next/src/page-actions.tsx | 18 +++--- packages/docs-next/test/bundler-env.mjs | 14 +++++ packages/docs-next/test/setup.mjs | 1 + 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 packages/docs-next/src/copy-markdown-button.tsx create mode 100644 packages/docs-next/test/bundler-env.mjs diff --git a/packages/docs-next/src/copy-markdown-button.tsx b/packages/docs-next/src/copy-markdown-button.tsx new file mode 100644 index 00000000..99a49d2b --- /dev/null +++ b/packages/docs-next/src/copy-markdown-button.tsx @@ -0,0 +1,57 @@ +"use client"; + +import { buttonVariants } from "fumadocs-ui/components/ui/button"; +import { useCopyButton } from "fumadocs-ui/utils/use-copy-button"; +import { Check, Copy } from "lucide-react"; +import { type ReactElement, useState } from "react"; + +// Matches the Open trigger beside it: secondary text, no outline, a filled hover. +const BUTTON_CLASS = `${buttonVariants({ variant: "ghost", size: "sm" })} gap-2 bg-transparent text-fd-muted-foreground [&_svg]:size-3.5 [&_svg]:text-fd-muted-foreground`; + +const cache = new Map(); + +/** + * 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. +export function CopyMarkdownButton({ + markdownUrl, +}: { + /** The page's markdown twin, root-relative and carrying the base path. */ + markdownUrl: string; +}): ReactElement { + const [isLoading, setLoading] = useState(false); + const [checked, onClick] = useCopyButton(async () => { + const cached = cache.get(markdownUrl); + if (cached !== undefined) return navigator.clipboard.writeText(cached); + setLoading(true); + try { + await navigator.clipboard.write([ + new ClipboardItem({ + "text/plain": fetch(markdownUrl).then(async (res) => { + if (!res.ok) throw new Error(`Failed to fetch ${markdownUrl}: ${res.status}`); + const content = await res.text(); + if (process.env.NODE_ENV === "production") cache.set(markdownUrl, content); + return content; + }), + }), + ]); + } finally { + setLoading(false); + } + }); + + return ( + + ); +} diff --git a/packages/docs-next/src/page-actions.test.tsx b/packages/docs-next/src/page-actions.test.tsx index 8f6e0773..b30c2eaa 100644 --- a/packages/docs-next/src/page-actions.test.tsx +++ b/packages/docs-next/src/page-actions.test.tsx @@ -37,7 +37,9 @@ test("the row renders Copy Markdown and the Open dropdown trigger", () => { assert.deepEqual(buttonLabels(root), ["Copy Markdown", "Open"]); }); -test("Copy Markdown copies the page's markdown twin", async (t) => { +test("Copy Markdown fetches the markdown twin under the base path exactly once", async (t) => { + // test/bundler-env.mjs gives fumadocs the bundle's BASE_URL, which its own + // copy button prefixed onto this already-prefixed URL. const fetched: string[] = []; t.mock.method(globalThis, "fetch", async (input: string) => { fetched.push(input); diff --git a/packages/docs-next/src/page-actions.tsx b/packages/docs-next/src/page-actions.tsx index f9ad465e..c79765e5 100644 --- a/packages/docs-next/src/page-actions.tsx +++ b/packages/docs-next/src/page-actions.tsx @@ -1,5 +1,5 @@ -import { MarkdownCopyButton } from "fumadocs-ui/layouts/docs/page"; import type { ReactElement } from "react"; +import { CopyMarkdownButton } from "./copy-markdown-button.js"; import { ViewOptions } from "./view-options.js"; /** @@ -11,15 +11,18 @@ import { ViewOptions } from "./view-options.js"; export type PageActionsOption = false | { viewOptions?: boolean }; /** - * The row of page actions under the page description: fumadocs' Copy Markdown - * button and our own "Open in ..." dropdown, both pointed at the page's - * markdown twin. + * The row of page actions under the page description: our own Copy Markdown + * button and "Open in ..." dropdown, both pointed at the page's markdown twin. * * `markdownUrl` is root-relative and carries the site's base path. - * `MarkdownCopyButton` fetches it and the dropdown links to it as an anchor + * `CopyMarkdownButton` fetches it and the dropdown links to it as an anchor * href, so the browser resolves it against the page's own origin and no * hostname is ever baked into the build. * + * The copy button is ours rather than fumadocs' `MarkdownCopyButton` because + * that one prefixes the bundler's `import.meta.env.BASE_URL`, which doubles a + * URL that already carries the base path. + * * The dropdown is ours rather than fumadocs' `ViewOptionsPopover` because its * assistant prompts name the page's Markdown twin rather than the HTML page, * and it marks only the items that leave the docs (refer to @@ -38,10 +41,7 @@ export function PageActions({ if (options === false) return null; return (
- + {options.viewOptions === false ? undefined : }
); diff --git a/packages/docs-next/test/bundler-env.mjs b/packages/docs-next/test/bundler-env.mjs new file mode 100644 index 00000000..1ec39221 --- /dev/null +++ b/packages/docs-next/test/bundler-env.mjs @@ -0,0 +1,14 @@ +// ESM loader hook: give fumadocs' modules the `import.meta.env.BASE_URL` that +// 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$/; +const BASE_URL = "/beta-docs/"; + +export async function load(url, context, nextLoad) { + const result = await nextLoad(url, context); + if (result.format !== "module" || result.source === undefined || !FUMADOCS.test(url)) + return result; + const env = `import.meta.env = { BASE_URL: ${JSON.stringify(BASE_URL)} };\n`; + return { ...result, source: env + String(result.source) }; +} diff --git a/packages/docs-next/test/setup.mjs b/packages/docs-next/test/setup.mjs index e191a2c1..9b5ea4ce 100644 --- a/packages/docs-next/test/setup.mjs +++ b/packages/docs-next/test/setup.mjs @@ -6,3 +6,4 @@ import { register } from "node:module"; register("./ignore-css.mjs", import.meta.url); register("./mock-next-navigation.mjs", import.meta.url); +register("./bundler-env.mjs", import.meta.url); From 4e2949a8bce3de1fe3842662367be2d418a3f698 Mon Sep 17 00:00:00 2001 From: Nick Vigilante Date: Thu, 19 Nov 2026 16:11:31 +0000 Subject: [PATCH 2/2] test(docs-next): let tests choose the bundle base path and default to /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 --- packages/docs-next/src/page-actions.test.tsx | 8 ++++---- packages/docs-next/test/bundler-env.mjs | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/docs-next/src/page-actions.test.tsx b/packages/docs-next/src/page-actions.test.tsx index b30c2eaa..c4ff9101 100644 --- a/packages/docs-next/src/page-actions.test.tsx +++ b/packages/docs-next/src/page-actions.test.tsx @@ -8,8 +8,8 @@ import { renderToFragment } from "../test/dom.js"; import { PageActions } from "./page-actions.js"; import { ViewOptions } from "./view-options.js"; -const MARKDOWN_URL = "/beta-docs/v2.36/admin/users.md"; -const PAGE_PATH = "/beta-docs/v2.36/admin/users/"; +const MARKDOWN_URL = "/docs/v2.36/admin/users.md"; +const PAGE_PATH = "/docs/v2.36/admin/users/"; const PAGE_URL = `https://coder.com${PAGE_PATH}`; // fumadocs' copy button reads the current pathname through `fumadocs-core`'s @@ -108,7 +108,7 @@ function pathsOf(root: ParentNode): (string | null)[] { } test("the Open trigger carries the shared dropdown caret, so it reads as a dropdown", () => { - const root = renderToFragment(); + const root = renderToFragment(); const trigger = root.querySelector("button"); assert.ok(trigger, "the trigger renders"); assert.equal(trigger.textContent, "Open"); @@ -179,7 +179,7 @@ test("opening the dropdown lists every item, addressed at the page the reader is items.map((item) => item.getAttribute("href")), [ // The twin resolves against the page, so it keeps the base path. - "https://coder.com/beta-docs/v2.36/admin/users.md", + "https://coder.com/docs/v2.36/admin/users.md", `https://scira.ai/?q=${encoded}`, `https://chatgpt.com/?prompt=${encoded}&hints=search`, `https://claude.ai/new?q=${encoded}`, diff --git a/packages/docs-next/test/bundler-env.mjs b/packages/docs-next/test/bundler-env.mjs index 1ec39221..766b7f8f 100644 --- a/packages/docs-next/test/bundler-env.mjs +++ b/packages/docs-next/test/bundler-env.mjs @@ -1,9 +1,10 @@ // ESM loader hook: give fumadocs' modules the `import.meta.env.BASE_URL` that // 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. +// theirs that prefixes it behaves here as it does in the browser. Set +// DOCS_NEXT_TEST_BASE_URL="/" to run a test as a root-served build, such as a +// preview or the offline export. const FUMADOCS = /\/node_modules\/fumadocs-(?:ui|core)\/dist\/.+\.js$/; -const BASE_URL = "/beta-docs/"; +const BASE_URL = process.env.DOCS_NEXT_TEST_BASE_URL ?? "/docs/"; export async function load(url, context, nextLoad) { const result = await nextLoad(url, context);