Skip to content

Commit 4e64c02

Browse files
committed
exempt data URL CSS from Pages Router global CSS restriction
data:text/css imports are generated by tooling (e.g. next-yak) and are inline per-component — the global CSS ordering concern doesn't apply. The validation already had a carve-out for node_modules, this adds the same for data URL modules.
1 parent ae64f6c commit 4e64c02

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

crates/next-api/src/module_graph.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,12 @@ async fn validate_pages_css_imports_individual(
846846
candidates
847847
.into_iter()
848848
.map(async |issue| {
849+
let path = issue.module.ident().path().await?;
849850
// We allow imports of global CSS files which are inside of `node_modules`.
851+
// We also allow data URL CSS imports (e.g. `data:text/css,...`) since they
852+
// are mostly tooling-generated and co-located with the importing components
850853
Ok(
851-
if !issue.module.ident().path().await?.is_in_node_modules() {
854+
if !path.is_in_node_modules() && !path.file_name().starts_with("data:") {
852855
Some(issue)
853856
} else {
854857
None
@@ -868,8 +871,9 @@ async fn validate_pages_css_imports_individual(
868871
/// Validates that the global CSS/SCSS/SASS imports are only valid imports with the following
869872
/// rules:
870873
/// * The import is made from a `node_modules` package
874+
/// * The imported CSS is a `data:` URL module
871875
/// * The import is made from a `.module.css` file
872-
/// * The import is made from the `pages/_app.js`, or equivalent file.
876+
/// * The import is made from the `pages/_app.js`, or equivalent file
873877
#[turbo_tasks::function]
874878
pub async fn validate_pages_css_imports(
875879
graph: Vc<ModuleGraph>,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
// CSS data urls are only supported in Turbopack
4+
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
5+
'css-data-url-global-pages',
6+
() => {
7+
const { next } = nextTestSetup({
8+
files: __dirname,
9+
})
10+
11+
it('should apply styles from data url correctly', async () => {
12+
const browser = await next.browser('/')
13+
14+
const fontWeight = await browser
15+
.elementByCss('#styled')
16+
.getComputedCss('font-weight')
17+
18+
expect(fontWeight).toBe('700')
19+
})
20+
}
21+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { NextConfig } from 'next'
2+
3+
const nextConfig: NextConfig = {
4+
/* config options here */
5+
}
6+
7+
export default nextConfig
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @ts-expect-error
2+
import 'data:text/css,#styled{font-weight:700}'
3+
4+
export default function Home() {
5+
return <div id="styled">This text should be bold</div>
6+
}

0 commit comments

Comments
 (0)