Unverified Commit 0fa95da5 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Encapsulate routing and initial hydration (#33875)

* Encapsulate initial rendering

* Run dev code before rendering

* Fix call

* Try reverting last change, fixing call
parent 5365f417
// This function is used to remove Next.js' no-FOUC styles workaround for using // This function is used to remove Next.js' no-FOUC styles workaround for using
// `style-loader` in development. It must be called before hydration, or else // `style-loader` in development. It must be called before hydration, or else
// rendering won't have the correct computed values in effects. // rendering won't have the correct computed values in effects.
export function displayContent(callback) { export function displayContent(): Promise<void> {
return new Promise((resolve) => {
;(window.requestAnimationFrame || setTimeout)(function () { ;(window.requestAnimationFrame || setTimeout)(function () {
for ( for (
var x = document.querySelectorAll('[data-next-hide-fouc]'), i = x.length; var x = document.querySelectorAll('[data-next-hide-fouc]'),
i = x.length;
i--; i--;
) { ) {
x[i].parentNode.removeChild(x[i]) x[i].parentNode!.removeChild(x[i])
}
if (callback) {
callback()
} }
resolve()
})
}) })
} }
...@@ -271,7 +271,9 @@ class Container extends React.Component<{ ...@@ -271,7 +271,9 @@ class Container extends React.Component<{
export const emitter: MittEmitter<string> = mitt() export const emitter: MittEmitter<string> = mitt()
let CachedComponent: React.ComponentType let CachedComponent: React.ComponentType
export async function initNext(opts: { webpackHMR?: any } = {}) { export async function initNext(
opts: { webpackHMR?: any; beforeRender?: () => Promise<void> } = {}
) {
// This makes sure this specific lines are removed in production // This makes sure this specific lines are removed in production
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
webpackHMR = opts.webpackHMR webpackHMR = opts.webpackHMR
...@@ -425,12 +427,11 @@ export async function initNext(opts: { webpackHMR?: any } = {}) { ...@@ -425,12 +427,11 @@ export async function initNext(opts: { webpackHMR?: any } = {}) {
err: initialErr, err: initialErr,
} }
if (process.env.NODE_ENV === 'production') { if (opts.beforeRender) {
render(renderCtx) await opts.beforeRender()
return emitter
} else {
return { emitter, renderCtx }
} }
render(renderCtx)
} }
export async function render(renderingProps: RenderRouteInfo): Promise<void> { export async function render(renderingProps: RenderRouteInfo): Promise<void> {
......
...@@ -47,8 +47,8 @@ window.next = { ...@@ -47,8 +47,8 @@ window.next = {
render, render,
renderError, renderError,
} }
initNext({ webpackHMR }) initNext({ webpackHMR, beforeRender: displayContent })
.then(({ renderCtx }) => { .then(() => {
initOnDemandEntries() initOnDemandEntries()
let buildIndicatorHandler = () => {} let buildIndicatorHandler = () => {}
...@@ -104,11 +104,6 @@ initNext({ webpackHMR }) ...@@ -104,11 +104,6 @@ initNext({ webpackHMR })
buildIndicatorHandler = handler buildIndicatorHandler = handler
}, process.env.__NEXT_BUILD_INDICATOR_POSITION) }, process.env.__NEXT_BUILD_INDICATOR_POSITION)
} }
// delay rendering until after styles have been applied in development
displayContent(() => {
render(renderCtx)
})
}) })
.catch((err) => { .catch((err) => {
console.error('Error was not caught', err) console.error('Error was not caught', err)
......
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