Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,11 @@ export async function renderToHTML(
}

// url will always be set
const routerIsReady = !!(getServerSideProps || hasPageGetInitialProps)
const routerIsReady = !!(
getServerSideProps ||
hasPageGetInitialProps ||
(!defaultAppGetInitialProps && !isSSG)
)
const router = new ServerRouter(
pathname,
query,
Expand Down
1 change: 1 addition & 0 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ export default class Router implements BaseRouter {
this.isReady = !!(
self.__NEXT_DATA__.gssp ||
self.__NEXT_DATA__.gip ||
(self.__NEXT_DATA__.appGip && !self.__NEXT_DATA__.gsp) ||
(!autoExportDynamic &&
!self.location.search &&
!process.env.__NEXT_HAS_REWRITES)
Expand Down
15 changes: 15 additions & 0 deletions test/integration/router-is-ready-app-gip/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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,
}
}
23 changes: 23 additions & 0 deletions test/integration/router-is-ready-app-gip/pages/appGip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useRouter } from 'next/router'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another page that uses getStaticProps to assert the behavior describe in https://github.com/vercel/next.js/pull/27473/files#r676169393 is correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some tests in 5cc4ff8. 🙇

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>
</>
)
}
32 changes: 32 additions & 0 deletions test/integration/router-is-ready-app-gip/pages/gsp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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(),
},
}
}
15 changes: 15 additions & 0 deletions test/integration/router-is-ready-app-gip/pages/invalid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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>
</>
)
}
68 changes: 68 additions & 0 deletions test/integration/router-is-ready-app-gip/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* 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()
})
})