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

Hosting environments that add a trailing back slash break page links #2394

Closed
littlespex opened this issue Mar 9, 2020 · 12 comments
Closed
Labels
bug An error in the Docusaurus core causing instability or issues with its execution domain: trailing slash Related to trailing slash config

Comments

@littlespex
Copy link

littlespex commented Mar 9, 2020

🐛 Bug Report

Hosting setups that add a trailing back slash to urls break all inter-site links on the page.

Have you read the Contributing Guidelines on issues?

Yes

To Reproduce

  1. Create a new site using npx @docusaurus/init@next init test-site classic
  2. Create two new sub folders in docs called a and b
  3. Add attached file a.md to a folder, and b.md to b folder.
  4. Replace sidebar.js with attached version.
  5. run npm run build to generate the flat site.
  6. Load the site in any browser by navigating directly to the url of page a: http://127.0.0.1:8080/docs/a/a
  7. Click the link Link to page "b"

Expected behavior

Page b should load.

Actual Behavior

Page Not Found page.

Your Environment

  • Docusaurus version used: 2.0.0-alpha.48
  • Environment name and version (e.g. Chrome 78.0.3904.108, Node.js 10.17.0): Safari 13.0.5 (15608.5.11), Chrome 80.0.3987.132, Firefox 73.0.1
  • Operating system and version (desktop or mobile): macOS 10.15.3 (19D76)

Reproducible Demo

(Paste the link to an example repo, including a siteConfig.js, and exact instructions to reproduce the issue.)

http://littlespex.com/docusaurus/docs/a/a

test-site.zip

@littlespex littlespex 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 Mar 9, 2020
@bravo-kernel
Copy link
Contributor

@littlespex are you sure the browser is adding that slash? Try running your site locally and see if the slash gets added. If not, it most likely is your hosting server.

FYI I am seeing the same behavior on Netlify (adding the slash, breaking the links)

@bravo-kernel
Copy link
Contributor

💡 If you are using Netlify, disabling Pretty URLs should solve the problem. I unchecked the first two boxes to prevent Netlify from both lowercasing the urls and.. adding the trailing slash.

image

@littlespex
Copy link
Author

I had updated the ticket description earlier to mention the hosting environment instead of the browser. Unfortunately we don’t have admin control over our docs environment and the team that runs it has enabled these url rewrite rules for other reasons. Our current workaround is to revert to docusaurus v1 which works as expected.

@davidhagg
Copy link

We are having this issue too running on an Azure Webservice.

@dotansimha
Copy link

Same here, with GH Pages.

@dotansimha
Copy link

dotansimha commented May 19, 2020

My current way to workaround that:

  1. Create a file called static/js/fix-location.js with:
