Unverified Commit b8a30bab authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Handle trailing slashes, even when query parameters (#14650)

Add tests and fix for when the url contains query parameters.
`router` now uses the same method for formatting url+as pair as `Link`, will be able to share code after https://github.com/vercel/next.js/pull/14633 is merged
parent 327f6eed
...@@ -38,24 +38,28 @@ function prepareRoute(path: string) { ...@@ -38,24 +38,28 @@ function prepareRoute(path: string) {
type Url = UrlObject | string type Url = UrlObject | string
function prepareUrlAs(url: Url, as: Url) { function formatTrailingSlash(url: UrlObject): UrlObject {
// If url and as provided as an object representation, return Object.assign({}, url, {
// we'll format them into the string version here. pathname:
url = typeof url === 'object' ? formatWithValidation(url) : url url.pathname &&
as = typeof as === 'object' ? formatWithValidation(as) : as normalizeTrailingSlash(url.pathname, !!process.env.__NEXT_TRAILING_SLASH),
})
}
url = addBasePath( function formatUrl(url: Url): string {
normalizeTrailingSlash(url, !!process.env.__NEXT_TRAILING_SLASH) return url
) ? formatWithValidation(
as = as formatTrailingSlash(typeof url === 'object' ? url : parse(url))
? addBasePath(
normalizeTrailingSlash(as, !!process.env.__NEXT_TRAILING_SLASH)
) )
: as : url
}
function prepareUrlAs(url: Url, as: Url) {
// If url and as provided as an object representation,
// we'll format them into the string version here.
return { return {
url, url: addBasePath(formatUrl(url)),
as, as: as ? addBasePath(formatUrl(as)) : as,
} }
} }
...@@ -436,7 +440,9 @@ export default class Router implements BaseRouter { ...@@ -436,7 +440,9 @@ export default class Router implements BaseRouter {
// url and as should always be prefixed with basePath by this // url and as should always be prefixed with basePath by this
// point by either next/link or router.push/replace so strip the // point by either next/link or router.push/replace so strip the
// basePath from the pathname to match the pages dir 1-to-1 // basePath from the pathname to match the pages dir 1-to-1
pathname = pathname ? delBasePath(pathname) : pathname pathname = pathname
? normalizeTrailingSlash(delBasePath(pathname), false)
: pathname
if (!pathname || protocol) { if (!pathname || protocol) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
......
...@@ -86,7 +86,8 @@ function testLinkShouldRewriteTo(expectations) { ...@@ -86,7 +86,8 @@ function testLinkShouldRewriteTo(expectations) {
await browser.elementByCss('#link').click() await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#hydration-marker') await browser.waitForElementByCss('#hydration-marker')
const { pathname } = new URL(await browser.eval('window.location.href')) const url = new URL(await browser.eval('window.location.href'))
const pathname = url.href.slice(url.origin.length)
expect(pathname).toBe(expectedHref) expect(pathname).toBe(expectedHref)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
...@@ -103,7 +104,8 @@ function testLinkShouldRewriteTo(expectations) { ...@@ -103,7 +104,8 @@ function testLinkShouldRewriteTo(expectations) {
await browser.elementByCss('#route-pusher').click() await browser.elementByCss('#route-pusher').click()
await browser.waitForElementByCss('#hydration-marker') await browser.waitForElementByCss('#hydration-marker')
const { pathname } = new URL(await browser.eval('window.location.href')) const url = new URL(await browser.eval('window.location.href'))
const pathname = url.href.slice(url.origin.length)
expect(pathname).toBe(expectedHref) expect(pathname).toBe(expectedHref)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
...@@ -112,7 +114,7 @@ function testLinkShouldRewriteTo(expectations) { ...@@ -112,7 +114,7 @@ function testLinkShouldRewriteTo(expectations) {
) )
} }
function testWithTrailingSlash() { function testWithoutTrailingSlash() {
testShouldRedirect([ testShouldRedirect([
['/about/', '/about'], ['/about/', '/about'],
['/catch-all/hello/world/', '/catch-all/hello/world'], ['/catch-all/hello/world/', '/catch-all/hello/world'],
...@@ -127,16 +129,19 @@ function testWithTrailingSlash() { ...@@ -127,16 +129,19 @@ function testWithTrailingSlash() {
'/catch-all/[...slug].js', '/catch-all/[...slug].js',
'/catch-all/[...slug]', '/catch-all/[...slug]',
], ],
['/about?hello=world', '/about.js', '/about'],
]) ])
testLinkShouldRewriteTo([ testLinkShouldRewriteTo([
['/', '/'], ['/', '/'],
['/about', '/about'], ['/about', '/about'],
['/about/', '/about'], ['/about/', '/about'],
['/about?hello=world', '/about?hello=world'],
['/about/?hello=world', '/about?hello=world'],
]) ])
} }
function testWithoutTrailingSlash() { function testWithTrailingSlash() {
testShouldRedirect([ testShouldRedirect([
['/about', '/about/'], ['/about', '/about/'],
['/catch-all/hello/world', '/catch-all/hello/world/'], ['/catch-all/hello/world', '/catch-all/hello/world/'],
...@@ -151,12 +156,15 @@ function testWithoutTrailingSlash() { ...@@ -151,12 +156,15 @@ function testWithoutTrailingSlash() {
'/catch-all/[...slug].js', '/catch-all/[...slug].js',
'/catch-all/[...slug]', '/catch-all/[...slug]',
], ],
['/about/?hello=world', '/about.js', '/about'],
]) ])
testLinkShouldRewriteTo([ testLinkShouldRewriteTo([
['/', '/'], ['/', '/'],
['/about', '/about/'], ['/about', '/about/'],
['/about/', '/about/'], ['/about/', '/about/'],
['/about?hello=world', '/about/?hello=world'],
['/about/?hello=world', '/about/?hello=world'],
]) ])
} }
...@@ -177,7 +185,7 @@ describe('Trailing slashes', () => { ...@@ -177,7 +185,7 @@ describe('Trailing slashes', () => {
await killApp(app) await killApp(app)
}) })
testWithTrailingSlash() testWithoutTrailingSlash()
}) })
describe('dev mode, trailingSlash: true', () => { describe('dev mode, trailingSlash: true', () => {
...@@ -196,7 +204,7 @@ describe('Trailing slashes', () => { ...@@ -196,7 +204,7 @@ describe('Trailing slashes', () => {
await killApp(app) await killApp(app)
}) })
testWithoutTrailingSlash() testWithTrailingSlash()
}) })
describe('production mode, trailingSlash: false', () => { describe('production mode, trailingSlash: false', () => {
...@@ -217,7 +225,7 @@ describe('Trailing slashes', () => { ...@@ -217,7 +225,7 @@ describe('Trailing slashes', () => {
await killApp(app) await killApp(app)
}) })
testWithTrailingSlash() testWithoutTrailingSlash()
it('should have a redirect in the routesmanifest', async () => { it('should have a redirect in the routesmanifest', async () => {
const manifest = await fs.readJSON( const manifest = await fs.readJSON(
...@@ -255,7 +263,7 @@ describe('Trailing slashes', () => { ...@@ -255,7 +263,7 @@ describe('Trailing slashes', () => {
await killApp(app) await killApp(app)
}) })
testWithoutTrailingSlash() testWithTrailingSlash()
it('should have a redirect in the routesmanifest', async () => { it('should have a redirect in the routesmanifest', async () => {
const manifest = await fs.readJSON( const manifest = await fs.readJSON(
......
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