Skip to content

Commit

Permalink
Encapsulate routing and initial hydration (vercel#33875)
Browse files Browse the repository at this point in the history
* Encapsulate initial rendering

* Run dev code before rendering

* Fix call

* Try reverting last change, fixing call
  • Loading branch information
devknoll authored and natew committed Feb 16, 2022
1 parent a981267 commit d35bb9b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
17 changes: 0 additions & 17 deletions packages/next/client/dev/fouc.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/next/client/dev/fouc.ts
@@ -0,0 +1,18 @@
// 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(): 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])
}
resolve()
})
})
}
13 changes: 7 additions & 6 deletions packages/next/client/index.tsx
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand Down
9 changes: 2 additions & 7 deletions packages/next/client/next-dev.js
Expand Up @@ -47,8 +47,8 @@ window.next = {
render,
renderError,
}
initNext({ webpackHMR })
.then(({ renderCtx }) => {
initNext({ webpackHMR, beforeRender: displayContent })
.then(() => {
initOnDemandEntries()

let buildIndicatorHandler = () => {}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d35bb9b

Please sign in to comment.