mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 02:08:55 +02:00
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:
parent
601c8fe688
commit
88a6f56654
3 changed files with 48 additions and 20 deletions
|
@ -6,11 +6,11 @@ Array [
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset=\\"UTF-8\\">
|
<meta charset=\\"UTF-8\\">
|
||||||
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc/\\">
|
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc\\">
|
||||||
<link rel=\\"canonical\\" href=\\"/abc/\\" />
|
<link rel=\\"canonical\\" href=\\"/abc\\" />
|
||||||
</head>
|
</head>
|
||||||
<script>
|
<script>
|
||||||
window.location.href = '/abc/';
|
window.location.href = '/abc';
|
||||||
</script>
|
</script>
|
||||||
</html>",
|
</html>",
|
||||||
]
|
]
|
||||||
|
@ -22,11 +22,11 @@ Array [
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset=\\"UTF-8\\">
|
<meta charset=\\"UTF-8\\">
|
||||||
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc/\\">
|
<meta http-equiv=\\"refresh\\" content=\\"0; url=/abc\\">
|
||||||
<link rel=\\"canonical\\" href=\\"/abc/\\" />
|
<link rel=\\"canonical\\" href=\\"/abc\\" />
|
||||||
</head>
|
</head>
|
||||||
<script>
|
<script>
|
||||||
window.location.href = '/abc/';
|
window.location.href = '/abc';
|
||||||
</script>
|
</script>
|
||||||
</html>",
|
</html>",
|
||||||
]
|
]
|
||||||
|
@ -38,22 +38,22 @@ Array [
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset=\\"UTF-8\\">
|
<meta charset=\\"UTF-8\\">
|
||||||
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/abc/\\">
|
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/abc\\">
|
||||||
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/abc/\\" />
|
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/abc\\" />
|
||||||
</head>
|
</head>
|
||||||
<script>
|
<script>
|
||||||
window.location.href = 'https://docusaurus.io/abc/';
|
window.location.href = 'https://docusaurus.io/abc';
|
||||||
</script>
|
</script>
|
||||||
</html>",
|
</html>",
|
||||||
"<!DOCTYPE html>
|
"<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset=\\"UTF-8\\">
|
<meta charset=\\"UTF-8\\">
|
||||||
<meta http-equiv=\\"refresh\\" content=\\"0; url=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/\\" />
|
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/def.html\\" />
|
||||||
</head>
|
</head>
|
||||||
<script>
|
<script>
|
||||||
window.location.href = 'https://docusaurus.io/def.html/';
|
window.location.href = 'https://docusaurus.io/def.html';
|
||||||
</script>
|
</script>
|
||||||
</html>",
|
</html>",
|
||||||
"<!DOCTYPE html>
|
"<!DOCTYPE html>
|
||||||
|
|
|
@ -9,8 +9,38 @@ import path from 'path';
|
||||||
|
|
||||||
import writeRedirectFiles, {
|
import writeRedirectFiles, {
|
||||||
toRedirectFilesMetadata,
|
toRedirectFilesMetadata,
|
||||||
|
createToUrl,
|
||||||
} from '../writeRedirectFiles';
|
} 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', () => {
|
describe('toRedirectFilesMetadata', () => {
|
||||||
test('should create appropriate metadatas', async () => {
|
test('should create appropriate metadatas', async () => {
|
||||||
const pluginContext = {
|
const pluginContext = {
|
||||||
|
|
|
@ -11,11 +11,7 @@ import {memoize} from 'lodash';
|
||||||
|
|
||||||
import {PluginContext, RedirectMetadata} from './types';
|
import {PluginContext, RedirectMetadata} from './types';
|
||||||
import createRedirectPageContent from './createRedirectPageContent';
|
import createRedirectPageContent from './createRedirectPageContent';
|
||||||
import {
|
import {getFilePathForRoutePath, normalizeUrl} from '@docusaurus/utils';
|
||||||
addTrailingSlash,
|
|
||||||
getFilePathForRoutePath,
|
|
||||||
removeTrailingSlash,
|
|
||||||
} from '@docusaurus/utils';
|
|
||||||
|
|
||||||
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
|
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
|
||||||
|
|
||||||
|
@ -24,6 +20,10 @@ export type RedirectFileMetadata = {
|
||||||
fileContent: string;
|
fileContent: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function createToUrl(baseUrl: string, to: string) {
|
||||||
|
return normalizeUrl([baseUrl, to]);
|
||||||
|
}
|
||||||
|
|
||||||
export function toRedirectFilesMetadata(
|
export function toRedirectFilesMetadata(
|
||||||
redirects: RedirectMetadata[],
|
redirects: RedirectMetadata[],
|
||||||
pluginContext: WriteFilesPluginContext,
|
pluginContext: WriteFilesPluginContext,
|
||||||
|
@ -40,9 +40,7 @@ export function toRedirectFilesMetadata(
|
||||||
pluginContext.outDir,
|
pluginContext.outDir,
|
||||||
getFilePathForRoutePath(redirect.from),
|
getFilePathForRoutePath(redirect.from),
|
||||||
);
|
);
|
||||||
const toUrl = addTrailingSlash(
|
const toUrl = createToUrl(pluginContext.baseUrl, redirect.to);
|
||||||
`${removeTrailingSlash(pluginContext.baseUrl)}${path.join(redirect.to)}`,
|
|
||||||
);
|
|
||||||
const fileContent = createPageContentMemoized(toUrl);
|
const fileContent = createPageContentMemoized(toUrl);
|
||||||
return {
|
return {
|
||||||
...redirect,
|
...redirect,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue