Commit 2112e81a authored by Jan Potoms's avatar Jan Potoms Committed by Joe Haddad

Don't prepend a slash on empty path (#9973)

Fixes https://github.com/zeit/next.js/issues/9678Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent 58b2d9e2
...@@ -2,9 +2,11 @@ export function rewriteUrlForNextExport(url: string): string { ...@@ -2,9 +2,11 @@ export function rewriteUrlForNextExport(url: string): string {
const [pathname, hash] = url.split('#') const [pathname, hash] = url.split('#')
// tslint:disable-next-line // tslint:disable-next-line
let [path, qs] = pathname.split('?') let [path, qs] = pathname.split('?')
if (path) {
path = path.replace(/\/$/, '') path = path.replace(/\/$/, '')
// Append a trailing slash if this path does not have an extension // Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) path += `/` if (!/\.[^/]+\/?$/.test(path)) path += `/`
}
if (qs) path += '?' + qs if (qs) path += '?' + qs
if (hash) path += '#' + hash if (hash) path += '#' + hash
return path return path
......
...@@ -15,6 +15,7 @@ module.exports = phase => { ...@@ -15,6 +15,7 @@ module.exports = phase => {
'/': { page: '/' }, '/': { page: '/' },
'/about': { page: '/about' }, '/about': { page: '/about' },
'/button-link': { page: '/button-link' }, '/button-link': { page: '/button-link' },
'/hash-link': { page: '/hash-link' },
'/get-initial-props-with-no-query': { '/get-initial-props-with-no-query': {
page: '/get-initial-props-with-no-query', page: '/get-initial-props-with-no-query',
}, },
......
import Link from 'next/link'
export default () => (
<div id="hash-link-page">
<Link href="#hash">
<a id="hash-link">Hash link</a>
</Link>
</div>
)
...@@ -21,6 +21,13 @@ export default function(context) { ...@@ -21,6 +21,13 @@ export default function(context) {
expect(link.substr(link.length - 1)).toBe('/') expect(link.substr(link.length - 1)).toBe('/')
}) })
it('should not add any slash on hash Link', async () => {
const browser = await webdriver(context.port, '/hash-link')
const link = await browser.elementByCss('#hash-link').getAttribute('href')
expect(link).toMatch(/\/hash-link\/#hash$/)
})
it('should not add trailing slash on Link when disabled', async () => { it('should not add trailing slash on Link when disabled', async () => {
const browser = await webdriver(context.portNoTrailSlash, '/') const browser = await webdriver(context.portNoTrailSlash, '/')
const link = await browser const link = await browser
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment