Skip to content

onBrokenAnchors reports named anchor tags as broken links #9808

Open

Description

@jrahal-mx

Have you read the Contributing Guidelines on issues?

Prerequisites

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

  1. In docusaurus.config.js, set onBrokenAnchors to Warn (if not using the default setting).
  2. Edit a doc page.
  3. Add a named anchor anywhere on the page. E.g., <a name="anchor">Some Text</a>
  4. Add a link to the named anchor, e.g., [Link Text](#anchor)
  5. Save the page.
  6. 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.

Activity

  1. added
    bug
    status: needs triage
    on Jan 30, 2024
  2. removed
    status: needs triage
    on Feb 1, 2024
  3. slorber commented on Feb 1, 2024

    @slorber
    Collaborator

    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:

    import Link from '@docusaurus/Link';
    
    <Link id="my-anchor"/>
    

    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?

  4. jrahal-mx commented on Feb 1, 2024

    @jrahal-mx
    Author

    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 onBrokenAnchors to be able to validate arbitrary anchors, along with heading anchors, in MDX pages. That's the case with the workaround you provided (Thanks!).

    If onBrokenAnchors can't validate arbitrary anchors, I'd expect it to ignore them instead of mistakenly reporting them as broken.

  5. slorber commented on Feb 2, 2024

    @slorber
    Collaborator

    I would love for onBrokenAnchors to be able to validate arbitrary anchors, along with heading anchors, in MDX pages. That's the case with the workaround you provided (Thanks!).

    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.

    If onBrokenAnchors can't validate arbitrary anchors, I'd expect it to ignore them instead of mistakenly reporting them as broken.

    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

  6. jrahal-mx commented on Feb 2, 2024

    @jrahal-mx
    Author

    We'll try to add support for <div id="anchor"/>, <a id="anchor"/> etc in Markdown files.

    That would be great!

    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

    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.

  7. nijikokun commented on Apr 9, 2024

    @nijikokun

    Is there some way to suppress this warning, this is currently blocking us from upgrading

  8. slorber commented on Apr 11, 2024

    @slorber
    Collaborator

    @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)

  9. CalvinWilkinson commented on Apr 23, 2024

    @CalvinWilkinson
  10. OzakIOne commented on Apr 23, 2024

    @OzakIOne
  11. CalvinWilkinson commented on Apr 24, 2024

    @CalvinWilkinson
  12. CalvinWilkinson commented on Apr 24, 2024

    @CalvinWilkinson
  13. slorber commented on Apr 25, 2024

    @slorber
  14. lachieh commented on Jul 23, 2024

    @lachieh
    Contributor

    Please note that is not valid anymore as per HTML5 spec, that's why I choose to not support it in our Link component.

    I came across this issue because we include a bunch of generated content in our docs which includes a few anchors like this:

    - <a name="item-1" /> Item 1
    

    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 name attributes to id instead. 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:

    import { Plugin } from 'unified';
    import { hasProperty } from 'hast-util-has-property';
    import { Nodes } from 'hast';
    import { visit } from 'unist-util-visit';
    
    const rehypeNameToId: Plugin<never> = () => {
      return (tree) => {
        visit(tree, 'element', (node: Nodes) => {
          if (hasProperty(node, 'name')) {
            node.properties.id = node.properties.name;
            node.properties.name = '';
          }
        });
      };
    };
    
    export default rehypeNameToId;
    

    docusaurus.config.ts:

    import rehypeNameToId from './rehypeNameToId.ts'
    
    export default {
      // repeat in preset, docs-plugins, blog-plugins, etc.
      presets: [
        [
          'classic',
          {
            blog: {
              rehypePlugins: [rehypeNameToId],
            }
          },
        ]
      ]
    }
    
  15. 5 remaining items

  16. Josh-Cena commented on Sep 3, 2024

    @Josh-Cena
  17. Josh-Cena commented on Sep 3, 2024

    @Josh-Cena
  18. anonhostpi commented on Sep 3, 2024

    @anonhostpi
  19. slorber commented on Sep 3, 2024

    @slorber
    Collaborator

    I'd suggest opening a different issue/discussion for the case of h1 headings in particular.

    Let's keep this issue clean related to onBrokenAnchors reporting #anchor as a broken link for <a id="anchor"/>.

    I'll hide all the h1-related discussions above.

  20. ilg-ul commented on Apr 23, 2025

    @ilg-ul
    Contributor

    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.

  21. danbarr commented on Oct 9, 2025

    @danbarr

    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.

  22. added a commit that references this issue on Oct 9, 2025
  23. slorber commented on Oct 9, 2025

    @slorber
    Collaborator

    We have an API for that: https://docusaurus.io/docs/next/docusaurus-core#useBrokenLinks

    import useBrokenLinks from '@docusaurus/useBrokenLinks';
    
    export default function MyHeading(props) {
      useBrokenLinks().collectAnchor(props.id);
      return <h2 {...props} />;
    }
    

    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/Heading component that already includes this feature, among other little details.

    import Heading from '@theme/Heading'
    
    <Heading as="h2">Text</Heading>
    
    <Heading as="h3">Text</Heading>
    

    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/Heading component 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.

  24. added a commit that references this issue on Oct 9, 2025
  25. danbarr commented on Oct 11, 2025

    @danbarr

    TIL! Thanks, the Heading component works well, in fact even better inside a TabItem because the anchor lands in the right spot; with the h3 tag it was landing a few lines too low.

  26. added a commit that references this issue on Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions