rsnext/packages/next/shared/lib/router/utils/remove-path-prefix.ts
Javi Velasco 523704b83f
Execute middleware on Next.js internal requests (#37121)
* Do not exclude internal _next request in middleware

* Allow for `NextURL` to parse prefetch requests

* Add test for middleware data prefetch

* Refactor `hasBasePath` and `replaceBasePath`

* Refactor `removeTrailingSlash`

* Refactor parsed next url to use `getNextPathnameInfo`

* Allow to configure `NextURL`

* Ensure middleware rewrites with always with a locale

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-05-27 13:29:04 -05:00

16 lines
618 B
TypeScript

import { pathHasPrefix } from './path-has-prefix'
/**
* Given a path and a prefix it will remove the prefix when it exists in the
* given path. It ensures it matches exactly without containing extra chars
* and if the prefix is not there it will be noop.
* @param path The path to remove the prefix from.
* @param prefix The prefix to be removed.
*/
export function removePathPrefix(path: string, prefix: string): string {
if (pathHasPrefix(path, prefix)) {
const withoutPrefix = path.slice(prefix.length)
return withoutPrefix.startsWith('/') ? withoutPrefix : `/${withoutPrefix}`
}
return path
}