rsnext/packages/next/next-server/lib/router/rewrite-url-for-export.ts
Jan Potoms 2112e81ae4 Don't prepend a slash on empty path (#9973)
Fixes https://github.com/zeit/next.js/issues/9678

Co-authored-by: Joe Haddad <timer150@gmail.com>
2020-01-08 11:45:17 -05:00

13 lines
419 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 (qs) path += '?' + qs
if (hash) path += '#' + hash
return path
}