Skip to content

Commit

Permalink
fix(v2): redirect plugin: use siteConfig.trailingSlash (#4988)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Jun 16, 2021
1 parent 80b6d97 commit b54ec72
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-client-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@docusaurus/core": "2.0.0-beta.0",
"@docusaurus/types": "2.0.0-beta.0",
"@docusaurus/utils": "2.0.0-beta.0",
"@docusaurus/utils-common": "2.0.0-beta.0",
"@docusaurus/utils-validation": "2.0.0-beta.0",
"chalk": "^4.1.1",
"eta": "^1.11.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,83 @@ Array [
]
`;

exports[`toRedirectFilesMetadata should create appropriate metadatas: fileContent 1`] = `
exports[`toRedirectFilesMetadata should create appropriate metadatas trailingSlash=false: fileContent 1`] = `
Array [
"<!DOCTYPE html>
<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\\" />
</head>
<script>
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\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/def.html';
</script>
</html>",
"<!DOCTYPE html>
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/';
</script>
</html>",
]
`;

exports[`toRedirectFilesMetadata should create appropriate metadatas trailingSlash=true: fileContent 1`] = `
Array [
"<!DOCTYPE html>
<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\\" />
</head>
<script>
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\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/def.html';
</script>
</html>",
"<!DOCTYPE html>
<html>
<head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"refresh\\" content=\\"0; url=https://docusaurus.io/\\">
<link rel=\\"canonical\\" href=\\"https://docusaurus.io/\\" />
</head>
<script>
window.location.href = 'https://docusaurus.io/';
</script>
</html>",
]
`;

