Skip to content

Commit

Permalink
Fix hydration with custom _app and granular chunks (#10144)
Browse files Browse the repository at this point in the history
* Add failing hydration test

* Add importing of next/router to _app

* Fix type

* Update _app check for windows

* Remove babel fix

* Update to use webpack to require next/router
  • Loading branch information
ijjk committed Jan 17, 2020
1 parent aa6991f commit 5a9a478
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
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)
})
})

0 comments on commit 5a9a478

Please sign in to comment.