Unverified Commit a3918569 authored by Jude Gao's avatar Jude Gao Committed by GitHub

fix global-error styles (#71914)

parent 655b808c
...@@ -311,7 +311,7 @@ impl AppPageLoaderTreeBuilder { ...@@ -311,7 +311,7 @@ impl AppPageLoaderTreeBuilder {
page, page,
default, default,
error, error,
global_error: _, global_error,
layout, layout,
loading, loading,
template, template,
...@@ -344,6 +344,8 @@ impl AppPageLoaderTreeBuilder { ...@@ -344,6 +344,8 @@ impl AppPageLoaderTreeBuilder {
.await?; .await?;
self.write_modules_entry(AppDirModuleType::DefaultPage, *default) self.write_modules_entry(AppDirModuleType::DefaultPage, *default)
.await?; .await?;
self.write_modules_entry(AppDirModuleType::GlobalError, *global_error)
.await?;
let modules_code = replace(&mut self.loader_tree_code, temp_loader_tree_code); let modules_code = replace(&mut self.loader_tree_code, temp_loader_tree_code);
......
...@@ -28,6 +28,7 @@ pub enum AppDirModuleType { ...@@ -28,6 +28,7 @@ pub enum AppDirModuleType {
Loading, Loading,
Template, Template,
NotFound, NotFound,
GlobalError,
} }
impl AppDirModuleType { impl AppDirModuleType {
...@@ -40,6 +41,7 @@ impl AppDirModuleType { ...@@ -40,6 +41,7 @@ impl AppDirModuleType {
AppDirModuleType::Loading => "loading", AppDirModuleType::Loading => "loading",
AppDirModuleType::Template => "template", AppDirModuleType::Template => "template",
AppDirModuleType::NotFound => "not-found", AppDirModuleType::NotFound => "not-found",
AppDirModuleType::GlobalError => "global-error",
} }
} }
} }
......
...@@ -60,6 +60,7 @@ const FILE_TYPES = { ...@@ -60,6 +60,7 @@ const FILE_TYPES = {
error: 'error', error: 'error',
loading: 'loading', loading: 'loading',
'not-found': 'not-found', 'not-found': 'not-found',
'global-error': 'global-error',
} as const } as const
const GLOBAL_ERROR_FILE_TYPE = 'global-error' const GLOBAL_ERROR_FILE_TYPE = 'global-error'
......
...@@ -183,7 +183,7 @@ function ServerRoot(): React.ReactNode { ...@@ -183,7 +183,7 @@ function ServerRoot(): React.ReactNode {
const router = ( const router = (
<AppRouter <AppRouter
actionQueue={actionQueue} actionQueue={actionQueue}
globalErrorComponent={initialRSCPayload.G} globalErrorComponentAndStyles={initialRSCPayload.G}
assetPrefix={initialRSCPayload.p} assetPrefix={initialRSCPayload.p}
/> />
) )
......
...@@ -622,17 +622,20 @@ function Router({ ...@@ -622,17 +622,20 @@ function Router({
export default function AppRouter({ export default function AppRouter({
actionQueue, actionQueue,
globalErrorComponent, globalErrorComponentAndStyles: [globalErrorComponent, globalErrorStyles],
assetPrefix, assetPrefix,
}: { }: {
actionQueue: AppRouterActionQueue actionQueue: AppRouterActionQueue
globalErrorComponent: ErrorComponent globalErrorComponentAndStyles: [ErrorComponent, React.ReactNode | undefined]
assetPrefix: string assetPrefix: string
}) { }) {
useNavFailureHandler() useNavFailureHandler()
return ( return (
<ErrorBoundary errorComponent={globalErrorComponent}> <ErrorBoundary
errorComponent={globalErrorComponent}
errorStyles={globalErrorStyles}
>
<Router actionQueue={actionQueue} assetPrefix={assetPrefix} /> <Router actionQueue={actionQueue} assetPrefix={assetPrefix} />
</ErrorBoundary> </ErrorBoundary>
) )
......
...@@ -161,6 +161,8 @@ import { InvariantError } from '../../shared/lib/invariant-error' ...@@ -161,6 +161,8 @@ import { InvariantError } from '../../shared/lib/invariant-error'
import './clean-async-snapshot.external' import './clean-async-snapshot.external'
import { INFINITE_CACHE } from '../../lib/constants' import { INFINITE_CACHE } from '../../lib/constants'
import { createComponentStylesAndScripts } from './create-component-styles-and-scripts'
import { parseLoaderTree } from './parse-loader-tree'
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
...@@ -723,6 +725,8 @@ async function getRSCPayload( ...@@ -723,6 +725,8 @@ async function getRSCPayload(
</React.Fragment> </React.Fragment>
) )
const globalErrorStyles = await getGlobalErrorStyles(tree, ctx)
return { return {
// See the comment above the `Preloads` component (below) for why this is part of the payload // See the comment above the `Preloads` component (below) for why this is part of the payload
P: <Preloads preloadCallbacks={preloadCallbacks} />, P: <Preloads preloadCallbacks={preloadCallbacks} />,
...@@ -732,7 +736,7 @@ async function getRSCPayload( ...@@ -732,7 +736,7 @@ async function getRSCPayload(
i: !!couldBeIntercepted, i: !!couldBeIntercepted,
f: [[initialTree, seedData, initialHead]], f: [[initialTree, seedData, initialHead]],
m: missingSlots, m: missingSlots,
G: GlobalError, G: [GlobalError, globalErrorStyles],
s: typeof ctx.renderOpts.postponed === 'string', s: typeof ctx.renderOpts.postponed === 'string',
S: workStore.isStaticGeneration, S: workStore.isStaticGeneration,
} }
...@@ -817,6 +821,8 @@ async function getErrorRSCPayload( ...@@ -817,6 +821,8 @@ async function getErrorRSCPayload(
null, null,
] ]
const globalErrorStyles = await getGlobalErrorStyles(tree, ctx)
return { return {
b: ctx.renderOpts.buildId, b: ctx.renderOpts.buildId,
p: ctx.assetPrefix, p: ctx.assetPrefix,
...@@ -824,7 +830,7 @@ async function getErrorRSCPayload( ...@@ -824,7 +830,7 @@ async function getErrorRSCPayload(
m: undefined, m: undefined,
i: false, i: false,
f: [[initialTree, initialSeedData, initialHead]], f: [[initialTree, initialSeedData, initialHead]],
G: GlobalError, G: [GlobalError, globalErrorStyles],
s: typeof ctx.renderOpts.postponed === 'string', s: typeof ctx.renderOpts.postponed === 'string',
S: workStore.isStaticGeneration, S: workStore.isStaticGeneration,
} satisfies InitialRSCPayload } satisfies InitialRSCPayload
...@@ -881,7 +887,7 @@ function App<T>({ ...@@ -881,7 +887,7 @@ function App<T>({
<ServerInsertedHTMLProvider> <ServerInsertedHTMLProvider>
<AppRouter <AppRouter
actionQueue={actionQueue} actionQueue={actionQueue}
globalErrorComponent={response.G} globalErrorComponentAndStyles={response.G}
assetPrefix={response.p} assetPrefix={response.p}
/> />
</ServerInsertedHTMLProvider> </ServerInsertedHTMLProvider>
...@@ -930,7 +936,7 @@ function AppWithoutContext<T>({ ...@@ -930,7 +936,7 @@ function AppWithoutContext<T>({
return ( return (
<AppRouter <AppRouter
actionQueue={actionQueue} actionQueue={actionQueue}
globalErrorComponent={response.G} globalErrorComponentAndStyles={response.G}
assetPrefix={response.p} assetPrefix={response.p}
/> />
) )
...@@ -3822,3 +3828,26 @@ export async function warmFlightResponse( ...@@ -3822,3 +3828,26 @@ export async function warmFlightResponse(
chunkListeners.push(r) chunkListeners.push(r)
}) })
} }
const getGlobalErrorStyles = async (
tree: LoaderTree,
ctx: AppRenderContext
): Promise<React.ReactNode | undefined> => {
const {
modules: { 'global-error': globalErrorModule },
} = parseLoaderTree(tree)
let globalErrorStyles
if (globalErrorModule) {
const [, styles] = await createComponentStylesAndScripts({
ctx,
filePath: globalErrorModule[1],
getComponent: globalErrorModule[0],
injectedCSS: new Set(),
injectedJS: new Set(),
})
globalErrorStyles = styles
}
return globalErrorStyles
}
...@@ -226,7 +226,7 @@ export type InitialRSCPayload = { ...@@ -226,7 +226,7 @@ export type InitialRSCPayload = {
/** missingSlots */ /** missingSlots */
m: Set<string> | undefined m: Set<string> | undefined
/** GlobalError */ /** GlobalError */
G: React.ComponentType<any> G: [React.ComponentType<any>, React.ReactNode | undefined]
/** postponed */ /** postponed */
s: boolean s: boolean
/** prerendered */ /** prerendered */
......
'use client'
import './global.css'
export default function GlobalError() {
return (
<html>
<body>
<h2>Error!</h2>
</body>
</html>
)
}
// to avoid bailing out of the build
export const dynamic = 'force-dynamic'
export default function RootLayout({ children }) {
throw new Error('Root Layout Error')
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Home() {
return <p>Home</p>
}
import { assertHasRedbox, getRedboxHeader } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'
async function testDev(browser, errorRegex) {
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(errorRegex)
}
describe('app dir - global error - with style import', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
it('should render global error with correct styles', async () => {
const browser = await next.browser('/')
if (isNextDev) {
await testDev(browser, /Root Layout Error/)
return
}
const h2 = await browser.elementByCss('h2')
expect(await h2.getComputedCss('color')).toBe('rgb(255, 255, 0)') // yellow
})
})
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