diff --git a/packages/next/next-server/lib/router/router.ts b/packages/next/next-server/lib/router/router.ts index 9e495493ebf8cd..4b9cc78a14eff0 100644 --- a/packages/next/next-server/lib/router/router.ts +++ b/packages/next/next-server/lib/router/router.ts @@ -796,6 +796,8 @@ export default class Router implements BaseRouter { options: TransitionOptions, forcedScroll?: { x: number; y: number } ): Promise { + console.log('test2') + if (!isLocalURL(url)) { window.location.href = url return false @@ -977,6 +979,8 @@ export default class Router implements BaseRouter { ? removePathTrailingSlash(delBasePath(pathname)) : pathname + console.log({ shouldResolveHref }) + if (shouldResolveHref && pathname !== '/_error') { if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) { const rewritesResult = resolveRewrites( @@ -1022,6 +1026,8 @@ export default class Router implements BaseRouter { resolvedAs = delLocale(delBasePath(resolvedAs), this.locale) + console.log({ resolvedAs, as, route }, isDynamicRoute(route)) + if (isDynamicRoute(route)) { const parsedAs = parseRelativeUrl(resolvedAs) const asPathname = parsedAs.pathname diff --git a/test/integration/rewrite-with-browser-history/next.config.js b/test/integration/rewrite-with-browser-history/next.config.js new file mode 100644 index 00000000000000..3dc69434ef51e9 --- /dev/null +++ b/test/integration/rewrite-with-browser-history/next.config.js @@ -0,0 +1,10 @@ +module.exports = { + rewrites() { + return [ + { + source: '/:pagePrefix/:path*', + destination: '/dynamic-page/:path*', + }, + ] + }, +} diff --git a/test/integration/rewrite-with-browser-history/pages/dynamic-page/[[...param]].js b/test/integration/rewrite-with-browser-history/pages/dynamic-page/[[...param]].js new file mode 100644 index 00000000000000..47e56e79e024ff --- /dev/null +++ b/test/integration/rewrite-with-browser-history/pages/dynamic-page/[[...param]].js @@ -0,0 +1,23 @@ +import { useRouter } from 'next/router' +import Link from 'next/link' + +export default function Page(props) { + const router = useRouter() + + return ( + <> +

another page

+

{router.pathname}

+

{JSON.stringify(router.query)}

+ + + Go back to index + +
+ + ) +} + +export const getServerSideProps = () => { + return { props: {} } +} diff --git a/test/integration/rewrite-with-browser-history/pages/index.js b/test/integration/rewrite-with-browser-history/pages/index.js new file mode 100644 index 00000000000000..cc832a8d8563ad --- /dev/null +++ b/test/integration/rewrite-with-browser-history/pages/index.js @@ -0,0 +1,19 @@ +import { useRouter } from 'next/router' + +export default function Page() { + const router = useRouter() + + return ( + <> +

index page

+

{router.pathname}

+

{JSON.stringify(router.query)}

+ +
+ + ) +} + +export const getServerSideProps = () => { + return { props: {} } +} diff --git a/test/integration/rewrite-with-browser-history/test/index.test.js b/test/integration/rewrite-with-browser-history/test/index.test.js new file mode 100644 index 00000000000000..9f4ec6672d9a99 --- /dev/null +++ b/test/integration/rewrite-with-browser-history/test/index.test.js @@ -0,0 +1,58 @@ +/* eslint-env jest */ + +import { join } from 'path' +import { + findPort, + killApp, + launchApp, + nextBuild, + nextStart, +} from 'next-test-utils' +import webdriver from 'next-webdriver' + +jest.setTimeout(1000 * 60 * 2) + +const appDir = join(__dirname, '../') + +let appPort +let app + +const runTests = () => { + it('back-button should go back to rewritten path successfully', async () => { + const browser = await webdriver(appPort, '/rewrite-me') + + expect(await browser.elementByCss('#another').text()).toBe('another page') + + await browser + .elementByCss('#to-index') + .click() + .waitForElementByCss('#index') + + await browser.back() + + expect(await browser.elementByCss('#another').text()).toBe('another page') + }) +} + +describe('rewrites persist with browser history actions', () => { + describe('dev mode', () => { + beforeAll(async () => { + appPort = await findPort() + app = await launchApp(appDir, appPort) + }) + afterAll(() => killApp(app)) + + runTests() + }) + + describe('production mode', () => { + beforeAll(async () => { + await nextBuild(appDir) + appPort = await findPort() + app = await nextStart(appDir, appPort) + }) + afterAll(() => killApp(app)) + + runTests() + }) +})