Unverified Commit b1503e13 authored by khades's avatar khades Committed by GitHub

Fix dynamic css unloading (#17173)

Serverside Dynamically loaded CSS module file insertion adds css-files as usual static files with special data-n-p tag, that is used in page transition logic. That files get removed on page transition cause they are not explicitly required in scope of page.

Mini-css-extract-plugin adds style tags at chunk insertion without any tags and leave them be, no matter how many page transitions were made.

I removed data-n-p tag from dynamically loaded css module files and added new data-n-d tag for it.

Fixes #16950 
parent 56bd4648
...@@ -163,8 +163,11 @@ export class Head extends Component< ...@@ -163,8 +163,11 @@ export class Head extends Component<
dynamicImports, dynamicImports,
} = this.context } = this.context
const cssFiles = files.allFiles.filter((f) => f.endsWith('.css')) const cssFiles = files.allFiles.filter((f) => f.endsWith('.css'))
const sharedFiles = new Set(files.sharedFiles) const sharedFiles: Set<string> = new Set(files.sharedFiles)
// Unmanaged files are CSS files that will be handled directly by the
// webpack runtime (`mini-css-extract-plugin`).
let unmangedFiles: Set<string> = new Set([])
let dynamicCssFiles = dedupe( let dynamicCssFiles = dedupe(
dynamicImports.filter((f) => f.file.endsWith('.css')) dynamicImports.filter((f) => f.file.endsWith('.css'))
).map((f) => f.file) ).map((f) => f.file)
...@@ -173,13 +176,14 @@ export class Head extends Component< ...@@ -173,13 +176,14 @@ export class Head extends Component<
dynamicCssFiles = dynamicCssFiles.filter( dynamicCssFiles = dynamicCssFiles.filter(
(f) => !(existing.has(f) || sharedFiles.has(f)) (f) => !(existing.has(f) || sharedFiles.has(f))
) )
unmangedFiles = new Set(dynamicCssFiles)
cssFiles.push(...dynamicCssFiles) cssFiles.push(...dynamicCssFiles)
} }
const cssLinkElements: JSX.Element[] = [] const cssLinkElements: JSX.Element[] = []
cssFiles.forEach((file) => { cssFiles.forEach((file) => {
const isSharedFile = sharedFiles.has(file) const isSharedFile = sharedFiles.has(file)
const isUnmanagedFile = unmangedFiles.has(file)
cssLinkElements.push( cssLinkElements.push(
<link <link
key={`${file}-preload`} key={`${file}-preload`}
...@@ -203,8 +207,8 @@ export class Head extends Component< ...@@ -203,8 +207,8 @@ export class Head extends Component<
crossOrigin={ crossOrigin={
this.props.crossOrigin || process.env.__NEXT_CROSS_ORIGIN this.props.crossOrigin || process.env.__NEXT_CROSS_ORIGIN
} }
data-n-g={isSharedFile ? '' : undefined} data-n-g={isUnmanagedFile ? undefined : isSharedFile ? '' : undefined}
data-n-p={isSharedFile ? undefined : ''} data-n-p={isUnmanagedFile ? undefined : isSharedFile ? undefined : ''}
/> />
) )
}) })
......
import styles from './with-css.module.css' import styles from './with-css.module.css'
export default () => <p className={styles.content}>With CSS</p> export default () => (
<p id="with-css" className={styles.content}>
With CSS
</p>
)
.content { .content {
color: inherit; color: inherit;
display: flex;
} }
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/dynamic-css/with-css'))
export default function PageChange1() {
return (
<div>
PageChange1
<Hello />
</div>
)
}
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/dynamic-css/with-css'))
export default function PageChange2() {
return (
<div>
PageChange2
<Hello />
</div>
)
}
...@@ -40,6 +40,28 @@ export default (context, render) => { ...@@ -40,6 +40,28 @@ export default (context, render) => {
expect(cssFiles.length).toBe(1) expect(cssFiles.length).toBe(1)
}) })
it('should not remove css styles for same css file between page transitions', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/dynamic/pagechange1')
await check(() => browser.elementByCss('body').text(), /PageChange1/)
const firstElement = await browser.elementById('with-css')
const css1 = await firstElement.getComputedCss('display')
expect(css1).toBe('flex')
await browser.eval(() =>
window.next.router.push('/dynamic/pagechange2')
)
await check(() => browser.elementByCss('body').text(), /PageChange2/)
const secondElement = await browser.elementById('with-css')
const css2 = await secondElement.getComputedCss('display')
expect(css2).toBe(css1)
} finally {
if (browser) {
await browser.close()
}
}
})
// It seem to be abnormal, dynamic CSS modules are completely self-sufficient, so shared styles are copied across files // It seem to be abnormal, dynamic CSS modules are completely self-sufficient, so shared styles are copied across files
it('should output two css files even in case of three css module files while one is shared across files', async () => { it('should output two css files even in case of three css module files while one is shared across files', async () => {
const $ = await get$('/dynamic/shared-css-module') const $ = await get$('/dynamic/shared-css-module')
......
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