-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support Base URLs with trailing slash (fix #1258)
- Loading branch information
Showing
3 changed files
with
43 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,21 +62,30 @@ function isBaseAssets(base: string): boolean { | |
return base.startsWith('/') || base.startsWith('http://') || base.startsWith('https://') | ||
} | ||
|
||
function normalizeUrlPathname(urlOriginal: string, trailingSlash: boolean): string | null { | ||
function normalizeUrlPathname(urlOriginal: string, trailingSlash: boolean, baseServer: string): string | null { | ||
const urlNormalized = modifyUrlPathname(urlOriginal, (urlPathname) => { | ||
assert(urlPathname.startsWith('/')) | ||
let urlPathnameNormalized = '/' + urlPathname.split('/').filter(Boolean).join('/') | ||
if (urlPathnameNormalized !== '/') { | ||
assert(!urlPathnameNormalized.endsWith('/')) | ||
if (trailingSlash) { | ||
urlPathnameNormalized = urlPathnameNormalized + '/' | ||
} | ||
let urlPathnameNormalized = normalize(urlPathname) | ||
if (urlPathnameNormalized === '/') { | ||
return urlPathnameNormalized | ||
} | ||
// If the Base URL has a trailing slash, then Vite (as of [email protected]) expects the root URL to also have a trailing slash, see https://github.com/vikejs/vike/issues/1258#issuecomment-1812226260 | ||
if (baseServer.endsWith('/') && baseServer !== '/' && normalize(baseServer) === urlPathnameNormalized) { | ||
trailingSlash = true | ||
} | ||
assert(!urlPathnameNormalized.endsWith('/')) | ||
if (trailingSlash) { | ||
urlPathnameNormalized = urlPathnameNormalized + '/' | ||
} | ||
return urlPathnameNormalized | ||
}) | ||
if (urlNormalized === urlOriginal) return null | ||
return urlNormalized | ||
} | ||
function normalize(urlPathname: string): string { | ||
assert(urlPathname.startsWith('/')) | ||
return '/' + urlPathname.split('/').filter(Boolean).join('/') | ||
} | ||
|
||
function modifyUrlPathname(url: string, modifier: (urlPathname: string) => string | null): string { | ||
const { origin, pathnameOriginal, searchOriginal, hashOriginal } = parseUrl(url, '/') | ||
|