Repository navigation
Closed
Labels
documentationThe issue is related to the documentation of Docusaurus
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
- I am currently getting warnings from onBrokenLinks/onBrokenAnchors. This is what I am ultimately trying to fix.
- Docusaurus doesn't know about them because they link to content that is generated at build time by another plugin that is incapable of using official Link/etc components that track this automatically.
- Docusaurus docs push me in the direction of https://docusaurus.io/docs/docusaurus-core#useBrokenLinks to handle a scenarios like this.
- Problem is I cannot seem to useBrokenLinks in a plugin. Hopefully I am just doing something wrong, but I am not even sure if I am supposed to be able to use this functionality in a plugin given "Docusarus Client API"; does that mean that functionality cannot be used in plugins?
In any case: is it possible to useBrokenLinks in a plugin? If yes, how/what am I doing wrong? If no, is it possible to solve my problem with a plugin (inform docusaurus of routes that are valid so that it doesn't warn/throw on them with onBrokenLinks/onBrokenAnchors).
Reproducible demo
Steps to reproduce
- Add a no-op plugin that tries to import useBrokenLinks like the codesandbox has at
src/plugins/foo/index.js. - Add the
plugins: ['src/plugins/foo']to docusuarus config.
If using the codesandbox link I provide - the above steps are already done (and are the only changes to the sandbox). The project as-is in there will fail to build.
Expected behavior
I hoped to use useBrokenLinks in a plugin to inform docusaurus about anchors/links that it should consider valid to stop receiving false onBrokenLinks/onBrokenAnchors warnings.
Actual behavior
Docusaurus fails to build - it cannot resolve the import of useBrokenLinks in a plugin.
[cause]: Error: Docusaurus could not load module at path "myproject/src/src/plugins/foo/index.js"
Cause: Cannot find module '@docusaurus/useBrokenLinks'
Your environment
- Public source code: https://codesandbox.io/p/devbox/jovial-benji-f4599g
- Public site URL: https://codesandbox.io/p/devbox/jovial-benji-f4599g
- Docusaurus version used: 3.1.1
- Environment name and version (e.g. Chrome 89, Node.js 16.4): Version 123.0.6312.58 (Official Build) (arm64)
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS 14.3.1
Self-service
- I'd be willing to fix this bug myself.
Metadata
Metadata
Assignees
Labels
documentationThe issue is related to the documentation of Docusaurus
Activity
Josh-Cena commentedon Mar 27, 2024
Hi! You cannot use
useBrokenLinksin a plugin file. Plugins are run in Node. You can only use it in a theme component. I think we should explain better how to register anchors directly in the plugin.[-]collecting broken anchors/links in plugins fails with: `Cause: Cannot find module '@docusaurus/useBrokenLinks'`[/-][+]Documentation should also teach registering anchors in plugin code[/+]danielspofford commentedon Mar 27, 2024
Appreciate your confirmation @Josh-Cena - I figured as much after continuing my efforts.
If this is possible can you please point me at anything (docs/source examples/whatever) that would shine some light on how I do that? Exactly what I'm after.
Josh-Cena commentedon Mar 27, 2024
I somewhat suspect that it's impossible, since right now only the rendering code reports back valid anchors. I'll let @slorber answer though; if it's really not achievable, maybe we should have an API. It sounds useful for plugins that create external HTML pages that don't use Docusaurus themes.
danielspofford commentedon Mar 27, 2024
I suspect that as well.
Yeah that is my exact use case. Was trying to work around rohit-gohri/redocusaurus#321.
The problem for me is that:
useBrokenLinks), but I do not have access to the external HTML page's content, so I cannot get anchors I would like to collect from it.Just to see if it was possible, I tried to: generate the anchors in the plugin, stick them in global data, and then access them from a theme component so that I could
useBrokenLinks. But there is an order of operations problem:Regardless of the order of operations problem, it feels wrong to me to serialize and pump that data around in that manner. Seems like allowing for that in plugins like you say would be very nice for this use case.
slorber commentedon Apr 5, 2024
@danielspofford it would greatly help me if you provided a minimal repro that reports broken anchors that you would like to solve. This way, I could see how to fix that repro so that those anchors do not get reported anymore.
Your repro just shows an attempt to import a React hook in Node.js code, something that is illegal). I can't act on it, and fix the problem, because the problem is not showcased in the first place. Show me the problem and I'll help fix it. Seeing a runnable sandbox of your setup that actually reports wrong broken anchors is more helpful than explaining things in text form.
danielspofford commentedon Apr 8, 2024
@slorber here is a minimal repro that reports broken anchors that I would like to solve.
https://codesandbox.io/p/devbox/distracted-cache-7xxfh5
example output
danielspofford commentedon Jan 10, 2025
@slorber here is an updated minimal repro with latest versions that reports broken anchors on
yarn build.https://codesandbox.io/p/devbox/sharp-gagarin-5jqtrn
Please advise.
slorber commentedon Jan 16, 2025
@danielspofford if you are using a third-party package such as redocusaurus, it can't be considered a minimal repro.
I can only help you figure out how to fix the problem if the problem is not nested inside an external package. The underlying problem is that Redocusaurus is not collecting rendered anchors, tracked in rohit-gohri/redocusaurus#321.
Until it supports that, you can disable the broken anchor's checker.
Eventually we could extend the broken anchor checker with additional options to give you the flexibility to ignore Redocusaurus broken links, until it supports this.
For example:
brunomartinspro commentedon Mar 27, 2025
@slorber @Josh-Cena this would be the best solution possible for multiple use cases, is there a current available solution like this?
I'm currently using the Link component for
redocusaurusURL fragments, but i also get a broken link when running thedocusaurus buildcommand.Usage
Component.tsx
But i still get:
brunomartinspro commentedon Apr 14, 2025
@slorber Created an implementation proposal:
#11101
danielspofford commentedon Jul 28, 2025
rohit-gohri/redocusaurus#419 answers my question I created this issue for and fixes the original issue that prompted the question.