Repository navigation
Open
Labels
bugAn error in the Docusaurus core causing instability or issues with its execution
Description
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
When you use named anchors (E.g., <a name="anchor">Some Text</a> or self-closing ) in a doc page, onBrokenAnchors reports it as a broken link.
Reproducible demo
No response
Steps to reproduce
- In
docusaurus.config.js, setonBrokenAnchorstoWarn(if not using the default setting). - Edit a doc page.
- Add a named anchor anywhere on the page. E.g.,
<a name="anchor">Some Text</a> - Add a link to the named anchor, e.g.,
[Link Text](#anchor) - Save the page.
- Build the site (
yarn build).
Expected behavior
No console feedback. onBrokenAnchors ignores the arbitrary named anchor because it's only supposed to work for anchors declared with the Heading component.
Actual behavior
onBrokenAnchors reports the named anchor as a broken anchor. Example:
[WARNING] Docusaurus found broken anchors!
Please check the pages of your site in the list below, and make sure you don't reference any anchor that does not exist.
Note: it's possible to ignore broken anchors with the 'onBrokenAnchors' Docusaurus configuration, and let the build pass.
Exhaustive list of all broken anchors found:
- Broken anchor on source page path = /user-types-and-permissions:
-> linking to #anchor (resolved as: /user-types-and-permissions#anchor)
Note that on the rendered page, the anchor works.
Your environment
- Public source code:
- Public site URL: (issue encountered locally during build)
- Docusaurus version used: 3.1.1
- Environment name and version (e.g. Chrome 89, Node.js 16.4): Version 120.0.6099.129 (Official Build) (arm64)
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS Ventura 13.4.1 (22F82)
Self-service
- I'd be willing to fix this bug myself.
Reacted by Calvin Wilkinson, Carl Reid and Blue Falcon
Metadata
Metadata
Assignees
Labels
bugAn error in the Docusaurus core causing instability or issues with its execution
Activity
slorber commentedon Feb 1, 2024
We'll try to improve this, but this is currently the intended behavior, because all anchors must be "collected" for the broken link checker to see them.
CF-related issue: #9763 (comment)
The current suggested workaround (requiring v3.1.1) is:
Please note that
<a name="xyz"/>is not valid anymore as per HTML5 spec, that's why I choose to not support it in our Link component.See https://stackoverflow.com/questions/484719/should-i-make-html-anchors-with-name-or-id
I'm keeping this open for now, as there's an opportunity for us to improve the DX.
But please share more details about your use case. For now, it's hard to know if you created that JSX inside a React file or an MDX file, and that matters to me to design an improved solution, for which the technical solution could be radically different (remark plugin VS custom JSX pragma).
Reading the repro steps, I kind of understand that the anchor is in an MDX doc file right?
Would you expect this to also work inside a React file such as
src/pages/index.js?jrahal-mx commentedon Feb 1, 2024
Yes exactly, my immediate use is to be able to have arbitrary (non-heading) anchors in MDX files. I haven't needed to use them in React pages.
I would love for
onBrokenAnchorsto be able to validate arbitrary anchors, along with heading anchors, in MDX pages. That's the case with the workaround you provided (Thanks!).If
onBrokenAnchorscan't validate arbitrary anchors, I'd expect it to ignore them instead of mistakenly reporting them as broken.slorber commentedon Feb 2, 2024
We'll try to add support for
<div id="anchor"/>,<a id="anchor"/>etc in Markdown files.However it's not possible to add support for
<MyCustomComponent id="anchor"/>and you'll have to keep using the Docusaurus API in that custom component.We validate each link anchor against all the anchors we successfully extracted from the target page, so it's important to collect all of them.
For a given page, we either validate or not, but there's no middle ground. What you ask for basically means disabling the broken anchor check
jrahal-mx commentedon Feb 2, 2024
That would be great!
I understand. That's not what I want. I would much rather the arbitrary anchors get checked along with the heading anchors. It's just that right now,
<a>anchors return false errors because they're not supported, which makes the broken anchors check noisy.I really appreciate your taking the time to consider this, BTW.
nijikokun commentedon Apr 9, 2024
Is there some way to suppress this warning, this is currently blocking us from upgrading
slorber commentedon Apr 11, 2024
@nijikokun a warning does not prevent your site from building, and yes this is configurable as explained in the docs and release notes:
https://docusaurus.io/blog/releases/3.1#broken-anchors-checker
onBrokenAnchors: "ignore"Disabling it is not a big deal, it just reverts to what we had in v3.0-
But it would be better to fix the broken anchors and help us figure out when we report false positives (if it's a different case from the one above)
CalvinWilkinson commentedon Apr 23, 2024
OzakIOne commentedon Apr 23, 2024
CalvinWilkinson commentedon Apr 24, 2024
CalvinWilkinson commentedon Apr 24, 2024
slorber commentedon Apr 25, 2024
lachieh commentedon Jul 23, 2024
I came across this issue because we include a bunch of generated content in our docs which includes a few anchors like this:
The tool that generates the content has a PR open to align to HTML5 spec, but in the meantime I made a quick little rehype plugin that will swap all
nameattributes toidinstead. This is definitely a preference but it helped remove all the errors and still works as intended.Here is the code if anyone also has this issue:
rehypeNameToId.ts:docusaurus.config.ts:5 remaining items
Josh-Cena commentedon Sep 3, 2024
Josh-Cena commentedon Sep 3, 2024
anonhostpi commentedon Sep 3, 2024
slorber commentedon Sep 3, 2024
I'd suggest opening a different issue/discussion for the case of h1 headings in particular.
Let's keep this issue clean related to
onBrokenAnchorsreporting#anchoras a broken link for<a id="anchor"/>.I'll hide all the h1-related discussions above.
refactor: fix "broken link anchors" warnings
Rfecator Jellyfin Vue anchor usage
Refactor Jellyfin Vue anchor usage
Refactor Jellyfin Vue anchor usage
ilg-ul commentedon Apr 23, 2025
I encountered the same issue, I have a plugin that generates reference pages and local anchors like
<a id="anchor">are not collected. The<Link id="anchor">workaround seems to be functional.I guess I can live with the workaround, but it would be nice to have it documented properly.
danbarr commentedon Oct 9, 2025
I'm also seeing broken anchor warnings for h2/h3 headings that I want to keep out of the ToC nav (because they're inside Tabs components and/or
<details>blocks), using the documented example of<h3 id="anchor-name">...</h3>If there's a collection solution forthcoming for
<a id=...anchors, the same for other tag types would be nice too.Add anchor workaround
slorber commentedon Oct 9, 2025
We have an API for that: https://docusaurus.io/docs/next/docusaurus-core#useBrokenLinks
Then you can use this React component anywhere in your MDX code, and Docusaurus broken link checker will know that the anchor exists.
Even better, use our built-in
@theme/Headingcomponent that already includes this feature, among other little details.To be even more semantically correct, you should use
@theme/MDXComponents/Heading: this is the same component we use to render Markdown headings. Although technically it doesn't change anything since it just uses our general@theme/Headingcomponent under the hood.The goal of this issue is to make the solutions above "automatic", by simply writing
<h2 id="anchor">in MDX, and not just for headings but also for any arbitrary tag.Add anchor workaround
danbarr commentedon Oct 11, 2025
TIL! Thanks, the
Headingcomponent works well, in fact even better inside a TabItem because the anchor lands in the right spot; with theh3tag it was landing a few lines too low.Add anchor workaround