fix(v2): redirect plugin should emit redirect files with lower precedence than redirect target (#5085)

* revert old behavior of the redirect plugin

* revert old behavior of the redirect plugin
This commit is contained in:
Sébastien Lorber 2021-06-29 19:34:55 +02:00 committed by GitHub
parent 7fe2a9891d
commit a78e4f19b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 139 deletions

View file

@ -1,87 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {getFilePathForRoutePath} from '../getFilePathForRoutePath';
import {posixPath} from '../posixPath';
describe('getFilePathForRoutePath trailingSlash=undefined', () => {
test('works for /', () => {
expect(posixPath(getFilePathForRoutePath('/', undefined))).toEqual(
'/index.html',
);
});
test('works for /somePath', () => {
expect(posixPath(getFilePathForRoutePath('/somePath', undefined))).toEqual(
'/somePath/index.html',
);
});
test('works for /somePath/', () => {
expect(posixPath(getFilePathForRoutePath('/somePath/', undefined))).toEqual(
'/somePath/index.html',
);
});
test('works for /somePath/xyz.html', () => {
expect(
posixPath(getFilePathForRoutePath('/somePath/xyz.html', undefined)),
).toEqual('/somePath/xyz.html');
});
});
describe('getFilePathForRoutePath trailingSlash=true', () => {
test('works for /', () => {
expect(posixPath(getFilePathForRoutePath('/', true))).toEqual(
'/index.html',
);
});
test('works for /somePath', () => {
expect(posixPath(getFilePathForRoutePath('/somePath', true))).toEqual(
'/somePath/index.html',
);
});
test('works for /somePath/', () => {
expect(posixPath(getFilePathForRoutePath('/somePath/', true))).toEqual(
'/somePath/index.html',
);
});
test('works for /somePath/xyz.html', () => {
expect(
posixPath(getFilePathForRoutePath('/somePath/xyz.html', true)),
).toEqual('/somePath/xyz.html');
});
});
describe('getFilePathForRoutePath trailingSlash=false', () => {
test('works for /', () => {
expect(posixPath(getFilePathForRoutePath('/', false))).toEqual(
'/index.html',
);
});
test('works for /somePath', () => {
expect(posixPath(getFilePathForRoutePath('/somePath', false))).toEqual(
'/somePath.html',
);
});
test('works for /somePath/', () => {
expect(posixPath(getFilePathForRoutePath('/somePath/', false))).toEqual(
'/somePath/index.html',
);
});
test('works for /somePath/xyz.html', () => {
expect(
posixPath(getFilePathForRoutePath('/somePath/xyz.html', false)),
).toEqual('/somePath/xyz.html');
});
});

View file

@ -1,43 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
/*
export function getFilePathForRoutePath(routePath: string): string {
const fileName = path.basename(routePath);
const filePath = path.dirname(routePath);
return path.join(filePath, `${fileName}/index.html`);
}
*/
// Almost exact copy of the behavior we implemented in our Docusaurus fork of the webpack static gen plugin
// See https://github.com/slorber/static-site-generator-webpack-plugin/blob/master/index.js#L167
export function getFilePathForRoutePath(
routePath: string,
trailingSlash: boolean | undefined,
): string {
// const outputFileName = routePath.replace(/^(\/|\\)/, ''); // Remove leading slashes for webpack-dev-server
// Paths ending with .html are left untouched
if (/\.(html?)$/i.test(routePath)) {
return routePath;
}
// Legacy retro-compatible behavior
if (typeof trailingSlash === 'undefined') {
return path.join(routePath, 'index.html');
}
// New behavior: we can say if we prefer file/folder output
// Useful resource: https://github.com/slorber/trailing-slash-guide
if (routePath === '' || routePath.endsWith('/') || trailingSlash) {
return path.join(routePath, 'index.html');
} else {
return `${routePath}.html`;
}
}

View file

@ -26,7 +26,6 @@ import {simpleHash, docuHash} from './hashUtils';
export const posixPath = posixPathImport;
export * from './getFilePathForRoutePath';
export * from './codeTranslationsUtils';
export * from './markdownParser';
export * from './markdownLinks';