Commit 04f1dd52 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Delay hydration until after page is visible in development (#10164)

* Delay hydration until after page is visible in development

* Tweak dead-code elimination
parent 6b87b2b5
export function displayContent() { export function displayContent(callback) {
// This is the fallback helper that removes Next.js' no-FOUC styles when // This is the fallback helper that removes Next.js' no-FOUC styles when
// CSS mode is enabled. This only really activates if you haven't created // CSS mode is enabled. This only really activates if you haven't created
// _any_ styles in your application yet. // _any_ styles in your application yet.
...@@ -10,5 +10,8 @@ export function displayContent() { ...@@ -10,5 +10,8 @@ export function displayContent() {
) { ) {
x[i].parentNode.removeChild(x[i]) x[i].parentNode.removeChild(x[i])
} }
if (callback) {
callback()
}
}) })
} }
...@@ -200,9 +200,15 @@ export default async ({ webpackHMR: passedWebpackHMR } = {}) => { ...@@ -200,9 +200,15 @@ export default async ({ webpackHMR: passedWebpackHMR } = {}) => {
} }
const renderCtx = { App, Component, props, err: initialErr } const renderCtx = { App, Component, props, err: initialErr }
render(renderCtx)
return emitter if (process.env.NODE_ENV === 'production') {
render(renderCtx)
return emitter
}
if (process.env.NODE_ENV !== 'production') {
return { emitter, render, renderCtx }
}
} }
export async function render(props) { export async function render(props) {
......
...@@ -28,7 +28,7 @@ const webpackHMR = initWebpackHMR({ assetPrefix: prefix }) ...@@ -28,7 +28,7 @@ const webpackHMR = initWebpackHMR({ assetPrefix: prefix })
window.next = next window.next = next
initNext({ webpackHMR }) initNext({ webpackHMR })
.then(emitter => { .then(({ emitter, renderCtx, render }) => {
initOnDemandEntries({ assetPrefix: prefix }) initOnDemandEntries({ assetPrefix: prefix })
if (process.env.__NEXT_BUILD_INDICATOR) initializeBuildWatcher() if (process.env.__NEXT_BUILD_INDICATOR) initializeBuildWatcher()
if ( if (
...@@ -39,7 +39,10 @@ initNext({ webpackHMR }) ...@@ -39,7 +39,10 @@ initNext({ webpackHMR })
initializePrerenderIndicator() initializePrerenderIndicator()
} }
displayContent() // delay rendering until after styles have been applied in development
displayContent(() => {
render(renderCtx)
})
let lastScroll let lastScroll
......
import React from 'react'
class App extends React.Component {
constructor() {
super()
this.divRef = React.createRef()
this.state = {
refHeight: 0,
}
}
componentDidMount() {
const refHeight = this.divRef.current.clientHeight
this.setState({ refHeight })
}
render() {
const { refHeight } = this.state
return (
<div ref={this.divRef}>
<h1>DOM Ref test using 9.2.0</h1>
<code id="ref-val">{`this component is ${refHeight}px tall`}</code>
</div>
)
}
}
export default App
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
launchApp,
findPort,
waitFor,
killApp,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
let app
let appPort
const runTest = () => {
it('Has correct initial ref values', async () => {
const browser = await webdriver(appPort, '/')
await waitFor(2000)
expect(await browser.elementByCss('#ref-val').text()).toContain('76px')
})
}
describe('Hydration', () => {
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTest()
})
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTest()
})
})
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