6da769129e
Moves two utility functions from `server/router.ts` into their own file. This avoids the middleware pulling in the full Next.js router into its bundle. There are probably more opportunities like this, but this is a good start. Middleware should likely be bundled by a non-chunking optimizing compiler.
17 lines
628 B
TypeScript
17 lines
628 B
TypeScript
export function replaceBasePath(pathname: string, basePath: string): string {
|
|
// ensure basePath is only stripped if it matches exactly
|
|
// and doesn't contain extra chars e.g. basePath /docs
|
|
// should replace for /docs, /docs/, /docs/a but not /docsss
|
|
if (hasBasePath(pathname, basePath)) {
|
|
pathname = pathname.substr(basePath.length)
|
|
if (!pathname.startsWith('/')) pathname = `/${pathname}`
|
|
}
|
|
return pathname
|
|
}
|
|
|
|
export function hasBasePath(pathname: string, basePath: string): boolean {
|
|
return (
|
|
typeof pathname === 'string' &&
|
|
(pathname === basePath || pathname.startsWith(basePath + '/'))
|
|
)
|
|
}
|