Skip to content

Commit

Permalink
Prevent URL from being updated while asPath is delayed (#8756)
Browse files Browse the repository at this point in the history
* Prevent updating URL when delaying asPath

* Ignore TypeScript not liking __NEXT_DATA__

* Update another global to fix standard check
  • Loading branch information
ijjk authored and Timer committed Sep 16, 2019
1 parent 204028d commit 583b467
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
6 changes: 1 addition & 5 deletions packages/next/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ export default async ({ webpackHMR: passedWebpackHMR } = {}) => {
await window.__NEXT_PRELOADREADY(dynamicIds)
}

// if auto prerendered and dynamic route wait to update asPath
// until after mount to prevent hydration mismatch
const initialAsPath = isDynamicRoute(page) && data.nextExport ? page : asPath

router = createRouter(page, query, initialAsPath, {
router = createRouter(page, query, asPath, {
initialProps: props,
pageLoader,
App,
Expand Down
6 changes: 5 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ export default class Router implements BaseRouter {
this.pageLoader = pageLoader
this.pathname = pathname
this.query = query
this.asPath = as
// if auto prerendered and dynamic route wait to update asPath
// until after mount to prevent hydration mismatch
this.asPath =
// @ts-ignore this is temporarily global (attached to window)
isDynamicRoute(pathname) && __NEXT_DATA__.nextExport ? pathname : as
this.sub = subscription
this.clc = null
this._wrapApp = wrapApp
Expand Down
4 changes: 4 additions & 0 deletions test/integration/auto-export/pages/[post]/[cmnt].js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ if (typeof window !== 'undefined') {
window.caughtWarns.push(1)
origError(...args)
}
window.pathnames = []
}

export default () => {
if (typeof window !== 'undefined') {
window.pathnames.push(window.location.pathname)
}
return <p>{useRouter().asPath}</p>
}
23 changes: 16 additions & 7 deletions test/integration/auto-export/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ const runTests = () => {
expect(html).toMatch(/post.*post-1/)
expect(html).toMatch(/nextExport/)
})

it('should update asPath after mount', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-2')
await waitFor(500)
const html = await browser.eval(`document.documentElement.innerHTML`)
expect(html).toMatch(/\/zeit\/cmnt-2/)
})

it('should not replace URL with page name while asPath is delayed', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-1')
await waitFor(500)
const val = await browser.eval(`!!window.pathnames.find(function(p) {
return p !== '/zeit/cmnt-1'
})`)
expect(val).toBe(false)
})
}

describe('Auto Export', () => {
Expand Down Expand Up @@ -73,12 +89,5 @@ describe('Auto Export', () => {
const numCaught = await browser.eval(`window.caughtWarns.length`)
expect(numCaught).toBe(0)
})

it('should update asPath after mount', async () => {
const browser = await webdriver(appPort, '/zeit/cmnt-2')
await waitFor(500)
const html = await browser.eval(`document.documentElement.innerHTML`)
expect(html).toMatch(/\/zeit\/cmnt-2/)
})
})
})

0 comments on commit 583b467

Please sign in to comment.