From e63d82216101b22047834c4d01fa449a302b6938 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 31 Jan 2020 19:19:59 -0600 Subject: [PATCH] Make sure to not override initial navigation when refreshing static page's query (#10353) * Add failing case * Make sure to only refresh query if still on initial page Co-authored-by: Joe Haddad --- packages/next/client/index.js | 5 +++-- .../pages/[name]/on-mount-redir.js | 14 +++++++++++++ .../dynamic-routing/test/index.test.js | 20 ++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/integration/dynamic-routing/pages/[name]/on-mount-redir.js diff --git a/packages/next/client/index.js b/packages/next/client/index.js index 61c75f7e6e90..60e64d0cdf7f 100644 --- a/packages/next/client/index.js +++ b/packages/next/client/index.js @@ -99,9 +99,10 @@ class Container extends React.Component { // If page was exported and has a querystring // If it's a dynamic route or has a querystring if ( - (data.nextExport && + router.isSsr && + ((data.nextExport && (isDynamicRoute(router.pathname) || location.search)) || - (Component && Component.__N_SSG && location.search) + (Component && Component.__N_SSG && location.search)) ) { // update query on mount for exported pages router.replace( diff --git a/test/integration/dynamic-routing/pages/[name]/on-mount-redir.js b/test/integration/dynamic-routing/pages/[name]/on-mount-redir.js new file mode 100644 index 000000000000..0194e310ce4c --- /dev/null +++ b/test/integration/dynamic-routing/pages/[name]/on-mount-redir.js @@ -0,0 +1,14 @@ +import React from 'react' +import Router from 'next/router' + +class Page extends React.Component { + componentDidMount() { + Router.push('/') + } + + render() { + return

redirecting..

+ } +} + +export default Page diff --git a/test/integration/dynamic-routing/test/index.test.js b/test/integration/dynamic-routing/test/index.test.js index 4edbcae89075..ec1d8ba48dd2 100644 --- a/test/integration/dynamic-routing/test/index.test.js +++ b/test/integration/dynamic-routing/test/index.test.js @@ -83,6 +83,12 @@ function runTests(dev) { } }) + it('should allow calling Router.push on mount successfully', async () => { + const browser = await webdriver(appPort, '/post-1/on-mount-redir') + waitFor(2000) + expect(await browser.elementByCss('h3').text()).toBe('My blog') + }) + // it('should navigate optional dynamic page', async () => { // let browser // try { @@ -456,6 +462,12 @@ function runTests(dev) { page: '/[name]/comments', regex: normalizeRegEx('^\\/([^\\/]+?)\\/comments(?:\\/)?$'), }, + { + page: '/[name]/on-mount-redir', + regex: normalizeRegEx( + '^\\/([^\\/]+?)\\/on\\-mount\\-redir(?:\\/)?$' + ), + }, { page: '/[name]/[comment]', regex: normalizeRegEx('^\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'), @@ -507,7 +519,10 @@ describe('Dynamic Routing', () => { }) describe('serverless mode', () => { + let origNextConfig + beforeAll(async () => { + origNextConfig = await fs.readFile(nextConfig, 'utf8') await fs.writeFile( nextConfig, ` @@ -526,7 +541,10 @@ describe('Dynamic Routing', () => { appPort = await findPort() app = await nextStart(appDir, appPort) }) - afterAll(() => killApp(app)) + afterAll(async () => { + await fs.writeFile(nextConfig, origNextConfig) + await killApp(app) + }) runTests() }) })