if (window && window.location && window.location.pathname.endsWith('/') && window.location.pathname !== '/') {
  window.history.replaceState('', '', window.location.pathname.substr(0, window.location.pathname.length - 1))
}
  1. Load this file as static JS file in docusaurus.config.js:
  scripts: [
    {
      src: '/js/fix-location.js',
      async: false,
      defer: false,
    },

Code0x58 added a commit to Code0x58/incubator-heron that referenced this issue May 28, 2020
This is as the website deployment 301s to add trailing slashes, and
Docusurus does not yet support that. Without one of the two yielding,
this workaround strips the trailing slash so the hrefs made still work.

Solution pinched from docusurus issue:
facebook/docusaurus#2394
Code0x58 added a commit to Code0x58/incubator-heron that referenced this issue May 28, 2020
This is as the website deployment 301s to add trailing slashes, and
Docusurus does not yet support that. Without one of the two yielding,
this workaround strips the trailing slash so the hrefs made still work.

Solution pinched from docusurus issue:
facebook/docusaurus#2394
nwangtw pushed a commit to apache/incubator-heron that referenced this issue May 29, 2020
This is as the website deployment 301s to add trailing slashes, and
Docusurus does not yet support that. Without one of the two yielding,
this workaround strips the trailing slash so the hrefs made still work.

Solution pinched from docusurus issue:
facebook/docusaurus#2394
@yangshun yangshun removed the status: needs triage This issue has not been triaged by maintainers label Jun 5, 2020
@cliedeman
Copy link

@dotansimha Thanks for the workaround.

Anyone else who uses this fix don't forget to include your baseUrl if your site is not hosted at the root

 scripts: [
    {
      src: '/baseUrl/js/fix-location.js',
      async: false,
      defer: false,
    },

@slorber
Copy link
Collaborator

slorber commented Aug 31, 2020

Hey, I found the original issue description not so easy to understand (particularly that md files are in zip files 🤪 that does not help to scan this issue). To clarify the problem, here's what I understand:

  • Locally you have a page at /folder/myDoc
  • It contains a relative link like myLink, supposed to navigate to /folder2/myTargetDoc
  • As this link is relative, the trailing slash added automatically breaks the link, as we now navigate to /folder/folder2/myTargetDoc and to make it work in production, we would have to write [myLink](../../folder2/myTargetDoc)

So this bug is only related to using relative links, is that correct @littlespex ?

Is it the problem you also encounter @davidhagg @dotansimha @cliedeman ? Or is it something else?

We should find a solution to this problem (track #3372), but in the meantime, possible workarounds are:

  • using absolute paths [myLink](/folder2/myTargetDoc)
  • using fs-based linking (only works for docs) [myLink](../folder2/myTargetDoc.md)

@littlespex littlespex changed the title Browser added trailing back slash breaks page links Hosting environments that add a trailing back slash break page links Sep 1, 2020
@littlespex
Copy link
Author

@slorber I believe this issue happens on any link that does not contain a trailing backslash. We had to revert to docusaurus v1, so I no longer have a working example.

nicknezis pushed a commit to apache/incubator-heron that referenced this issue Sep 14, 2020
This is as the website deployment 301s to add trailing slashes, and
Docusurus does not yet support that. Without one of the two yielding,
this workaround strips the trailing slash so the hrefs made still work.

Solution pinched from docusurus issue:
facebook/docusaurus#2394
@MarkFarmiloe
Copy link

In docusaurus/packages/docusaurus/src/client/normalizeLocation.ts
any trailing /index.html is knocked off the path.
If you knock off any trailing / as well with
pathname = pathname.replace(/\/$/, '');
on line 23, this seems to fix this bug.

But I don't know whether this would break anything else.
But it does seem to me that if you want to normalize, then allowing both /foo/bar/ and /foo/bar doesn't seem to be a very good job of normalizing to me.

MarkFarmiloe added a commit to MarkFarmiloe/docusaurus that referenced this issue Apr 26, 2021
@veeramarni
Copy link

I'm having the same issue, it breaks URL navigation due to HTTP proxy services. I use `Nginx in the docker, when I directly open a link without trailing slash, it redirects to HTTP domain with the port used in Nginx. Since we don't run on 443 port it breaks the public link.

For example, if I try to open https://domain.com/a it redirects to http://domain.com:8080/a/ and it fails to load the page. But if you open https://domain.com/a/ it works fine. Does anyone have the same issue?

@slorber
Copy link
Collaborator

slorber commented Apr 28, 2021

FYI there is a related issue here: #3372 so I'm going to close this one.

I proposed a temporary workaround here: #3372 (comment)

Using client-side normalization is not bad but it won't prevent the host to eventually add a useless server redirect to the URL with a trailing slash.

I used it successfully on React-Native website:

  • using Github Pages
  • using Netlify with pretty URLs disabled (the global "disable asset optimization" checkbox is broken: make sure "pretty URL" is really unchecked)

The PR I linked to contains a plugin that to will prevent the host to add a trailing slash and prevent a useless server redirect.

Please try my workaround with your specific hosting env, and report on the other issue if the workaround works or not for your usecase.

If the workaround works for everyone, I'll generalize it and add it by default in Docusaurus core, so your feedback is important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution domain: trailing slash Related to trailing slash config
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants