Skip to content

Commit

Permalink
Ensure isReady is set correctly for auto static dynamic route (#20729)
Browse files Browse the repository at this point in the history
Follow-up to #20628 this ensures `isReady` is not initially true when the query isn't present but the page is an automatically statically optimized dynamic route
  • Loading branch information
ijjk authored Jan 4, 2021
1 parent 9ff3785 commit 88b6c47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,10 @@ export default class Router implements BaseRouter {
this.query = query
// if auto prerendered and dynamic route wait to update asPath
// until after mount to prevent hydration mismatch
this.asPath =
isDynamicRoute(pathname) && self.__NEXT_DATA__.autoExport ? pathname : as
const autoExportDynamic =
isDynamicRoute(pathname) && self.__NEXT_DATA__.autoExport

this.asPath = autoExportDynamic ? pathname : as
this.basePath = basePath
this.sub = subscription
this.clc = null
Expand All @@ -550,7 +552,7 @@ export default class Router implements BaseRouter {
this.isReady = !!(
self.__NEXT_DATA__.gssp ||
self.__NEXT_DATA__.gip ||
!self.location.search
(!autoExportDynamic && !self.location.search)
)

if (process.env.__NEXT_I18N_SUPPORT) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/build-output/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Build Output', () => {
expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
expect(err404Size.endsWith('kB')).toBe(true)

expect(parseFloat(err404FirstLoad)).toBeCloseTo(65.4, 1)
expect(parseFloat(err404FirstLoad)).toBeCloseTo(65.5, 1)
expect(err404FirstLoad.endsWith('kB')).toBe(true)

expect(parseFloat(sharedByAll)).toBeCloseTo(62, 1)
Expand Down
18 changes: 18 additions & 0 deletions test/integration/router-is-ready/pages/auto-export/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useRouter } from 'next/router'

export default function Page(props) {
const router = useRouter()

if (typeof window !== 'undefined') {
if (!window.isReadyValues) {
window.isReadyValues = []
}
window.isReadyValues.push(router.isReady)
}

return (
<>
<p id="auto-export">auto-export page</p>
</>
)
}
10 changes: 10 additions & 0 deletions test/integration/router-is-ready/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ function runTests(isDev) {
expect(await browser.eval('window.isReadyValues')).toEqual([false, true])
})

it('isReady should be true after query update for dynamic auto-export page without query', async () => {
const browser = await webdriver(appPort, '/auto-export/first')
expect(await browser.eval('window.isReadyValues')).toEqual([false, true])
})

it('isReady should be true after query update for dynamic auto-export page with query', async () => {
const browser = await webdriver(appPort, '/auto-export/first?hello=true')
expect(await browser.eval('window.isReadyValues')).toEqual([false, true])
})

it('isReady should be true after query update for getStaticProps page with query', async () => {
const browser = await webdriver(appPort, '/gsp?hello=world')
expect(await browser.eval('window.isReadyValues')).toEqual([false, true])
Expand Down

0 comments on commit 88b6c47

Please sign in to comment.