-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Changes from 3 commits
a53ed77
ba6d8c4
c67f6ce
5cc4ff8
4edf424
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useRouter } from 'next/router' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add another page that uses There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
</> | ||
) | ||
} |
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> | ||
</> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* 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 with getInitialProps in _app', async () => { | ||
const browser = await webdriver(appPort, '/appGip') | ||
expect(await browser.eval('window.isReadyValues')).toEqual([true]) | ||
}) | ||
|
||
it('isReady should be true immediately for pages with getInitialProps in _app, with query', async () => { | ||
const browser = await webdriver(appPort, '/appGip?hello=world') | ||
expect(await browser.eval('window.isReadyValues')).toEqual([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() | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A page with
getStaticProps
isn't consideredready
until we're on the client and can determine if a query is present or not even withgetInitialProps
in_app
. See here for related query updating that makes thegetStaticProps
pageready
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I applied the change in 5cc4ff8.