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): fix redirect toUrl (windows + trailing slash) #3903

Merged
merged 5 commits into from
Dec 28, 2020

Conversation

saydo17
Copy link
Contributor

@saydo17 saydo17 commented Dec 10, 2020

Motivation

@docusaurs-plugin-client-redirects fails on windows because windows is special and uses \ in path instead of /.

Replacing \ with / before url encoding resolves this issue.

Resolves #3886

Have you read the Contributing Guidelines on pull requests?

Yes

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Dec 10, 2020
@github-actions
Copy link

github-actions bot commented Dec 10, 2020

Size Change: +15 B (0%)

Total Size: 154 kB

ℹ️ View Unchanged
Filename Size Change
website/build/blog/2017/12/14/introducing-docusaurus/index.html 20.7 kB -3 B (0%)
website/build/docs/introduction/index.html 180 B 0 B
website/build/index.html 5.82 kB -1 B (0%)
website/build/main.********.js 109 kB +19 B (0%)
website/build/styles.********.css 17.5 kB 0 B

compressed-size-action

@netlify
Copy link

netlify bot commented Dec 10, 2020

✔️ Deploy preview for docusaurus-2 ready!

🔨 Explore the source changes: 127c3bb

🔍 Inspect the deploy logs: https://app.netlify.com/sites/docusaurus-2/deploys/5fe9b6a10e03ee0008f1b436

😎 Browse the preview: https://deploy-preview-3903--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Dec 10, 2020

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 61
🟢 Accessibility 99
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-3903--docusaurus-2.netlify.app/classic/

@@ -42,7 +42,7 @@ export function toRedirectFilesMetadata(
);
const toUrl = addTrailingSlash(
`${removeTrailingSlash(pluginContext.baseUrl)}${path.join(redirect.to)}`,
);
).replace(/\\/g, '/');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi and thanks for working on this issue.

I have a hard time understanding where the '\' comes from, can you log the baseurl and redirect.to params so that we figure this out?

I feel like your solution works but is more a quickfix, and we should rather fix the problem at its root, where the \ is used instead of /

Copy link
Collaborator

@slorber slorber Dec 28, 2020

Choose a reason for hiding this comment

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

Can you try running this code on your computer?

const path = require("path");

function addTrailingSlash(str) {
  return str.endsWith('/') ? str : `${str}/`;
}

function removeTrailingSlash(str) {
  return removeSuffix(str, '/');
}

function removeSuffix(str, suffix) {
  if (suffix === '') {
    return str; // always returns "" otherwise!
  }
  return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
}


//////////////////////////////////////////
// test code

const baseUrl = "/";
const to = '/docs/something/else';

const toUrl = addTrailingSlash(
  `${removeTrailingSlash(baseUrl)}${path.join(to)}`
);

console.log(toUrl);

I suspect the path.join(to) is what adds the \\, it does not seem correct to use it to construct a URL path.

The weird thing is path.join(to) does nothing for me as there's only 1 param, but maybe the behavior on Windows is different?

We could replace it with the normalizeUrl([baseUrl,to]) in @docusaurus/utils

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be able to get this fixed thanks to CI and windows tests, FYI I'm pushing to your branch so that you are credited for your contribution

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh, unfortunately forgot that our Jest setup on windows is still not good 😓 (#3540)

I believe this "normalizeUrl" could fix the issue, so going to merge but please let me know if you still have the problem after this change I made to your PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slorber Glad to help.

The \ is defiantly coming from the path.join(to). Windows is special and uses \ for it's path separator unlike any other OS.

Here are the results from the test code above.

On Windows, I get \docs\something\else/.
On WSL I get the expected output /docs/something/else/.

I do agree that path.join(to) is probably the best way to to construct URLs, but I did find path.posix.join(to) produces the expected output on Windows.

@slorber slorber linked an issue Dec 28, 2020 that may be closed by this pull request
@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Dec 28, 2020
@slorber slorber changed the title fix(v2): replace \ with / when creating client redirect on windows fix(v2): fix redirect toUrl (windows + trailing slash) Dec 28, 2020
@slorber slorber merged commit 88a6f56 into facebook:master Dec 28, 2020
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.

redirects plugin adds trailing slash plugin-client-redirects fails on Windows
3 participants