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

fix(v2): remove trailing slash from router links #2465

Closed
wants to merge 1 commit into from

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Mar 25, 2020

Motivation

Resolves #2394

Some hosting providers automatically add a trailing slash to the website links, which leads to the fact that some functionality stops working correctly. For example, in such cases, relative links to other Markdown files do not work. Therefore, to avoid this, we can remove the trailing slash from the internal (which are processed by the router) links before they get to the router. This is safe operation, because we usually get links without a trailing slash, and we can safely delete it.

Have you read the Contributing Guidelines on pull requests?

Yes.

Test Plan

  1. Create a Markdown file in a new subdirectory so that it refers to the file in the parent directory OR follow instructions in To Reproduce section in the issue Hosting environments that add a trailing back slash break page links #2394

docs/main.md:

---
id: main
title: Main page
---

Some page...

docs/sub/index.md:

---
id: sub
title: Subcategory page
---

See [main page](../main).

sidebars.js

module.exports = {
  docs: {
    Root: ['main', 'sub/sub']
  }
}

Now, when navigating to the docs/sub/sub path without a trailing slash and with it (docs/sub/sub/), the result should be the same:

<p><a href="/docs/main">See main page</a>.</p>

Earlier, when navigating a path with a trailing slash, the result was as follows:

<p><a href="/docs/sub/main">See main page</a>.</p>

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: bug fix This PR fixes a bug in a past release. label Mar 25, 2020
@lex111 lex111 requested a review from yangshun March 25, 2020 08:41
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 25, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Mar 25, 2020

Deploy preview for docusaurus-2 ready!

Built with commit af2eb48

https://deploy-preview-2465--docusaurus-2.netlify.com

@yangshun
Copy link
Contributor

Is this truly safe? What if users were doing ../../ to circumvent the issue and now that we strip the trailing slash, their path will be wrong.

But I agree we should try to normalize and either add trailing slashes to all or strip all the trailing slashes.

@lex111
Copy link
Contributor Author

lex111 commented Mar 27, 2020 via email

@lex111
Copy link
Contributor Author

lex111 commented Mar 31, 2020

I checked this change with links of more than one nesting level ([Link](../../id)), it is works fine.

@yangshun
Copy link
Contributor

Oh great, I'll do more testing first.

@yangshun yangshun added this to the v2.0.0-alpha.51 milestone Apr 4, 2020
Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very sure what this PR actually does. If we want to modify the link, why aren't we stripped it from targetLink and modifying the window.location instead? Also, causing side effects on the window in the render path of a component doesn't seem like a great idea IMO, not sure if intended.

@lex111
Copy link
Contributor Author

lex111 commented Apr 5, 2020

What side effects do you see here? Removing the trailing slash (which was added by the hosting provider), we process the page as originally "intended" (i.e. without a slash). Therefore, if you call working linking a side effect, then yes, in this case it is.
We only change the path of current location, just as if you opened this link without trailing slash.

@yangshun
Copy link
Contributor

yangshun commented Apr 5, 2020

The side effect is that you're modifying window.location (which a a global environment) within a component. window.location is not owned by the component. What if there's another similar component in some part of the code base that for some reason wants to add trailing slashes to window.location? Wouldn't both components be competing for control over window.location? What's worse is that you can't guarantee yours will always "win".

Why don't we modify the targetLink instead and leave the window.location alone?

@lex111
Copy link
Contributor Author

lex111 commented Apr 5, 2020

Not sure if this will help in the issue solution. Well then I close it, maybe later it will be resolved by someone else.

@lex111 lex111 closed this Apr 5, 2020
@lex111 lex111 deleted the lex111/iss2394 branch April 5, 2020 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hosting environments that add a trailing back slash break page links
4 participants