Unverified Commit 10928dfa authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[global-not-found] fix shared css imports not being picked (#80151)

parent 1a188847
......@@ -108,6 +108,9 @@ const pluginState = getProxiedPluginState({
injectedClientEntries: {} as Record<string, string>,
})
const POSSIBLE_SHARED_CONVENTIONS = ['template', 'layout']
const STANDALONE_BUNDLE_CONVENTION = 'global-not-found'
function deduplicateCSSImportsForEntry(mergedCSSimports: CssImports) {
// If multiple entry module connections are having the same CSS import,
// we only need to have one module to keep track of that CSS import.
......@@ -138,8 +141,8 @@ function deduplicateCSSImportsForEntry(mergedCSSimports: CssImports) {
const aName = path.parse(aPath).name
const bName = path.parse(bPath).name
const indexA = ['template', 'layout'].indexOf(aName)
const indexB = ['template', 'layout'].indexOf(bName)
const indexA = POSSIBLE_SHARED_CONVENTIONS.indexOf(aName)
const indexB = POSSIBLE_SHARED_CONVENTIONS.indexOf(bName)
if (indexA === -1) return 1
if (indexB === -1) return -1
......@@ -148,20 +151,29 @@ function deduplicateCSSImportsForEntry(mergedCSSimports: CssImports) {
const dedupedCSSImports: CssImports = {}
const trackedCSSImports = new Set<string>()
for (const [entryName, cssImports] of sortedCSSImports) {
for (const [entryFilePath, cssImports] of sortedCSSImports) {
const entryConventionName = path.parse(entryFilePath).name
for (const cssImport of cssImports) {
if (trackedCSSImports.has(cssImport)) continue
// If the CSS import is already tracked, we can skip it.
// Or if it's any standalone entry such as `global-not-found`, it won't share any resources with other entry, skip it.
if (
trackedCSSImports.has(cssImport) &&
STANDALONE_BUNDLE_CONVENTION !== entryConventionName
) {
continue
}
// Only track CSS imports that are in files that can inherit CSS.
const filename = path.parse(entryName).name
if (['template', 'layout'].includes(filename)) {
if (POSSIBLE_SHARED_CONVENTIONS.includes(entryConventionName)) {
trackedCSSImports.add(cssImport)
}
if (!dedupedCSSImports[entryName]) {
dedupedCSSImports[entryName] = []
if (!dedupedCSSImports[entryFilePath]) {
dedupedCSSImports[entryFilePath] = []
}
dedupedCSSImports[entryName].push(cssImport)
dedupedCSSImports[entryFilePath].push(cssImport)
}
}
......
'use client'
import { notFound } from 'next/navigation'
export default function Page() {
notFound()
}
'use client'
export function Client() {
return <div id="client">client</div>
}
import { Client } from './client'
import './global.css'
import './global-not-found.css'
export default function GlobalNotFound() {
return (
// html tag is different from actual page's layout
<html data-global-not-found="true">
<body>
<h1 id="global-error-title">global-not-found</h1>
<Client />
<p className="orange-text">orange text (from global.css)</p>
<p className="blue-text">blue text (from global-not-found.css)</p>
</body>
</html>
)
}
.orange-text {
color: rgb(255, 165, 0);
}
import './global.css'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
<footer>
<p className="orange-text">layout footer orange text</p>
</footer>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('global-not-found - css', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should include css for global-not-found properly', async () => {
const browser = await next.browser('/does-not-exist')
// This css import is shared with `/` route's layout
const textColorOrange = await browser
.elementByCss('.orange-text')
.getComputedCss('color')
expect(textColorOrange).toBe('rgb(255, 165, 0)')
// This css import is only used in the global-not-found convention
const textColorRed = await browser
.elementByCss('.blue-text')
.getComputedCss('color')
expect(textColorRed).toBe('rgb(0, 0, 255)')
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
globalNotFound: true,
},
}
module.exports = nextConfig
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