Unverified Commit 5476827f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure rewrites are resolved correctly through history (#25666)

* Ensure rewrites are resolved correctly through history

* update test
parent d5c2fe61
...@@ -804,7 +804,8 @@ export default class Router implements BaseRouter { ...@@ -804,7 +804,8 @@ export default class Router implements BaseRouter {
window.location.href = url window.location.href = url
return false return false
} }
const shouldResolveHref = url === as || (options as any)._h const shouldResolveHref =
url === as || (options as any)._h || (options as any)._shouldResolveHref
// for static pages with query params in the URL we delay // for static pages with query params in the URL we delay
// marking the router ready until after the query is updated // marking the router ready until after the query is updated
...@@ -982,6 +983,8 @@ export default class Router implements BaseRouter { ...@@ -982,6 +983,8 @@ export default class Router implements BaseRouter {
: pathname : pathname
if (shouldResolveHref && pathname !== '/_error') { if (shouldResolveHref && pathname !== '/_error') {
;(options as any)._shouldResolveHref = true
if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) { if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) {
const rewritesResult = resolveRewrites( const rewritesResult = resolveRewrites(
addBasePath(addLocale(cleanedAs, this.locale)), addBasePath(addLocale(cleanedAs, this.locale)),
......
...@@ -129,7 +129,7 @@ describe('Build Output', () => { ...@@ -129,7 +129,7 @@ describe('Build Output', () => {
expect(parseFloat(err404Size)).toBeCloseTo(gz ? 3.06 : 8.15, 1) expect(parseFloat(err404Size)).toBeCloseTo(gz ? 3.06 : 8.15, 1)
expect(err404Size.endsWith('kB')).toBe(true) expect(err404Size.endsWith('kB')).toBe(true)
expect(parseFloat(err404FirstLoad)).toBeCloseTo(gz ? 66.3 : 202, 1) expect(parseFloat(err404FirstLoad)).toBeCloseTo(gz ? 66.3 : 203, 1)
expect(err404FirstLoad.endsWith('kB')).toBe(true) expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll)).toBeCloseTo(gz ? 63.2 : 194, 1) expect(parseFloat(sharedByAll)).toBeCloseTo(gz ? 63.2 : 194, 1)
......
export default () => 'multi-rewrites' const Page = () => 'multi-rewrites'
export default Page
import Link from 'next/link' import Link from 'next/link'
export default () => ( const Page = () => (
<> <>
<h3 id="nav">Nav</h3> <h3 id="nav">Nav</h3>
<Link href="/hello" as="/first"> <Link href="/hello" as="/first">
...@@ -42,3 +42,4 @@ export default () => ( ...@@ -42,3 +42,4 @@ export default () => (
<br /> <br />
</> </>
) )
export default Page
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
normalizeRegEx, normalizeRegEx,
initNextServerScript, initNextServerScript,
nextExport, nextExport,
hasRedbox,
} from 'next-test-utils' } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2) jest.setTimeout(1000 * 60 * 2)
...@@ -38,6 +39,42 @@ let appPort ...@@ -38,6 +39,42 @@ let appPort
let app let app
const runTests = (isDev = false) => { const runTests = (isDev = false) => {
it('should resolveHref correctly navigating through history', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1')
expect(await browser.eval('document.documentElement.innerHTML')).toContain(
'multi-rewrites'
)
await browser.eval('next.router.push("/rewriting-to-auto-export")')
await browser.waitForElementByCss('#auto-export')
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'hello',
rewrite: '1',
})
expect(await browser.eval('window.beforeNav')).toBe(1)
await browser.eval('next.router.push("/nav")')
await browser.waitForElementByCss('#nav')
expect(await browser.elementByCss('#nav').text()).toBe('Nav')
await browser.back()
await browser.waitForElementByCss('#auto-export')
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: 'hello',
rewrite: '1',
})
expect(await browser.eval('window.beforeNav')).toBe(1)
if (isDev) {
expect(await hasRedbox(browser, false)).toBe(false)
}
})
it('should continue in beforeFiles rewrites', async () => { it('should continue in beforeFiles rewrites', async () => {
const res = await fetchViaHTTP(appPort, '/old-blog/about') const res = await fetchViaHTTP(appPort, '/old-blog/about')
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
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