exports[`toRedirectFilesMetadata should create appropriate metadatas trailingSlash=undefined: fileContent 1`] = `
Array [
"<!DOCTYPE html>
<html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ function createTestPluginContext(
describe('collectRedirects', () => {
test('should collect no redirect for undefined config', () => {
expect(
collectRedirects(createTestPluginContext(undefined, ['/', '/path'])),
collectRedirects(
createTestPluginContext(undefined, ['/', '/path']),
undefined,
),
).toEqual([]);
});

test('should collect no redirect for empty config', () => {
expect(collectRedirects(createTestPluginContext({}))).toEqual([]);
expect(collectRedirects(createTestPluginContext({}), undefined)).toEqual(
[],
);
});

test('should collect redirects to html/exe extension', () => {
Expand All @@ -42,6 +47,7 @@ describe('collectRedirects', () => {
},
['/', '/somePath', '/otherPath.html'],
),
undefined,
),
).toEqual([
{
Expand All @@ -64,6 +70,7 @@ describe('collectRedirects', () => {
},
['/', '/somePath', '/otherPath.html'],
),
undefined,
),
).toEqual([
{
Expand Down Expand Up @@ -91,6 +98,79 @@ describe('collectRedirects', () => {
},
['/', '/somePath'],
),
undefined,
),
).toEqual([
{
from: '/someLegacyPath',
to: '/somePath',
},
{
from: '/someLegacyPathArray1',
to: '/',
},
{
from: '/someLegacyPathArray2',
to: '/',
},
]);
});

test('should collect redirects from plugin option redirects with trailingSlash=true', () => {
expect(
collectRedirects(
createTestPluginContext(
{
redirects: [
{
from: '/someLegacyPath',
to: '/somePath',
},
{
from: ['/someLegacyPathArray1', '/someLegacyPathArray2'],
to: '/',
},
],
},
['/', '/somePath/'],
),
true,
),
).toEqual([
{
from: '/someLegacyPath',
to: '/somePath/',
},
{
from: '/someLegacyPathArray1',
to: '/',
},
{
from: '/someLegacyPathArray2',
to: '/',
},
]);
});

test('should collect redirects from plugin option redirects with trailingSlash=false', () => {
expect(
collectRedirects(
createTestPluginContext(
{
redirects: [
{
from: '/someLegacyPath',
to: '/somePath/',
},
{
from: ['/someLegacyPathArray1', '/someLegacyPathArray2'],
to: '/',
},
],
},
['/', '/somePath'],
),
false,
),
).toEqual([
{
Expand Down Expand Up @@ -130,6 +210,7 @@ describe('collectRedirects', () => {
},
['/', '/someExistingPath', '/anotherExistingPath'],
),
undefined,
),
).toThrowErrorMatchingSnapshot();
});
Expand All @@ -148,6 +229,7 @@ describe('collectRedirects', () => {
},
['/', '/testpath', '/otherPath.html'],
),
undefined,
),
).toEqual([
{
Expand Down Expand Up @@ -197,6 +279,7 @@ describe('collectRedirects', () => {
},
['/'],
),
undefined,
),
).toThrowErrorMatchingSnapshot();
});
Expand All @@ -215,6 +298,7 @@ describe('collectRedirects', () => {
},
['/'],
),
undefined,
),
).toThrowErrorMatchingSnapshot();
});
Expand All @@ -236,6 +320,7 @@ describe('collectRedirects', () => {
'/toShouldWork',
],
),
undefined,
),
).toEqual([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('createToUrl', () => {
});

describe('toRedirectFilesMetadata', () => {
test('should create appropriate metadatas', async () => {
test('should create appropriate metadatas trailingSlash=undefined', async () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
Expand All @@ -55,10 +55,11 @@ describe('toRedirectFilesMetadata', () => {
{from: '/xyz', to: '/'},
],
pluginContext,
undefined,
);

expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
path.join(pluginContext.outDir, '/abc.html/index.html'),
path.join(pluginContext.outDir, '/abc.html'),
path.join(pluginContext.outDir, '/def/index.html'),
path.join(pluginContext.outDir, '/xyz/index.html'),
]);
Expand All @@ -68,6 +69,60 @@ describe('toRedirectFilesMetadata', () => {
);
});

test('should create appropriate metadatas trailingSlash=true', async () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
};

const redirectFiles = toRedirectFilesMetadata(
[
{from: '/abc.html', to: '/abc'},
{from: '/def', to: '/def.html'},
{from: '/xyz', to: '/'},
],
pluginContext,
true,
);

expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
path.join(pluginContext.outDir, '/abc.html'),
path.join(pluginContext.outDir, '/def/index.html'),
path.join(pluginContext.outDir, '/xyz/index.html'),
]);

expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent',
);
});

test('should create appropriate metadatas trailingSlash=false', async () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
};

const redirectFiles = toRedirectFilesMetadata(
[
{from: '/abc.html', to: '/abc'},
{from: '/def', to: '/def.html'},
{from: '/xyz', to: '/'},
],
pluginContext,
false,
);

expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
path.join(pluginContext.outDir, '/abc.html'),
path.join(pluginContext.outDir, '/def.html'),
path.join(pluginContext.outDir, '/xyz.html'),
]);

expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent',
);
});

test('should create appropriate metadatas for root baseUrl', async () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
Expand All @@ -76,6 +131,7 @@ describe('toRedirectFilesMetadata', () => {
const redirectFiles = toRedirectFilesMetadata(
[{from: '/abc.html', to: '/abc'}],
pluginContext,
undefined,
);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent baseUrl=/',
Expand All @@ -90,6 +146,7 @@ describe('toRedirectFilesMetadata', () => {
const redirectFiles = toRedirectFilesMetadata(
[{from: '/abc.html', to: '/abc'}],
pluginContext,
undefined,
);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent baseUrl=empty',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@ import {
createToExtensionsRedirects,
} from './extensionRedirects';
import {validateRedirect} from './redirectValidation';
import {applyTrailingSlash} from '@docusaurus/utils-common';

import chalk from 'chalk';

export default function collectRedirects(
pluginContext: PluginContext,
trailingSlash: boolean | undefined,
): RedirectMetadata[] {
const redirects = doCollectRedirects(pluginContext);
let redirects = doCollectRedirects(pluginContext);
redirects = applyRedirectsTrailingSlash(redirects, trailingSlash);
validateCollectedRedirects(redirects, pluginContext);
return filterUnwantedRedirects(redirects, pluginContext);
}

// If users wants to redirect to=/abc and they enable trailingSlash=true then
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid path now)
// => we want to redirect to=/abc/ without the user having to change all its redirect plugin options
// It should be easy to toggle siteConfig.trailingSlash option without having to change other configs
function applyRedirectsTrailingSlash(
redirects: RedirectMetadata[],
trailingSlash: boolean | undefined,
) {
return redirects.map((redirect) => {
return {
...redirect,
to: applyTrailingSlash(redirect.to, trailingSlash),
};
});
}

function validateCollectedRedirects(
redirects: RedirectMetadata[],
pluginContext: PluginContext,
Expand Down
Loading

0 comments on commit b54ec72

Please sign in to comment.