未验证 提交 989b6ad1 编写于 作者: J Jan Potoms 提交者: GitHub

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
上级 e125d905
......@@ -7,7 +7,7 @@ export function rewriteUrlForNextExport(url: string): string {
// Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) path += `/`
}
if (qs) path += '?' + qs
if (hash) path += '#' + hash
if (typeof qs === 'string') path += '?' + qs
if (typeof hash === 'string') path += '#' + hash
return path
}
......@@ -17,6 +17,8 @@ module.exports = (phase) => {
'/about': { page: '/about' },
'/button-link': { page: '/button-link' },
'/hash-link': { page: '/hash-link' },
'/empty-hash-link': { page: '/empty-hash-link' },
'/empty-query-link': { page: '/empty-query-link' },
'/get-initial-props-with-no-query': {
page: '/get-initial-props-with-no-query',
},
......
import Link from 'next/link'
export default () => (
<div id="empty-hash-link-page">
<Link href="/hello#">
<a id="empty-hash-link">Empty Hash link</a>
</Link>
</div>
)
import Link from 'next/link'
export default () => (
<div id="empty-query-link-page">
<Link href="/hello?">
<a id="empty-query-link">Empty Query link</a>
</Link>
</div>
)
......@@ -28,6 +28,24 @@ export default function (context) {
expect(link).toMatch(/\/hash-link\/#hash$/)
})
it('should preserve hash symbol on empty hash Link', async () => {
const browser = await webdriver(context.port, '/empty-hash-link')
const link = await browser
.elementByCss('#empty-hash-link')
.getAttribute('href')
expect(link).toMatch(/\/hello\/#$/)
})
it('should preserve question mark on empty query Link', async () => {
const browser = await webdriver(context.port, '/empty-query-link')
const link = await browser
.elementByCss('#empty-query-link')
.getAttribute('href')
expect(link).toMatch(/\/hello\/\?$/)
})
it('should not add trailing slash on Link when disabled', async () => {
const browser = await webdriver(context.portNoTrailSlash, '/')
const link = await browser
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册