Skip to content

fix(portal): link an external card to the portal's own files under its sub-path - #486

Merged
sohail2721 merged 5 commits into
devfrom
sohail/subpath-external-own-files
Nov 27, 2026
Merged

sohail2721 merged 5 commits into
devfrom
sohail/subpath-external-own-files

Conversation

@sohail2721

@sohail2721 sohail2721 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

A portal hosted under a sub-path, such as a preview at /p/<id>/ or acme.github.io/docs, linked the template's "Agent-Friendly Docs" card to the host's root, where there is no file.

What was wrong

The default content links the card like this:

<Card title="Agent-Friendly Docs" href="/llms.txt" external />

fumadocs-core's Link renders an external link as a plain <a target="_blank"> and never hands it to the router. The router is what adds the base path. Under /p/abc123/, the card was href="/llms.txt" and got a 404, while the SDKs card beside it was /p/abc123/sdks. At the root of a domain nothing was wrong.

The fix

An external card now reads a /… address the way the rest of the portal does. external changes only how the card opens: a plain link, in a new tab.

  • One rule, in one place. underBase in lib/base-path.ts puts any root-relative address under the base, as the router does for links. It also leaves https://…, //…, mailto:, relative and # addresses as written.
    • The Card override in components/mdx.tsx uses it for external cards.
    • The images in API descriptions (api-markdown.tsx) use it too, replacing their own copy of the regex.
  • It covers every file the portal serves under the path: llms.txt and the other generated files, the pages' .md copies, files from static/ and the /__downloads/ zips. It also covers a page opened in a new tab.
  • The prerender and the browser both get it. Fumadocs' server MDX entry reuses the same components.
  • Existing build directories need no change, because the fix is in rendering, not in their content.

A change in behavior, under a sub-path only. Under a sub-path, an external card to /pricing now leads to /docs/pricing. One written as /docs/llms.txt now leads to /docs/docs/llms.txt. A Markdown link already behaved this way. To link outside the portal, write a full URL. At the root of a domain nothing changes.

Not covered

Checked

  • A copy of the subpath fixture with the default cards, built at https://docs.test/p/abc123 and driven in headless Chrome:

    Link Before After
    Agent-Friendly Docs card /llms.txt, 404 /p/abc123/llms.txt, 200, new tab
    SDKs card /p/abc123/sdks unchanged
    Same card, portal at the root /llms.txt, 200 unchanged
  • test/portal-template/base-path.test.ts checks underBase. Root-relative addresses go under the base, /docs/llms.txt included, as the router reads it. Every other kind of address is left as written.

  • test/portal-template/card.test.ts (new) renders the real Card override under /docs/. An external card gets the path and still opens a new tab. A card not marked external goes to the router as written.

  • test/helpers/template-render.ts holds the Vite render setup that card.test.ts, page-actions.test.ts and authorization-failure.test.ts now share.

  • test/e2e/portal-build.test.ts:

    • The subpath fixture's figures page links two external cards, to /api/llms.txt and /api/api/search.json. The second address starts like the base and still goes under it.
    • The root build keeps /llms.txt.
  • A mocha run with every test file loaded, as CI loads them, filtered to the root and sub-path e2e and the template's tests: 78 passing, including the template's type check.

I did not run the whole suite.

🤖 Generated with Claude Code

…s sub-path

Fumadocs renders a card marked external as a plain link, which the
router never sees, so under a sub-path the template's Agent-Friendly
Docs card went to /llms.txt at the host's root and got a 404.

The Card override adds the base to the files the portal writes:
llms.txt, llms-full.txt, sitemap.xml, api/search.json and the pages'
.md copies. robots.txt is left out, since the build writes it only at
a domain root. Any other address, and one that already has the base,
stays as written. A test reads the routes, so a new file route that
the set leaves out fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Muhammad Sohail and others added 3 commits November 24, 2026 12:22
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… a base they start with

The `.md` rule claimed a sibling app's `/blog/post.md`, and the "already
under the base" check left `/api/search.json` at the host's root when the
portal itself lives at `/api/`. The set is now exact membership only.

The subpath fixture's index page goes back to `.md`, keeping its inline
image covered, and the figures page carries the external cards instead,
one of them to `/api/search.json` under `/api/`. The e2e previews on
127.0.0.1 and uses `globalThis.fetch`, so the ESLint global is reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Vite sets NODE_ENV to production when it is unset, as it is under mocha
in CI, so every test after the preview loaded React's production
renderer beside the development JSX runtime already loaded, and failed
with "dispatcher.getOwner is not a function". The test now sets
NODE_ENV itself, so Vite leaves it alone, and restores it once the
server closes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@saeedjamshaid saeedjamshaid 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.

Fixes the default Agent-Friendly Docs card under a path, and I found no regressions. CI is green on all 12 jobs and Sonar reports no issues.

Should fix (tests)

  • The e2e's preview server only proves that two files exist, which the suite already checks.
  • card.test.ts is the third copy of the Vite render setup.
  • The "left as written" addresses test externalHref, not the card.

Design (a question)

  • An external card reads a /… link differently from every other link in the portal.
  • If the list stays, it's a second copy of the one in prerender-pages.ts.

Outside this PR

  • I couldn't find a ticket for the Markdown-link click bug you describe. Could you file one?

Comment thread portal-template/src/lib/base-path.ts Resolved
Comment thread portal-template/src/lib/base-path.ts Resolved
Comment thread test/e2e/portal-build.test.ts Resolved
Comment thread test/portal-template/card.test.ts Resolved
Comment thread test/portal-template/card.test.ts Resolved
…ath, as the router does

An external card now reads a `/...` address the way every other link in
the portal does: `underBase` puts any root-relative address under the
base, and `external` changes only how the card opens. The images in API
descriptions share the rule. This drops the list of the portal's own
files, a second copy of the prerender's, and covers static files, the
`.md` copies and pages opened in a new tab.

The template tests share one Vite render helper, the address cases are
plain calls on `underBase`, and the e2e drops its preview server, which
only proved files the suite already checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sohail2721

Copy link
Copy Markdown
Contributor Author

Filed the Markdown-link click bug as #487.

@sonarqubecloud

Copy link
Copy Markdown

@saeedjamshaid saeedjamshaid 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.

All five comments are addressed in a547151: one underBase rule for external cards and images, one shared Vite render helper, and the address cases as plain calls. CI is green, and #487 tracks the click bug.

@sohail2721
sohail2721 merged commit bab883c into dev Nov 27, 2026
17 of 18 checks passed
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