-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Markdown content with custom slug
fails to resolve relative links ("error Docusaurus found broken links!")
#6210
Comments
Relative links are only file paths when they end in |
@Josh-Cena If this is not supposed to work at all, why do relative links work if they are placed in a file without custom Also, just to clarify: the problem is linking from a custom slug file, not to it. |
If you don't have a custom slug, then the two files are served with the same base path: |
I am not sure I understand what you are saying. The The problem is only with |
Sorry, was having dinner, didn't explain it clear enough. In Docusaurus, there are two types of paths: file paths and URL paths. File paths are what exist on your system; URL paths are what your site's visitors see in their browser. Without extra configuration, they usually map very intuitively:
What
A key observation is that these two path systems are isomorphic: So, how should you link pages? As implied in my previous response in #6208, Docusaurus wants to prioritize using file paths over URLs in general, because editors can help you update file references during refactoring, but URLs are a downstream feature. Therefore, in However, if you write So, what page does How should you solve it? If you want to use file paths, explicitly tell Docusaurus that it's a file path by adding the extension: Does that clear up some doubts? |
Also, you could ask in our Discord server first if there's anything not working: https://discord.com/invite/docusaurus We can help you figure out if it's a bug first, so we don't clog the GitHub issue tracker. |
Again, @Josh-Cena. Your help has been much appreciated! I will heed your advice and take future issues to Discord first. Thanks for the invite! |
@Josh-Cena Again, thanks for the elaborate answer! I guess, explaining how files and URL resolution works was not necessary. The main piece of information I was missing seems to be this:
That implies that the Some final thoughts: To my mind, relative URL resolution should be relative to the file, not relative to the Maybe, as a middleground, |
URLs will never be relative to the file. When your file has been turned to HTML pages, how does the browser know what the original path is like? You are missing the key part here: file path is server-side, URL path is client-side. The server knows about the client because it builds the client, but the client is ignorant about the server. That's what I mean by saying that "URL is downstream". Also remember that FS structure tells you nothing about the URL structure. Files that happen to be in the same folder can end up with drastically different URL. Can you even provide a well-defined heuristic of what "URL relative to the file" is like? If you can (in the form of pseudocode or some test cases) from there we can make another feature request. And that's exactly the reason why I said you should use MD links instead of URL links. Changing the routes can lead to unexpected broken links. |
As I said: I agree, that fixing the behavior is not an easy feat. My argument does not go against the difficulty of the technical implementation. As I said: I agree that this is not easy to change. But from a documentation editor perspective, it is odd:
I still think, It would be best if it would at least be mentioned in the I guess, your argument is that, in general, relative urls should not be used. I can now agree with that. Using file-based linking also has the advantage that VSCode (or other IDEs) can easily track and fix them when files change. If that is the case, maybe there should be a togglable warning for people who are as stupid as myself :D |
I hope you understand what I had been emphasizing above. URL links, relative or absolute, can still be useful in many cases because file links currently only work when you link files within one plugin. This might be fixed in the future.
Mmm, we can. I've actually explained to multiple people about how routing works. I kind of added some details here: https://docusaurus.io/docs/next/docs-introduction#docs-only-mode but ultimately I think it would be useful to have a centralized place where we talk about all things related to routing: |
Agree that users generally struggle with our routing system. The docs-only mode also makes it even more confusing as you have to combine routeBasePath + slug to set a doc as your homepage. I've seen recently a live stream where a user struggled, also encountered the "onBrokenLinks" error after changing a slug, and wasn't sure how to fix it. In this case, however, we didn't invent anything, and relative links are relative to the current page they appear in, it's just how the web works. Just write 2 HTML files locally in different folders and test for yourself. That's also why we tend to recommend using relative file paths: |
This is not a guess, it is actually a documented recommendation, see above. Is there a better place where we should recommend this? I feel it's in the right place, and you just didn't read the whole doc. It's hard to make everything intuitive and in the end you have to read the doc 🤪
We have a system to detect broken links preventing you to deploy a broken site, isn't it covering this use-case? |
@slorber About my "togglable warning" recommendation - the idea is to show a warning if it is a relative link under "certain circumstances". E.g., in my case I should not try to link to my Maybe, to act as an early warning system - it could become a blanket warning in case of any links that the "server-side URL parser" fails to be able to link to a file?
I guess, this would be an extension of |
We already emit a warning for all links ending with All other links are left untouched and added as is to the final HTML, so it's your responsibility to be sure your own links work. We can't add warnings everywhere, because in the end, using relative links is something legit to do, that some users currently use in production. Do we really want to show them new unwanted warnings while their system is working? I don't know, but if you have a warning in mind, please give a very concrete example of what to display to the user, under which circumstances. Please try to be more specific, using terms like "under certain circumstances" is vague and not very helpful. If you want a new warning in Docusaurus, please create a repro repository highlighting a very concrete problem that you want us to solve. |
@slorber I agree. My last comment was not sufficiently concise, and also tried to over-engineer things. Ultimately, it is probably easiest to go with Josh's idea:
And then, crucially, add a link to the |
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
yarn build
fails witherror Docusaurus found broken links!
if a markdowndocs
page with customslug
has a relative link in it.Steps to reproduce
classic
preset withdocs
enabled, like so:docs
:content/01-ch1/01-sec1.mdx
:content/01-ch1/02-sec2.mdx
:Expected behavior
Relative paths should not error out, even in case of custom
slug
.Actual behavior
yarn build
:NOTE1: This does not seem to be a problem with a slug referring to a different path. It still errors out if
slug: /ch1/
NOTE2: It stops complaining, if I use the absolute path
/ch1/sec2
.Your environment
2.0.0-beta.13
Reproducible demo
No response
Self-service
The text was updated successfully, but these errors were encountered: