Unverified Commit e4e5c167 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix css applying for root not found (#47992)

### What

This issue is introduced in #47688, we need to do the same work for
rendering single component which collecting the assets and then render
with root layout + root not found

Fix #47970
Related #47862 (partially fix the css issue but not link issue)

### How

This PR encapsulates the preload and stylesheets assets collection and
rendering process, and move them into a helper, and share between the
component rendering and the root not found rendering
parent 9b0af046
...@@ -2541,11 +2541,14 @@ export default async function build( ...@@ -2541,11 +2541,14 @@ export default async function build(
const updatedRelativeDest = path const updatedRelativeDest = path
.join('pages', '404.html') .join('pages', '404.html')
.replace(/\\/g, '/') .replace(/\\/g, '/')
await promises.copyFile(
orig, if (await fileExists(orig)) {
path.join(distDir, 'server', updatedRelativeDest) await promises.copyFile(
) orig,
pagesManifest['/404'] = updatedRelativeDest path.join(distDir, 'server', updatedRelativeDest)
)
pagesManifest['/404'] = updatedRelativeDest
}
}) })
} }
......
...@@ -75,7 +75,7 @@ type AppRouterProps = Omit< ...@@ -75,7 +75,7 @@ type AppRouterProps = Omit<
assetPrefix: string assetPrefix: string
// Top level boundaries props // Top level boundaries props
notFound: React.ReactNode | undefined notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined notFoundStyles?: React.ReactNode | undefined
asNotFound?: boolean asNotFound?: boolean
} }
......
...@@ -459,6 +459,89 @@ export async function renderToHTMLOrFlight( ...@@ -459,6 +459,89 @@ export async function renderToHTMLOrFlight(
return [Comp, styles] return [Comp, styles]
} }
const createStaticAssets = async ({
layoutOrPagePath,
injectedCSS: injectedCSSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
}: {
layoutOrPagePath: string | undefined
injectedCSS: Set<string>
injectedFontPreloadTags: Set<string>
}) => {
const stylesheets: string[] = layoutOrPagePath
? getCssInlinedLinkTags(
clientReferenceManifest,
serverCSSManifest!,
layoutOrPagePath,
serverCSSForEntries,
injectedCSSWithCurrentLayout,
true
)
: []
const preloadedFontFiles = layoutOrPagePath
? getPreloadedFontFilesInlineLinkTags(
serverCSSManifest!,
nextFontManifest,
serverCSSForEntries,
layoutOrPagePath,
injectedFontPreloadTagsWithCurrentLayout
)
: []
return (
<>
{preloadedFontFiles?.length === 0 ? (
<link
data-next-font={
nextFontManifest?.appUsingSizeAdjust ? 'size-adjust' : ''
}
rel="preconnect"
href="/"
crossOrigin="anonymous"
/>
) : null}
{preloadedFontFiles
? preloadedFontFiles.map((fontFile) => {
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(fontFile)![1]
return (
<link
key={fontFile}
rel="preload"
href={`${assetPrefix}/_next/${fontFile}`}
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
data-next-font={
fontFile.includes('-s') ? 'size-adjust' : ''
}
/>
)
})
: null}
{stylesheets
? stylesheets.map((href, index) => (
<link
rel="stylesheet"
// In dev, Safari will wrongly cache the resource if you preload it:
// - https://github.com/vercel/next.js/issues/5860
// - https://bugs.webkit.org/show_bug.cgi?id=187726
// We used to add a `?ts=` query for resources in `pages` to bypass it,
// but in this case it is fine as we don't need to preload the styles.
href={`${assetPrefix}/_next/${href}`}
// `Precedence` is an opt-in signal for React to handle
// resource loading and deduplication, etc:
// https://github.com/facebook/react/pull/25060
// @ts-ignore
precedence="next.js"
key={index}
/>
))
: null}
</>
)
}
/** /**
* Use the provided loader tree to create the React Component tree. * Use the provided loader tree to create the React Component tree.
*/ */
...@@ -498,29 +581,15 @@ export async function renderToHTMLOrFlight( ...@@ -498,29 +581,15 @@ export async function renderToHTMLOrFlight(
const layoutOrPagePath = layout?.[1] || page?.[1] const layoutOrPagePath = layout?.[1] || page?.[1]
const injectedCSSWithCurrentLayout = new Set(injectedCSS) const injectedCSSWithCurrentLayout = new Set(injectedCSS)
const stylesheets: string[] = layoutOrPagePath
? getCssInlinedLinkTags(
clientReferenceManifest,
serverCSSManifest!,
layoutOrPagePath,
serverCSSForEntries,
injectedCSSWithCurrentLayout,
true
)
: []
const injectedFontPreloadTagsWithCurrentLayout = new Set( const injectedFontPreloadTagsWithCurrentLayout = new Set(
injectedFontPreloadTags injectedFontPreloadTags
) )
const preloadedFontFiles = layoutOrPagePath
? getPreloadedFontFilesInlineLinkTags( const assets = createStaticAssets({
serverCSSManifest!, layoutOrPagePath,
nextFontManifest, injectedCSS: injectedCSSWithCurrentLayout,
serverCSSForEntries, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
layoutOrPagePath, })
injectedFontPreloadTagsWithCurrentLayout
)
: []
const [Template, templateStyles] = template const [Template, templateStyles] = template
? await createComponentAndStyles({ ? await createComponentAndStyles({
...@@ -855,53 +924,7 @@ export async function renderToHTMLOrFlight( ...@@ -855,53 +924,7 @@ export async function renderToHTMLOrFlight(
) : ( ) : (
<Component {...props} /> <Component {...props} />
)} )}
{preloadedFontFiles?.length === 0 ? ( {assets}
<link
data-next-font={
nextFontManifest?.appUsingSizeAdjust ? 'size-adjust' : ''
}
rel="preconnect"
href="/"
crossOrigin="anonymous"
/>
) : null}
{preloadedFontFiles
? preloadedFontFiles.map((fontFile) => {
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(fontFile)![1]
return (
<link
key={fontFile}
rel="preload"
href={`${assetPrefix}/_next/${fontFile}`}
as="font"
type={`font/${ext}`}
crossOrigin="anonymous"
data-next-font={
fontFile.includes('-s') ? 'size-adjust' : ''
}
/>
)
})
: null}
{stylesheets
? stylesheets.map((href, index) => (
<link
rel="stylesheet"
// In dev, Safari will wrongly cache the resource if you preload it:
// - https://github.com/vercel/next.js/issues/5860
// - https://bugs.webkit.org/show_bug.cgi?id=187726
// We used to add a `?ts=` query for resources in `pages` to bypass it,
// but in this case it is fine as we don't need to preload the styles.
href={`${assetPrefix}/_next/${href}`}
// `Precedence` is an opt-in signal for React to handle
// resource loading and deduplication, etc:
// https://github.com/facebook/react/pull/25060
// @ts-ignore
precedence="next.js"
key={index}
/>
))
: null}
</> </>
) )
}, },
...@@ -1201,13 +1224,15 @@ export async function renderToHTMLOrFlight( ...@@ -1201,13 +1224,15 @@ export async function renderToHTMLOrFlight(
async (props) => { async (props) => {
// Create full component tree from root to leaf. // Create full component tree from root to leaf.
const injectedCSS = new Set<string>() const injectedCSS = new Set<string>()
const injectedFontPreloadTags = new Set<string>()
const { Component: ComponentTree } = await createComponentTree({ const { Component: ComponentTree } = await createComponentTree({
createSegmentPath: (child) => child, createSegmentPath: (child) => child,
loaderTree, loaderTree,
parentParams: {}, parentParams: {},
firstItem: true, firstItem: true,
injectedCSS, injectedCSS,
injectedFontPreloadTags: new Set(), injectedFontPreloadTags,
rootLayoutIncluded: false, rootLayoutIncluded: false,
asNotFound: props.asNotFound, asNotFound: props.asNotFound,
}) })
...@@ -1229,6 +1254,12 @@ export async function renderToHTMLOrFlight( ...@@ -1229,6 +1254,12 @@ export async function renderToHTMLOrFlight(
? [DefaultNotFound] ? [DefaultNotFound]
: [] : []
const assets = createStaticAssets({
layoutOrPagePath: layout?.[1],
injectedCSS,
injectedFontPreloadTags,
})
const initialTree = createFlightRouterStateFromLoaderTree( const initialTree = createFlightRouterStateFromLoaderTree(
loaderTree, loaderTree,
getDynamicParamFromSegment, getDynamicParamFromSegment,
...@@ -1255,12 +1286,15 @@ export async function renderToHTMLOrFlight( ...@@ -1255,12 +1286,15 @@ export async function renderToHTMLOrFlight(
globalErrorComponent={GlobalError} globalErrorComponent={GlobalError}
notFound={ notFound={
NotFound && RootLayout ? ( NotFound && RootLayout ? (
<RootLayout params={{}}> <>
<NotFound /> {assets}
</RootLayout> <RootLayout params={{}}>
{notFoundStyles}
<NotFound />
</RootLayout>
</>
) : undefined ) : undefined
} }
notFoundStyles={notFoundStyles}
asNotFound={props.asNotFound} asNotFound={props.asNotFound}
> >
<ComponentTree /> <ComponentTree />
......
export default function RootNotFound() {
return <h1 className="not-found">Root not found</h1>
}
body { body {
font-size: large; font-size: large;
} }
.not-found {
color: rgb(210, 105, 30); /* chocolate */
}
...@@ -194,6 +194,15 @@ createNextDescribe( ...@@ -194,6 +194,15 @@ createNextDescribe(
).toBe('rgb(255, 0, 0)') ).toBe('rgb(255, 0, 0)')
}) })
it('should include root layout css for root not-found.js', async () => {
const browser = await next.browser('/this-path-does-not-exist')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(210, 105, 30)')
})
it('should include css imported in error.js', async () => { it('should include css imported in error.js', async () => {
const browser = await next.browser('/error/client-component') const browser = await next.browser('/error/client-component')
await browser.elementByCss('button').click() await browser.elementByCss('button').click()
......
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