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 hydration with custom _app and granular chunks #10144

Merged
merged 7 commits into from Jan 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/next/build/entries.ts
Expand Up @@ -115,10 +115,19 @@ export function createEntrypoints(
}

if (!isApiRoute) {
client[bundlePath] = `next-client-pages-loader?${stringify({
const pageLoader = `next-client-pages-loader?${stringify({
page,
absolutePagePath,
})}!`

// Make sure next/router is a dependency of _app or else granularChunks
// might cause the router to not be able to load causing hydration
// to fail

client[bundlePath] =
page === '/_app'
? [pageLoader, require.resolve('../client/router')]
: pageLoader
}
})

Expand Down
1 change: 0 additions & 1 deletion packages/next/pages/_app.tsx
Expand Up @@ -7,7 +7,6 @@ import {
AppPropsType,
} from '../next-server/lib/utils'
import { Router } from '../client/router'
import '../client/router'

export { AppInitialProps }

Expand Down
1 change: 1 addition & 0 deletions test/integration/hydration/pages/_app.js
@@ -0,0 +1 @@
export default ({ Component, pageProps }) => <Component {...pageProps} />
16 changes: 16 additions & 0 deletions test/integration/hydration/pages/_document.js
@@ -0,0 +1,16 @@
import Document, { Head, Html, Main, NextScript } from 'next/document'
import React from 'react'

class WeddingDocument extends Document {
render() {
return (
<Html lang="en-GB">
<Head />
<Main />
<NextScript />
</Html>
)
}
}

export default WeddingDocument
6 changes: 6 additions & 0 deletions test/integration/hydration/pages/details.js
@@ -0,0 +1,6 @@
export default () => {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
return 'details'
}
6 changes: 6 additions & 0 deletions test/integration/hydration/pages/index.js
@@ -0,0 +1,6 @@
export default () => {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
return 'index'
}
33 changes: 33 additions & 0 deletions test/integration/hydration/test/index.test.js
@@ -0,0 +1,33 @@
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
findPort,
waitFor,
killApp,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
let app
let appPort

describe('Hydration', () => {
beforeAll(async () => {
await fs.remove(path.join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

it('Hydrates correctly', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000)
expect(await browser.eval('window.didHydrate')).toBe(true)
})
})