Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate routing and initial hydration #33875

Merged
merged 6 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -268,7 +268,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 @@ -422,12 +424,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