rsnext/packages/next/next-server/lib/router/rewrite-url-for-export.ts
Jan Potoms 989b6ad145
Preserve url better in exportTrailingSlash (#13840)
Discovered while doing https://github.com/vercel/next.js/pull/13333. `#` or `?` used to be stripped if there wasn't a value behind
2020-06-07 17:15:06 +00:00

13 lines
459 B
TypeScript

export function rewriteUrlForNextExport(url: string): string {
const [pathname, hash] = url.split('#')
// tslint:disable-next-line
let [path, qs] = pathname.split('?')
if (path) {
path = path.replace(/\/$/, '')
// Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) path += `/`
}
if (typeof qs === 'string') path += '?' + qs
if (typeof hash === 'string') path += '#' + hash
return path
}