Unverified Commit dcdcf49a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix resolving href with a rewrite (#16975)

This makes sure we properly resolve a rewrite when only the `href` value is used. This was causing a full-reload and was missed in the existing test since we weren't making sure a full navigation didn't occur which has been added in this PR. 

Fixes: https://github.com/vercel/next.js/issues/16974
parent 72b78f3b
...@@ -609,7 +609,7 @@ export default class Router implements BaseRouter { ...@@ -609,7 +609,7 @@ export default class Router implements BaseRouter {
method = 'replaceState' method = 'replaceState'
} }
const route = removePathTrailingSlash(pathname) let route = removePathTrailingSlash(pathname)
const { shallow = false } = options const { shallow = false } = options
// we need to resolve the as value using rewrites for dynamic SSG // we need to resolve the as value using rewrites for dynamic SSG
...@@ -625,6 +625,25 @@ export default class Router implements BaseRouter { ...@@ -625,6 +625,25 @@ export default class Router implements BaseRouter {
query, query,
(p: string) => this._resolveHref({ pathname: p }, pages).pathname! (p: string) => this._resolveHref({ pathname: p }, pages).pathname!
) )
if (resolvedAs !== as) {
const potentialHref = removePathTrailingSlash(
this._resolveHref(
Object.assign({}, parsed, { pathname: resolvedAs }),
pages,
false
).pathname!
)
// if this directly matches a page we need to update the href to
// allow the correct page chunk to be loaded
if (pages.includes(potentialHref)) {
route = potentialHref
pathname = potentialHref
parsed.pathname = pathname
url = formatWithValidation(parsed)
}
}
} }
resolvedAs = delBasePath(resolvedAs) resolvedAs = delBasePath(resolvedAs)
...@@ -980,10 +999,10 @@ export default class Router implements BaseRouter { ...@@ -980,10 +999,10 @@ export default class Router implements BaseRouter {
return this.asPath !== asPath return this.asPath !== asPath
} }
_resolveHref(parsedHref: UrlObject, pages: string[]) { _resolveHref(parsedHref: UrlObject, pages: string[], applyBasePath = true) {
const { pathname } = parsedHref const { pathname } = parsedHref
const cleanPathname = removePathTrailingSlash( const cleanPathname = removePathTrailingSlash(
denormalizePagePath(delBasePath(pathname!)) denormalizePagePath(applyBasePath ? delBasePath(pathname!) : pathname!)
) )
if (cleanPathname === '/404' || cleanPathname === '/_error') { if (cleanPathname === '/404' || cleanPathname === '/_error') {
...@@ -998,7 +1017,7 @@ export default class Router implements BaseRouter { ...@@ -998,7 +1017,7 @@ export default class Router implements BaseRouter {
isDynamicRoute(page) && isDynamicRoute(page) &&
getRouteRegex(page).re.test(cleanPathname!) getRouteRegex(page).re.test(cleanPathname!)
) { ) {
parsedHref.pathname = addBasePath(page) parsedHref.pathname = applyBasePath ? addBasePath(page) : page
return true return true
} }
}) })
......
...@@ -2,6 +2,7 @@ import { ParsedUrlQuery } from 'querystring' ...@@ -2,6 +2,7 @@ import { ParsedUrlQuery } from 'querystring'
import pathMatch from './path-match' import pathMatch from './path-match'
import prepareDestination from './prepare-destination' import prepareDestination from './prepare-destination'
import { Rewrite } from '../../../../lib/load-custom-routes' import { Rewrite } from '../../../../lib/load-custom-routes'
import { removePathTrailingSlash } from '../../../../client/normalize-trailing-slash'
const customRouteMatcher = pathMatch(true) const customRouteMatcher = pathMatch(true)
...@@ -33,7 +34,7 @@ export default function resolveRewrites( ...@@ -33,7 +34,7 @@ export default function resolveRewrites(
asPath = destRes.parsedDestination.pathname! asPath = destRes.parsedDestination.pathname!
Object.assign(query, destRes.parsedDestination.query) Object.assign(query, destRes.parsedDestination.query)
if (pages.includes(asPath)) { if (pages.includes(removePathTrailingSlash(asPath))) {
// check if we now match a page as this means we are done // check if we now match a page as this means we are done
// resolving the rewrites // resolving the rewrites
break break
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export default function Page() { export default function Page() {
return `auto-export ${useRouter().query.slug}` return <p id="auto-export">auto-export {useRouter().query.slug}</p>
} }
...@@ -28,5 +28,9 @@ export default () => ( ...@@ -28,5 +28,9 @@ export default () => (
<a id="to-params">to params</a> <a id="to-params">to params</a>
</Link> </Link>
<br /> <br />
<Link href="/rewriting-to-auto-export">
<a id="to-rewritten-dynamic">to rewritten dynamic</a>
</Link>
<br />
</> </>
) )
...@@ -316,11 +316,13 @@ const runTests = (isDev = false) => { ...@@ -316,11 +316,13 @@ const runTests = (isDev = false) => {
it('should work with rewrite when manually specifying href/as', async () => { it('should work with rewrite when manually specifying href/as', async () => {
const browser = await webdriver(appPort, '/nav') const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser await browser
.elementByCss('#to-params-manual') .elementByCss('#to-params-manual')
.click() .click()
.waitForElementByCss('#query') .waitForElementByCss('#query')
expect(await browser.eval('window.beforeNav')).toBe(1)
const query = JSON.parse(await browser.elementByCss('#query').text()) const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({ expect(query).toEqual({
something: '1', something: '1',
...@@ -330,11 +332,13 @@ const runTests = (isDev = false) => { ...@@ -330,11 +332,13 @@ const runTests = (isDev = false) => {
it('should work with rewrite when only specifying href', async () => { it('should work with rewrite when only specifying href', async () => {
const browser = await webdriver(appPort, '/nav') const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser await browser
.elementByCss('#to-params') .elementByCss('#to-params')
.click() .click()
.waitForElementByCss('#query') .waitForElementByCss('#query')
expect(await browser.eval('window.beforeNav')).toBe(1)
const query = JSON.parse(await browser.elementByCss('#query').text()) const query = JSON.parse(await browser.elementByCss('#query').text())
expect(query).toEqual({ expect(query).toEqual({
something: '1', something: '1',
...@@ -342,6 +346,20 @@ const runTests = (isDev = false) => { ...@@ -342,6 +346,20 @@ const runTests = (isDev = false) => {
}) })
}) })
it('should work with rewrite when only specifying href and ends in dynamic route', async () => {
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-rewritten-dynamic')
.click()
.waitForElementByCss('#auto-export')
expect(await browser.eval('window.beforeNav')).toBe(1)
const text = await browser.eval(() => document.documentElement.innerHTML)
expect(text).toContain('auto-export hello')
})
it('should match a page after a rewrite', async () => { it('should match a page after a rewrite', async () => {
const html = await renderViaHTTP(appPort, '/to-hello') const html = await renderViaHTTP(appPort, '/to-hello')
expect(html).toContain('Hello') expect(html).toContain('Hello')
......
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