mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 14:06:59 +02:00
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:
parent
41b78466da
commit
7592982960
11 changed files with 254 additions and 102 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue