fix(v2): fix redirect toUrl (windows + trailing slash) (#3903)

* replace \ with / when creating client redirect on windows

* fix(vs): replace \ with / when creating client redirect on windows

* redirects plugin: add createToUrl fn + tests

* redirects plugin: add createToUrl fn + tests
do not  add trailing  slash + try to fix windows path issues

Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
saydo17 2020-12-28 02:55:15 -08:00 committed by GitHub
parent 601c8fe688
commit 88a6f56654
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 20 deletions

View file

@ -6,11 +6,11 @@ Array [
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc/\\">
<link rel=\\"canonical\\" href=\\"/abc/\\" />
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc\\">
<link rel=\\"canonical\\" href=\\"/abc\\" />
</head>
<script>
window.location.href = '/abc/';
window.location.href = '/abc';
</script>
</html>",
]
@ -22,11 +22,11 @@ Array [
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc/\\">
<link rel=\\"canonical\\" href=\\"/abc/\\" />
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc\\">
<link rel=\\"canonical\\" href=\\"/abc\\" />
</head>
<script>
window.location.href = '/abc/';
window.location.href = '/abc';
</script>
</html>",
]
@ -38,22 +38,22 @@ Array [
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/abc/\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/abc/\\" />
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/abc\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/abc\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/abc/';
window.location.href = 'https://docusaurus.io/abc';
</script>
</html>",
"<!DOCTYPE html>
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/def.html/\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/def.html/\\" />
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/def.html\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/def.html\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/def.html/';
window.location.href = 'https://docusaurus.io/def.html';
</script>
</html>",
"<!DOCTYPE html>

View file

@ -9,8 +9,38 @@ import path from 'path';
import writeRedirectFiles, {
toRedirectFilesMetadata,
createToUrl,
} from '../writeRedirectFiles';
// Test to fix toUrl bugs we had:
// - https://github.com/facebook/docusaurus/issues/3886
// - https://github.com/facebook/docusaurus/issues/3925
describe('createToUrl', () => {
test('should create appropriate redirect urls', async () => {
expect(createToUrl('/', '/docs/something/else')).toEqual(
'/docs/something/else',
);
expect(createToUrl('/', '/docs/something/else/')).toEqual(
'/docs/something/else/',
);
expect(createToUrl('/', 'docs/something/else')).toEqual(
'/docs/something/else',
);
});
test('should create appropriate redirect urls with baseUrl', async () => {
expect(createToUrl('/baseUrl/', '/docs/something/else')).toEqual(
'/baseUrl/docs/something/else',
);
expect(createToUrl('/baseUrl/', '/docs/something/else/')).toEqual(
'/baseUrl/docs/something/else/',
);
expect(createToUrl('/baseUrl/', 'docs/something/else')).toEqual(
'/baseUrl/docs/something/else',
);
});
});
describe('toRedirectFilesMetadata', () => {
test('should create appropriate metadatas', async () => {
const pluginContext = {

View file

@ -11,11 +11,7 @@ import {memoize} from 'lodash';
import {PluginContext, RedirectMetadata} from './types';
import createRedirectPageContent from './createRedirectPageContent';
import {
addTrailingSlash,
getFilePathForRoutePath,
removeTrailingSlash,
} from '@docusaurus/utils';
import {getFilePathForRoutePath, normalizeUrl} from '@docusaurus/utils';
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
@ -24,6 +20,10 @@ export type RedirectFileMetadata = {
fileContent: string;
};
export function createToUrl(baseUrl: string, to: string) {
return normalizeUrl([baseUrl, to]);
}
export function toRedirectFilesMetadata(
redirects: RedirectMetadata[],
pluginContext: WriteFilesPluginContext,
@ -40,9 +40,7 @@ export function toRedirectFilesMetadata(
pluginContext.outDir,
getFilePathForRoutePath(redirect.from),
);
const toUrl = addTrailingSlash(
`${removeTrailingSlash(pluginContext.baseUrl)}${path.join(redirect.to)}`,
);
const toUrl = createToUrl(pluginContext.baseUrl, redirect.to);
const fileContent = createPageContentMemoized(toUrl);
return {
...redirect,