fix(v2): redirect from should work with trailingSlash: true (#5093)

This commit is contained in:
Sébastien Lorber 2021-06-30 10:44:03 +02:00 committed by GitHub
parent a78e4f19b2
commit e5916dc596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 19 deletions

View File

@ -111,7 +111,7 @@ describe('createFromExtensionsRedirects', () => {
`);
});
test('should create redirects to html/htm extensions', () => {
test('should create redirects from html/htm extensions', () => {
const ext = ['html', 'htm'];
expect(createFromExtensionsRedirects([''], ext)).toEqual([]);
expect(createFromExtensionsRedirects(['/'], ext)).toEqual([]);
@ -120,7 +120,10 @@ describe('createFromExtensionsRedirects', () => {
{from: '/abc.htm', to: '/abc'},
]);
expect(createFromExtensionsRedirects(['/def.html'], ext)).toEqual([]);
expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([]);
expect(createFromExtensionsRedirects(['/def/'], ext)).toEqual([
{from: '/def.html/', to: '/def/'},
{from: '/def.htm/', to: '/def/'},
]);
});
test('should create "from" redirects when relativeRoutesPath contains a prefix', () => {

View File

@ -6,7 +6,11 @@
*/
import {flatten} from 'lodash';
import {removeSuffix} from '@docusaurus/utils';
import {
addTrailingSlash,
removeSuffix,
removeTrailingSlash,
} from '@docusaurus/utils';
import {RedirectMetadata} from './types';
const ExtensionAdditionalMessage =
@ -61,7 +65,8 @@ export function createToExtensionsRedirects(
return flatten(paths.map(createPathRedirects));
}
// Create new /path.html that redirects to existing an /path
// Create new /path.html/index.html that redirects to existing an /path
// The filename pattern might look weird but it's on purpose (see https://github.com/facebook/docusaurus/issues/5055)
export function createFromExtensionsRedirects(
paths: string[],
extensions: string[],
@ -74,11 +79,22 @@ export function createFromExtensionsRedirects(
dottedExtensions.some((ext) => str.endsWith(ext));
const createPathRedirects = (path: string): RedirectMetadata[] => {
if (path === '' || path.endsWith('/') || alreadyEndsWithAnExtension(path)) {
if (path === '' || path === '/' || alreadyEndsWithAnExtension(path)) {
return [];
}
// /path => /path.html
// /path/ => /path.html/
function getFrom(ext: string) {
if (path.endsWith('/')) {
return addTrailingSlash(`${removeTrailingSlash(path)}.${ext}`);
} else {
return `${path}.${ext}`;
}
}
return extensions.map((ext) => ({
from: `${path}.${ext}`,
from: getFrom(ext),
to: path,
}));
};

View File

@ -15,7 +15,6 @@ import writeRedirectFiles, {
RedirectFileMetadata,
} from './writeRedirectFiles';
import {removePrefix, addLeadingSlash} from '@docusaurus/utils';
import chalk from 'chalk';
export default function pluginClientRedirectsPages(
context: LoadContext,
@ -25,18 +24,6 @@ export default function pluginClientRedirectsPages(
const options = normalizePluginOptions(opts);
// Special case, when using trailingSlash=false we output /xyz.html files instead of /xyz/index.html
// It makes no sense to use option fromExtensions=["html"]: the redirect files would be the same as the original files
if (options.fromExtensions.includes('html') && trailingSlash === false) {
console.warn(
chalk.yellow(`Using the Docusaurus redirect plugin with fromExtensions=['html'] and siteConfig.trailingSlash=false is prevented.
It would lead the redirect plugin to override all the page.html files created by Docusaurus.`),
);
options.fromExtensions = options.fromExtensions.filter(
(ext) => ext !== 'html',
);
}
return {
name: 'docusaurus-plugin-client-redirects',
async postBuild(props: Props) {