Skip to content
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

Add strict client-side routes - always respect presence or absence of a trailing slash #8693

Open
6 of 7 tasks
idoros opened this issue Feb 22, 2023 · 3 comments
Open
6 of 7 tasks
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@idoros
Copy link

idoros commented Feb 22, 2023

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and 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 running a site with trailingSlash: false, the build creates an html file per page, then when going to that page URL with an added slash (e.g. site.com/page/) the server returns a page not found with a 404 code as expected, but the client router is re-rendering the page as if the slash wasn't part of the URL, which in turn causes links to break.

Reproducible demo

https://stylable.io/docs/guides/migration-v3/

Steps to reproduce

  1. open network to see 404 response (you can sometimes see the 404 flickers)
  2. see page is re-rendered with the content as if the slash was not there
  3. go to the Runtime reference link and click it to see a broken link
  4. change the initial url to be without the slash and everything works fine

Expected behavior

Client should respect 404 page and either keep the 404 as is or redirect the URL to remove the slash

Actual behavior

Client is "fixing" routing without actually redirecting the URL resulting in broken links and 404 flicker

Your environment

Self-service

  • I'd be willing to fix this bug myself.
@idoros idoros added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Feb 22, 2023
@slorber
Copy link
Collaborator

slorber commented Feb 23, 2023

then when going to that page URL with an added slash (e.g. site.com/page/) the server returns a page not found with a 404 code as expected,

Actually this is not "expected", it depends on the host and the behavior you want.

You'll notice that Docusaurus website has a different behavior, and both URLs work without returning a 404:

That behavior does not look wrong to me.

You can choose to serve 1 URL, or both, as long as you have the canonical URL set correctly in page metadata, your SEO shouldn't be penalized.

You could figure out how to configure your host to have the exact same behavior as Docusaurus if you want. Some hosts may not allow that easily though.

the client router is re-rendering the page as if the slash wasn't part of the URL

We choose to not use the strict option of React-Router, that decides if we care or not about the trailing slash.
https://v5.reactrouter.com/web/api/Route/strict-bool

This makes sense to be fault tolerant for me by default, and allow most sites to be served successfully from both URLs.

Now we could add a Docusaurus option to make the client-side routing more strict in case you really want to ensure a single URL is successfully served.

I'm going to requalify this issue as a feature request to discuss the introduction of strict client-side routes. Not sure what could be the public API for it. Do we need an extra config attribute? Or maybe it's better to reuse the trailingSlash attribute and allow provide a more complex object value?

which in turn causes links to break.

That's a different problem. You have links that are sensitive to trailing slashes.

Our docs provide linking recommendations: https://docusaurus.io/docs/markdown-features/links

In particular, we discourage the usage of regular html relative links (./xyz, in favor of markdown relative links (./xyz.md) for that reason

@slorber slorber added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. and removed status: needs triage This issue has not been triaged by maintainers bug An error in the Docusaurus core causing instability or issues with its execution labels Feb 23, 2023
@slorber slorber changed the title forgiving client router redirects 404 response and breaks links Add strict client-side routes - always respect presence or absence of a trailing slash Feb 23, 2023
@idoros
Copy link
Author

idoros commented Feb 23, 2023

Thank you for the quick response!

Actually this is not "expected", it depends on the host and the behavior you want.

Fair enough. But the default non-configurable tolerant routing together with the broken links is the main issue (the flickering 404 is a less significant).

So my options are:

  • fixing all the links to be relative file paths with extensions
  • somehow change github-pages behavior to serve the page.html/
  • wait for a configuration option to make the routing strict as the host

Is there a way to override react router configuration?

@slorber
Copy link
Collaborator

slorber commented Feb 23, 2023

fixing all the links to be relative file paths with extensions

In any case that seems like a good to do

somehow change github-pages behavior to serve the page.html/

GitHub pages unfortunately does not provide any server-side option and not really an host I'd recommend if you want any kind of server-side control

wait for a configuration option to make the routing strict as the host

yes

In case you want to contribute (or simply make a PR POC with an hardcoded value), this is where you could add the strict option (similar to exact):

https://github.com/facebook/docusaurus/blob/main/packages/docusaurus/src/server/routes.ts#L101

Is there a way to override react router configuration?

I don't think there's a way to do that in userland without core modifications.

Maybe this could work but not sure: creating a Webpack plugin that modifies the .docusaurus/router.js file just before Webpack compilation starts.

sebcrozet added a commit to dimforge/parry.rs that referenced this issue Feb 2, 2025
* update docusaurus to 3.5

* geometric_representations.mdx first pass

* wip geometric queries

* mesh transformation first pass

* adapt composite shape section

* use file path rather than url paths

prompted by facebook/docusaurus#8693 (comment)

More info https://docusaurus.io/docs/markdown-features/links.

* removed references to path transformation + fix a link

* removed reference to collision detection pipeline

* inject files + fix code snippets

See Vrixyz#1

* use published strinject

* chore: remove outdated sponsors

* fix: remove nonexistant CompositeShape method

---------

Co-authored-by: Sébastien Crozet <sebcrozet@dimforge.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
Development

No branches or pull requests

2 participants