Unverified Commit 23ac4351 authored by Sojin Park's avatar Sojin Park Committed by GitHub

fix: Make router ready in case of custom _app getInitialProps (#27473)

From https://github.com/vercel/next.js/pull/20628,  when the page is rendered server-side, `Router`'s `isReady` field needs to be initially set to `true`. However, when `_app` has custom `getInitialProps`, it seems that it is not the case, even though the page is rendered on the server. 

This leads to a bug that `Router.isReady` is never set to `true`.

This pull request fixes the problem by fixing the initial calculation logic of `isReady` of `Router`.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes
parent 7c0f707b
...@@ -571,7 +571,11 @@ export async function renderToHTML( ...@@ -571,7 +571,11 @@ export async function renderToHTML(
} }
// url will always be set // url will always be set
const routerIsReady = !!(getServerSideProps || hasPageGetInitialProps) const routerIsReady = !!(
getServerSideProps ||
hasPageGetInitialProps ||
(!defaultAppGetInitialProps && !isSSG)
)
const router = new ServerRouter( const router = new ServerRouter(
pathname, pathname,
query, query,
......
...@@ -627,6 +627,7 @@ export default class Router implements BaseRouter { ...@@ -627,6 +627,7 @@ export default class Router implements BaseRouter {
this.isReady = !!( this.isReady = !!(
self.__NEXT_DATA__.gssp || self.__NEXT_DATA__.gssp ||
self.__NEXT_DATA__.gip || self.__NEXT_DATA__.gip ||
(self.__NEXT_DATA__.appGip && !self.__NEXT_DATA__.gsp) ||
(!autoExportDynamic && (!autoExportDynamic &&
!self.location.search && !self.location.search &&
!process.env.__NEXT_HAS_REWRITES) !process.env.__NEXT_HAS_REWRITES)
......
export default function TestApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
TestApp.getInitialProps = async ({ Component, ctx }) => {
let pageProps
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return {
pageProps,
}
}
import { useRouter } from 'next/router'
import { useLayoutEffect } from 'react'
export default function Page(props) {
const router = useRouter()
if (typeof window !== 'undefined') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
if (!window.isReadyValues) {
window.isReadyValues = []
}
window.isReadyValues.push(router.isReady)
}, [router])
}
return (
<>
<p id="appGip">appGip page</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
import { useRouter } from 'next/router'
import { useLayoutEffect } from 'react'
export default function Page(props) {
const router = useRouter()
if (typeof window !== 'undefined') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
if (!window.isReadyValues) {
window.isReadyValues = []
}
window.isReadyValues.push(router.isReady)
}, [router])
}
return (
<>
<p id="gsp">gsp page</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
export const getStaticProps = () => {
return {
props: {
hello: 'world',
random: Math.random(),
},
}
}
import { useRouter } from 'next/router'
export default function Page(props) {
// eslint-disable-next-line
const router = useRouter()
// console.log(router.isReady)
return (
<>
<p id="invalid">invalid page</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
/* eslint-env jest */
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextStart,
nextBuild,
File,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 1)
let app
let appPort
const appDir = join(__dirname, '../')
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
function runTests(isDev) {
it('isReady should be true immediately for pages without getStaticProps', async () => {
const browser = await webdriver(appPort, '/appGip')
expect(await browser.eval('window.isReadyValues')).toEqual([true])
})
it('isReady should be true immediately for pages without getStaticProps, with query', async () => {
const browser = await webdriver(appPort, '/appGip?hello=world')
expect(await browser.eval('window.isReadyValues')).toEqual([true])
})
it('isReady should be true immediately for getStaticProps page without query', async () => {
const browser = await webdriver(appPort, '/gsp')
expect(await browser.eval('window.isReadyValues')).toEqual([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])
})
}
describe('router.isReady with appGip', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
invalidPage.restore()
})
runTests(true)
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment