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
// `style-loader` in development. It must be called before hydration, or else
// rendering won't have the correct computed values in effects.
export function displayContent(callback) {
;(window.requestAnimationFrame || setTimeout)(function () {
for (
var x = document.querySelectorAll('[data-next-hide-fouc]'), i = x.length;
i--;
export function displayContent(): Promise<void> {
return new Promise((resolve) => {
;(window.requestAnimationFrame || setTimeout)(function () {
for (
var x = document.querySelectorAll('[data-next-hide-fouc]'),
i = x.length;
i--;
) {
x[i].parentNode.removeChild(x[i])
}
if (callback) {
callback()
}
) {
x[i].parentNode!.removeChild(x[i])
}
resolve()
})
})
}
......@@ -271,7 +271,9 @@ class Container extends React.Component<{
export const emitter: MittEmitter<string> = mitt()
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
if (process.env.NODE_ENV === 'development') {
webpackHMR = opts.webpackHMR
......@@ -425,12 +427,11 @@ export async function initNext(opts: { webpackHMR?: any } = {}) {
err: initialErr,
}
if (process.env.NODE_ENV === 'production') {
render(renderCtx)
return emitter
} else {
return { emitter, renderCtx }
if (opts.beforeRender) {
await opts.beforeRender()
}
render(renderCtx)
}
export async function render(renderingProps: RenderRouteInfo): Promise<void> {
......
......@@ -47,8 +47,8 @@ window.next = {
render,
renderError,
}
initNext({ webpackHMR })
.then(({ renderCtx }) => {
initNext({ webpackHMR, beforeRender: displayContent })
.then(() => {
initOnDemandEntries()
let buildIndicatorHandler = () => {}
......@@ -104,11 +104,6 @@ initNext({ webpackHMR })
buildIndicatorHandler = handler
}, process.env.__NEXT_BUILD_INDICATOR_POSITION)
}
// delay rendering until after styles have been applied in development
displayContent(() => {
render(renderCtx)
})
})
.catch((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