Skip to content

Commit

Permalink
Make sure to not override initial navigation when refreshing static p…
Browse files Browse the repository at this point in the history
…age's query (#10353)

* Add failing case

* Make sure to only refresh query if still on initial page

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
ijjk and Timer committed Feb 1, 2020
1 parent 9e6689e commit e63d822
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/next/client/index.js
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions 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 <p>redirecting..</p>
}
}

export default Page
20 changes: 19 additions & 1 deletion test/integration/dynamic-routing/test/index.test.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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('^\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$'),
Expand Down Expand Up @@ -507,7 +519,10 @@ describe('Dynamic Routing', () => {
})

describe('serverless mode', () => {
let origNextConfig

beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`
Expand All @@ -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()
})
})

0 comments on commit e63d822

Please sign in to comment.