Unverified Commit 5a9a478c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix hydration with custom _app and granular chunks (#10144)

* 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
parent aa6991f2
...@@ -115,10 +115,19 @@ export function createEntrypoints( ...@@ -115,10 +115,19 @@ export function createEntrypoints(
} }
if (!isApiRoute) { if (!isApiRoute) {
client[bundlePath] = `next-client-pages-loader?${stringify({ const pageLoader = `next-client-pages-loader?${stringify({
page, page,
absolutePagePath, 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
} }
}) })
......
...@@ -7,7 +7,6 @@ import { ...@@ -7,7 +7,6 @@ import {
AppPropsType, AppPropsType,
} from '../next-server/lib/utils' } from '../next-server/lib/utils'
import { Router } from '../client/router' import { Router } from '../client/router'
import '../client/router'
export { AppInitialProps } export { AppInitialProps }
......
export default ({ Component, pageProps }) => <Component {...pageProps} />
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
export default () => {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
return 'details'
}
export default () => {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
return 'index'
}
/* 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)
})
})
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