Unverified Commit 9174b7a3 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix component lifecycle order for style swap (#19125)

Co-authored-by: 's avatarGerald Monaco <gbmonaco@google.com>
parent f108dbf2
......@@ -661,7 +661,7 @@ function doRender(input: RenderRouteInfo): Promise<any> {
return true
}
function onCommit() {
function onHeadCommit() {
if (
// We use `style-loader` in development, so we don't need to do anything
// unless we're in production:
......@@ -723,12 +723,15 @@ function doRender(input: RenderRouteInfo): Promise<any> {
// unstyled content:
getComputedStyle(document.body, 'height')
}
}
function onRootCommit() {
resolvePromise()
}
const elem = (
<Root callback={onCommit}>
<Root callback={onRootCommit}>
<Head callback={onHeadCommit} />
<AppContainer>
<App {...appProps} />
</AppContainer>
......@@ -776,3 +779,12 @@ function Root({
}, [])
return children as React.ReactElement
}
// Dummy component that we render as a child of Root so that we can
// toggle the correct styles before the page is rendered.
function Head({ callback }: { callback: () => void }) {
// We use `useLayoutEffect` to guarantee the callback is executed
// as soon as React flushes the update.
React.useLayoutEffect(() => callback(), [callback])
return null
}
import Link from 'next/link'
export default function Home() {
return (
<main>
<Link href="/other" prefetch={false}>
<a id="link-other">other</a>
</Link>
</main>
)
}
import React from 'react'
import Link from 'next/link'
import css from './other.module.css'
export default class Other extends React.Component {
ref = React.createRef()
constructor(props) {
super(props)
this.state = {
color: null,
}
}
componentDidMount() {
this.setState({
color: window.getComputedStyle(this.ref.current).color,
})
}
render() {
return (
<main>
<Link href="/" prefetch={false}>
<a id="link-index">index page</a>
</Link>
<br />
<h1 id="red-title" className={css.root} ref={this.ref}>
{this.state.color}
</h1>
</main>
)
}
}
......@@ -245,6 +245,82 @@ describe('CSS Support', () => {
})
})
describe('React Lifecyce Order (dev)', () => {
const appDir = join(fixturesDir, 'transition-react')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
let appPort
let app
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
it('should have the correct color on mount after navigation', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
// Navigate to other:
await browser.waitForElementByCss('#link-other').click()
const text = await browser.waitForElementByCss('#red-title').text()
expect(text).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('React Lifecyce Order (production)', () => {
const appDir = join(fixturesDir, 'transition-react')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
let appPort
let app
let code
let stdout
beforeAll(async () => {
;({ code, stdout } = await nextBuild(appDir, [], {
stdout: true,
}))
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
it('should have compiled successfully', () => {
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
})
it('should have the correct color on mount after navigation', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
// Navigate to other:
await browser.waitForElementByCss('#link-other').click()
const text = await browser.waitForElementByCss('#red-title').text()
expect(text).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('Invalid CSS in _document', () => {
const appDir = join(fixturesDir, 'invalid-module-document')
......
......@@ -80,7 +80,7 @@ describe('Production response size', () => {
)
// These numbers are without gzip compression!
const delta = responseSizesBytes - 282 * 1024
const delta = responseSizesBytes - 283 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
})
......
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