Skip to content

Fix CSS import queries (?url, ?inline, ?raw) being incorrectly collected in RSC plugin #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/plugin-rsc/src/css-query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, test } from 'vitest'

/**
* Check if a CSS import ID has special queries that should be excluded from CSS collection.
* These queries transform CSS imports to return different data types rather than actual CSS to be linked.
*/
function hasSpecialCssQuery(id: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove trivial unit tests. add e2e instead by updating examples/basic and e2e/basic.test.ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I've removed the trivial unit tests and replaced them with e2e tests. Added CSS query test components to examples/basic and comprehensive e2e tests in basic.test.ts that verify both the functional behavior and SSR CSS collection. (0f6af41)

try {
const url = new URL(id, 'file://')
return (
url.searchParams.has('url') ||
url.searchParams.has('inline') ||
url.searchParams.has('raw')
)
} catch {
// If URL parsing fails, check with simple string matching as fallback
return id.includes('?url') || id.includes('?inline') || id.includes('?raw')
}
}

describe('hasSpecialCssQuery', () => {
test('should return true for CSS imports with ?url query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?url&other=param')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&url')).toBe(true)
})

test('should return true for CSS imports with ?inline query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?inline')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?inline&other=param')).toBe(
true,
)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&inline')).toBe(
true,
)
})

test('should return true for CSS imports with ?raw query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?raw&other=param')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&raw')).toBe(true)
})

test('should return false for normal CSS imports', () => {
expect(hasSpecialCssQuery('/path/to/style.css')).toBe(false)
expect(hasSpecialCssQuery('/path/to/style.css?t=123456')).toBe(false)
expect(hasSpecialCssQuery('/path/to/style.css?other=param')).toBe(false)
})

test('should handle complex URLs with multiple parameters', () => {
expect(hasSpecialCssQuery('/path/to/style.css?t=123&url&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&inline&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&raw&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&other=param&v=1')).toBe(
false,
)
})

test('should handle absolute URLs', () => {
expect(hasSpecialCssQuery('http://localhost:3000/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?inline')).toBe(
true,
)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?t=123')).toBe(
false,
)
})

test('should handle file URLs', () => {
expect(hasSpecialCssQuery('file:///path/to/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?inline')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?t=123')).toBe(false)
})
})
20 changes: 19 additions & 1 deletion packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,24 @@ export async function findSourceMapURL(
// css support
//

/**
* Check if a CSS import ID has special queries that should be excluded from CSS collection.
* These queries transform CSS imports to return different data types rather than actual CSS to be linked.
*/
function hasSpecialCssQuery(id: string): boolean {
try {
const url = new URL(id, 'file://')
return (
url.searchParams.has('url') ||
url.searchParams.has('inline') ||
url.searchParams.has('raw')
)
} catch {
// If URL parsing fails, check with simple string matching as fallback
return id.includes('?url') || id.includes('?inline') || id.includes('?raw')
}
}

export function vitePluginRscCss(
rscCssOptions?: Pick<RscPluginOptions, 'rscCssTransform'>,
): Plugin[] {
Expand All @@ -1475,7 +1493,7 @@ export function vitePluginRscCss(
}
for (const next of mod?.importedModules ?? []) {
if (next.id) {
if (isCSSRequest(next.id)) {
if (isCSSRequest(next.id) && !hasSpecialCssQuery(next.id)) {
cssIds.add(next.id)
} else {
recurse(next.id)
Expand Down