fix(v2): never remove trailing slash from site root like '/baseUrl/' (#5082)

* never apply trailingSlash to site root ('/baseUrl/') => only subroutes

* add deprecation comment for loadContext.baseUrl in favor of loadContext.siteConfig.baseUrl

* commit typo

* useless code
This commit is contained in:
Sébastien Lorber 2021-06-29 15:17:23 +02:00 committed by GitHub
parent 41b78466da
commit 7592982960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 254 additions and 102 deletions

View file

@ -5,10 +5,20 @@
* LICENSE file in the root directory of this source tree.
*/
import type {DocusaurusConfig} from '@docusaurus/types';
export type ApplyTrailingSlashParams = Pick<
DocusaurusConfig,
'trailingSlash' | 'baseUrl'
>;
// Trailing slash handling depends in some site configuration options
export default function applyTrailingSlash(
path: string,
trailingSlash: boolean | undefined,
options: ApplyTrailingSlashParams,
): string {
const {trailingSlash, baseUrl} = options;
if (path.startsWith('#')) {
// Never apply trailing slash to an anchor link
return path;
@ -34,7 +44,14 @@ export default function applyTrailingSlash(
const [pathname] = path.split(/[#?]/);
// Never transform '/' to ''
const newPathname =
pathname === '/' ? '/' : handleTrailingSlash(pathname, trailingSlash);
// Never remove the baseUrl trailing slash!
// If baseUrl = /myBase/, we want to emit /myBase/index.html and not /myBase.html !
// See https://github.com/facebook/docusaurus/issues/5077
const shouldNotApply = pathname === '/' || pathname === baseUrl;
const newPathname = shouldNotApply
? pathname
: handleTrailingSlash(pathname, trailingSlash);
return path.replace(pathname, newPathname);
}