Unverified Commit 2d987598 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle on-demand-entries and error overlay for server components (#38480)

parent d920a171
...@@ -571,14 +571,29 @@ export default async function getBaseWebpackConfig( ...@@ -571,14 +571,29 @@ export default async function getBaseWebpackConfig(
.replace(/\\/g, '/'), .replace(/\\/g, '/'),
...(config.experimental.appDir ...(config.experimental.appDir
? { ? {
[CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT]: [CLIENT_STATIC_FILES_RUNTIME_MAIN_ROOT]: dev
`./` + ? [
path require.resolve(
.relative( `next/dist/compiled/@next/react-refresh-utils/dist/runtime`
dir, ),
path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'app-next.js') `./` +
) path
.replace(/\\/g, '/'), .relative(
dir,
path.join(
NEXT_PROJECT_ROOT_DIST_CLIENT,
'app-next-dev.js'
)
)
.replace(/\\/g, '/'),
]
: `./` +
path
.relative(
dir,
path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'app-next.js')
)
.replace(/\\/g, '/'),
} }
: {}), : {}),
} as ClientEntries) } as ClientEntries)
......
...@@ -140,6 +140,12 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -140,6 +140,12 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
export const AppRouter = require('next/dist/client/components/app-router.client.js').default export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
export const HotReloader = ${
// Disable HotReloader component in production
this.mode === 'development'
? `require('next/dist/client/components/hot-reloader.client.js').default`
: 'null'
}
export const hooksClientContext = require('next/dist/client/components/hooks-client-context.js') export const hooksClientContext = require('next/dist/client/components/hooks-client-context.js')
export const __next_app_webpack_require__ = __webpack_require__ export const __next_app_webpack_require__ = __webpack_require__
......
...@@ -8,6 +8,46 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we ...@@ -8,6 +8,46 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we
/// <reference types="react-dom/experimental" /> /// <reference types="react-dom/experimental" />
// Override chunk URL mapping in the webpack runtime
// https://github.com/webpack/webpack/blob/2738eebc7880835d88c727d364ad37f3ec557593/lib/RuntimeGlobals.js#L204
declare global {
const __webpack_require__: any
}
// eslint-disable-next-line no-undef
const getChunkScriptFilename = __webpack_require__.u
const chunkFilenameMap: any = {}
// eslint-disable-next-line no-undef
__webpack_require__.u = (chunkId: any) => {
return chunkFilenameMap[chunkId] || getChunkScriptFilename(chunkId)
}
// Ignore the module ID transform in client.
// eslint-disable-next-line no-undef
// @ts-expect-error TODO: fix type
self.__next_require__ = __webpack_require__
// eslint-disable-next-line no-undef
// @ts-expect-error TODO: fix type
self.__next_chunk_load__ = (chunk) => {
if (chunk.endsWith('.css')) {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = '/_next/' + chunk
document.head.appendChild(link)
return Promise.resolve()
}
const [chunkId, chunkFileName] = chunk.split(':')
chunkFilenameMap[chunkId] = `static/chunks/${chunkFileName}.js`
// @ts-ignore
// eslint-disable-next-line no-undef
return __webpack_chunk_load__(chunkId)
}
export const version = process.env.__NEXT_VERSION export const version = process.env.__NEXT_VERSION
const appElement: HTMLElement | Document | null = document const appElement: HTMLElement | Document | null = document
...@@ -119,7 +159,7 @@ function useInitialServerResponse(cacheKey: string) { ...@@ -119,7 +159,7 @@ function useInitialServerResponse(cacheKey: string) {
return newResponse return newResponse
} }
const ServerRoot = ({ cacheKey }: { cacheKey: string }) => { function ServerRoot({ cacheKey }: { cacheKey: string }) {
React.useEffect(() => { React.useEffect(() => {
rscCache.delete(cacheKey) rscCache.delete(cacheKey)
}) })
...@@ -128,6 +168,19 @@ const ServerRoot = ({ cacheKey }: { cacheKey: string }) => { ...@@ -128,6 +168,19 @@ const ServerRoot = ({ cacheKey }: { cacheKey: string }) => {
return root return root
} }
function ErrorOverlay({
children,
}: React.PropsWithChildren<{}>): React.ReactElement {
if (process.env.NODE_ENV === 'production') {
return <>{children}</>
} else {
const {
ReactDevOverlay,
} = require('next/dist/compiled/@next/react-dev-overlay/dist/client')
return <ReactDevOverlay globalOverlay>{children}</ReactDevOverlay>
}
}
function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement { function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
if (process.env.__NEXT_TEST_MODE) { if (process.env.__NEXT_TEST_MODE) {
// eslint-disable-next-line react-hooks/rules-of-hooks // eslint-disable-next-line react-hooks/rules-of-hooks
...@@ -143,17 +196,19 @@ function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement { ...@@ -143,17 +196,19 @@ function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
return children as React.ReactElement return children as React.ReactElement
} }
const RSCComponent = () => { function RSCComponent() {
const cacheKey = getCacheKey() const cacheKey = getCacheKey()
return <ServerRoot cacheKey={cacheKey} /> return <ServerRoot cacheKey={cacheKey} />
} }
export function hydrate() { export function hydrate() {
renderReactElement(appElement!, () => ( renderReactElement(appElement!, () => (
<React.StrictMode> <ErrorOverlay>
<Root> <React.StrictMode>
<RSCComponent /> <Root>
</Root> <RSCComponent />
</React.StrictMode> </Root>
</React.StrictMode>
</ErrorOverlay>
)) ))
} }
import { hydrate, version } from './app-index'
// TODO: implement FOUC guard
// TODO: hydration warning
window.next = {
version,
appDir: true,
}
hydrate()
// TODO: build indicator
...@@ -6,41 +6,7 @@ import 'next/dist/client/components/layout-router.client.js' ...@@ -6,41 +6,7 @@ import 'next/dist/client/components/layout-router.client.js'
window.next = { window.next = {
version, version,
root: true, appDir: true,
}
// Override chunk URL mapping in the webpack runtime
// https://github.com/webpack/webpack/blob/2738eebc7880835d88c727d364ad37f3ec557593/lib/RuntimeGlobals.js#L204
// eslint-disable-next-line no-undef
const getChunkScriptFilename = __webpack_require__.u
const chunkFilenameMap = {}
// eslint-disable-next-line no-undef
__webpack_require__.u = (chunkId) => {
return chunkFilenameMap[chunkId] || getChunkScriptFilename(chunkId)
}
// Ignore the module ID transform in client.
// eslint-disable-next-line no-undef
self.__next_require__ = __webpack_require__
// eslint-disable-next-line no-undef
self.__next_chunk_load__ = (chunk) => {
if (chunk.endsWith('.css')) {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = '/_next/' + chunk
document.head.appendChild(link)
return Promise.resolve()
}
const [chunkId, chunkFileName] = chunk.split(':')
chunkFilenameMap[chunkId] = `static/chunks/${chunkFileName}.js`
// @ts-ignore
// eslint-disable-next-line no-undef
return __webpack_chunk_load__(chunkId)
} }
hydrate() hydrate()
...@@ -50,10 +50,12 @@ export default function AppRouter({ ...@@ -50,10 +50,12 @@ export default function AppRouter({
initialTree, initialTree,
initialCanonicalUrl, initialCanonicalUrl,
children, children,
hotReloader,
}: { }: {
initialTree: FlightRouterState initialTree: FlightRouterState
initialCanonicalUrl: string initialCanonicalUrl: string
children: React.ReactNode children: React.ReactNode
hotReloader?: React.ReactNode
}) { }) {
const [{ tree, cache, pushRef, canonicalUrl }, dispatch] = React.useReducer< const [{ tree, cache, pushRef, canonicalUrl }, dispatch] = React.useReducer<
typeof reducer typeof reducer
...@@ -238,6 +240,7 @@ export default function AppRouter({ ...@@ -238,6 +240,7 @@ export default function AppRouter({
}} }}
> >
{children} {children}
{hotReloader}
</AppTreeContext.Provider> </AppTreeContext.Provider>
</AppRouterContext.Provider> </AppRouterContext.Provider>
</FullAppTreeContext.Provider> </FullAppTreeContext.Provider>
......
This diff is collapsed.
...@@ -377,6 +377,9 @@ export async function renderToHTML( ...@@ -377,6 +377,9 @@ export async function renderToHTML(
const LayoutRouter = const LayoutRouter =
ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default
const HotReloader = ComponentMod.HotReloader as
| typeof import('../client/components/hot-reloader.client').default
| null
const headers = req.headers const headers = req.headers
// @ts-expect-error TODO: fix type of req // @ts-expect-error TODO: fix type of req
...@@ -746,7 +749,7 @@ export async function renderToHTML( ...@@ -746,7 +749,7 @@ export async function renderToHTML(
const search = stringifyQuery(query) const search = stringifyQuery(query)
// TODO: validate req.url as it gets passed to render. // TODO: validate req.url as it gets passed to render.
const initialCanonicalUrl = req.url const initialCanonicalUrl = req.url!
// TODO: change tree to accommodate this // TODO: change tree to accommodate this
// /blog/[...slug]/page.js -> /blog/hello-world/b/c/d -> ['children', 'blog', 'children', ['slug', 'hello-world/b/c/d']] // /blog/[...slug]/page.js -> /blog/hello-world/b/c/d -> ['children', 'blog', 'children', ['slug', 'hello-world/b/c/d']]
...@@ -760,7 +763,8 @@ export async function renderToHTML( ...@@ -760,7 +763,8 @@ export async function renderToHTML(
firstItem: true, firstItem: true,
}) })
const AppRouter = ComponentMod.AppRouter const AppRouter =
ComponentMod.AppRouter as typeof import('../client/components/app-router.client').default
const { const {
QueryContext, QueryContext,
PathnameContext, PathnameContext,
...@@ -774,6 +778,7 @@ export async function renderToHTML( ...@@ -774,6 +778,7 @@ export async function renderToHTML(
<PathnameContext.Provider value={pathname}> <PathnameContext.Provider value={pathname}>
{/* <ParamsContext.Provider value={pathParams}> */} {/* <ParamsContext.Provider value={pathParams}> */}
<AppRouter <AppRouter
hotReloader={HotReloader && <HotReloader assetPrefix="" />}
initialCanonicalUrl={initialCanonicalUrl} initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree} initialTree={initialTree}
> >
......
...@@ -15,6 +15,56 @@ import { serverComponentRegex } from '../../build/webpack/loaders/utils' ...@@ -15,6 +15,56 @@ import { serverComponentRegex } from '../../build/webpack/loaders/utils'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info' import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { isMiddlewareFile, isMiddlewareFilename } from '../../build/utils' import { isMiddlewareFile, isMiddlewareFilename } from '../../build/utils'
import { PageNotFoundError } from '../../shared/lib/utils' import { PageNotFoundError } from '../../shared/lib/utils'
import { FlightRouterState } from '../app-render'
function treePathToEntrypoint(
segmentPath: string[],
parentPath?: string
): string {
const [parallelRouteKey, segment] = segmentPath
const path =
(parentPath ? parentPath + '/' : '') +
(parallelRouteKey !== 'children' ? parallelRouteKey + '/' : '') +
(segment === '' ? 'page' : segment)
// Last segment
if (segmentPath.length === 2) {
return path
}
const childSegmentPath = segmentPath.slice(2)
return treePathToEntrypoint(childSegmentPath, path)
}
function getEntrypointsFromTree(
tree: FlightRouterState,
isFirst: boolean,
parentPath: string[] = []
) {
const [segment, parallelRoutes] = tree
const currentSegment = Array.isArray(segment) ? segment[0] : segment
const currentPath = [...parentPath, currentSegment]
if (!isFirst && currentSegment === '') {
// TODO get rid of '' at the start of tree
return [treePathToEntrypoint(currentPath.slice(1))]
}
return Object.keys(parallelRoutes).reduce(
(paths: string[], key: string): string[] => {
const childTree = parallelRoutes[key]
const childPages = getEntrypointsFromTree(childTree, false, [
...currentPath,
key,
])
return [...paths, ...childPages]
},
[]
)
}
export const ADDED = Symbol('added') export const ADDED = Symbol('added')
export const BUILDING = Symbol('building') export const BUILDING = Symbol('building')
...@@ -78,6 +128,7 @@ export function onDemandEntryHandler({ ...@@ -78,6 +128,7 @@ export function onDemandEntryHandler({
invalidator = new Invalidator(multiCompiler) invalidator = new Invalidator(multiCompiler)
const doneCallbacks: EventEmitter | null = new EventEmitter() const doneCallbacks: EventEmitter | null = new EventEmitter()
const lastClientAccessPages = [''] const lastClientAccessPages = ['']
const lastServerAccessPagesForAppDir = ['']
const startBuilding = (_compilation: webpack.Compilation) => { const startBuilding = (_compilation: webpack.Compilation) => {
invalidator.startBuilding() invalidator.startBuilding()
...@@ -153,9 +204,48 @@ export function onDemandEntryHandler({ ...@@ -153,9 +204,48 @@ export function onDemandEntryHandler({
const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge)) const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge))
setInterval(function () { setInterval(function () {
disposeInactiveEntries(lastClientAccessPages, maxInactiveAge) disposeInactiveEntries(
lastClientAccessPages,
lastServerAccessPagesForAppDir,
maxInactiveAge
)
}, pingIntervalTime + 1000).unref() }, pingIntervalTime + 1000).unref()
function handleAppDirPing(
tree: FlightRouterState
): { success: true } | { invalid: true } {
const pages = getEntrypointsFromTree(tree, true)
for (const page of pages) {
const pageKey = `server/${page}`
const entryInfo = entries[pageKey]
// If there's no entry, it may have been invalidated and needs to be re-built.
if (!entryInfo) {
// if (page !== lastEntry) client pings, but there's no entry for page
return { invalid: true }
}
// We don't need to maintain active state of anything other than BUILT entries
if (entryInfo.status !== BUILT) continue
// If there's an entryInfo
if (!lastServerAccessPagesForAppDir.includes(pageKey)) {
lastServerAccessPagesForAppDir.unshift(pageKey)
// Maintain the buffer max length
// TODO: verify that the current pageKey is not at the end of the array as multiple entrypoints can exist
if (lastServerAccessPagesForAppDir.length > pagesBufferLength) {
lastServerAccessPagesForAppDir.pop()
}
}
entryInfo.lastActiveTime = Date.now()
entryInfo.dispose = false
}
return { success: true }
}
function handlePing(pg: string) { function handlePing(pg: string) {
const page = normalizePathSep(pg) const page = normalizePathSep(pg)
const pageKey = `client${page}` const pageKey = `client${page}`
...@@ -272,11 +362,13 @@ export function onDemandEntryHandler({ ...@@ -272,11 +362,13 @@ export function onDemandEntryHandler({
) )
if (parsedData.event === 'ping') { if (parsedData.event === 'ping') {
const result = handlePing(parsedData.page) const result = parsedData.appDirRoute
? handleAppDirPing(parsedData.tree)
: handlePing(parsedData.page)
client.send( client.send(
JSON.stringify({ JSON.stringify({
...result, ...result,
event: 'pong', [parsedData.appDirRoute ? 'action' : 'event']: 'pong',
}) })
) )
} }
...@@ -288,10 +380,17 @@ export function onDemandEntryHandler({ ...@@ -288,10 +380,17 @@ export function onDemandEntryHandler({
function disposeInactiveEntries( function disposeInactiveEntries(
lastClientAccessPages: string[], lastClientAccessPages: string[],
lastServerAccessPagesForAppDir: string[],
maxInactiveAge: number maxInactiveAge: number
) { ) {
Object.keys(entries).forEach((page) => { Object.keys(entries).forEach((page) => {
const { lastActiveTime, status, dispose } = entries[page] const { lastActiveTime, status, dispose, bundlePath } = entries[page]
const isClientComponentsEntry =
bundlePath.startsWith('app/') && page.startsWith('client/')
// Disposing client component entry is handled when disposing server component entry
if (isClientComponentsEntry) return
// Skip pages already scheduled for disposing // Skip pages already scheduled for disposing
if (dispose) return if (dispose) return
...@@ -303,15 +402,26 @@ function disposeInactiveEntries( ...@@ -303,15 +402,26 @@ function disposeInactiveEntries(
// We should not build the last accessed page even we didn't get any pings // We should not build the last accessed page even we didn't get any pings
// Sometimes, it's possible our XHR ping to wait before completing other requests. // Sometimes, it's possible our XHR ping to wait before completing other requests.
// In that case, we should not dispose the current viewing page // In that case, we should not dispose the current viewing page
if (lastClientAccessPages.includes(page)) return if (
lastClientAccessPages.includes(page) ||
lastServerAccessPagesForAppDir.includes(page)
)
return
if (lastActiveTime && Date.now() - lastActiveTime > maxInactiveAge) { if (lastActiveTime && Date.now() - lastActiveTime > maxInactiveAge) {
const isServerComponentsEntry =
bundlePath.startsWith('app/') && page.startsWith('server/')
// Dispose client component entrypoint when server component entrypoint is disposed.
if (isServerComponentsEntry) {
entries[page.replace('server/', 'client/')].dispose = true
}
entries[page].dispose = true entries[page].dispose = true
} }
}) })
} }
// Make sure only one invalidation happens at a time // Make sure only one invalidation happens at a time∫
// Otherwise, webpack hash gets changed and it'll force the client to reload. // Otherwise, webpack hash gets changed and it'll force the client to reload.
class Invalidator { class Invalidator {
private multiCompiler: webpack.MultiCompiler private multiCompiler: webpack.MultiCompiler
......
...@@ -47,9 +47,11 @@ type ErrorType = 'runtime' | 'build' ...@@ -47,9 +47,11 @@ type ErrorType = 'runtime' | 'build'
const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({
children, children,
preventDisplay, preventDisplay,
globalOverlay,
}: { }: {
children?: React.ReactNode children?: React.ReactNode
preventDisplay?: ErrorType[] preventDisplay?: ErrorType[]
globalOverlay?: boolean
}) { }) {
const [state, dispatch] = React.useReducer< const [state, dispatch] = React.useReducer<
React.Reducer<OverlayState, Bus.BusEvent> React.Reducer<OverlayState, Bus.BusEvent>
...@@ -84,7 +86,7 @@ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({ ...@@ -84,7 +86,7 @@ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({
{children ?? null} {children ?? null}
</ErrorBoundary> </ErrorBoundary>
{isMounted ? ( {isMounted ? (
<ShadowPortal> <ShadowPortal globalOverlay={globalOverlay}>
<CssReset /> <CssReset />
<Base /> <Base />
<ComponentStyles /> <ComponentStyles />
......
...@@ -3,10 +3,12 @@ import { createPortal } from 'react-dom' ...@@ -3,10 +3,12 @@ import { createPortal } from 'react-dom'
export type ShadowPortalProps = { export type ShadowPortalProps = {
children: React.ReactNode children: React.ReactNode
globalOverlay?: boolean
} }
export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({
children, children,
globalOverlay,
}) { }) {
let mountNode = React.useRef<HTMLDivElement | null>(null) let mountNode = React.useRef<HTMLDivElement | null>(null)
let portalNode = React.useRef<HTMLElement | null>(null) let portalNode = React.useRef<HTMLElement | null>(null)
...@@ -14,7 +16,9 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({ ...@@ -14,7 +16,9 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({
let [, forceUpdate] = React.useState<{} | undefined>() let [, forceUpdate] = React.useState<{} | undefined>()
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
const ownerDocument = mountNode.current!.ownerDocument! const ownerDocument = globalOverlay
? document
: mountNode.current!.ownerDocument!
portalNode.current = ownerDocument.createElement('nextjs-portal') portalNode.current = ownerDocument.createElement('nextjs-portal')
shadowNode.current = portalNode.current.attachShadow({ mode: 'open' }) shadowNode.current = portalNode.current.attachShadow({ mode: 'open' })
ownerDocument.body.appendChild(portalNode.current) ownerDocument.body.appendChild(portalNode.current)
...@@ -24,11 +28,11 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({ ...@@ -24,11 +28,11 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({
portalNode.current.ownerDocument.body.removeChild(portalNode.current) portalNode.current.ownerDocument.body.removeChild(portalNode.current)
} }
} }
}, []) }, [globalOverlay])
return shadowNode.current ? ( return shadowNode.current ? (
createPortal(children, shadowNode.current as any) createPortal(children, shadowNode.current as any)
) : ( ) : globalOverlay ? null : (
<span ref={mountNode} /> <span ref={mountNode} />
) )
} }
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