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

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 16, 2025

Problem

CSS imports with special queries like ?url, ?inline, and ?raw were being incorrectly included in CSS collection for server-side rendering in the RSC plugin. These queries transform CSS imports to return different data types rather than actual CSS that should be linked in documents.

// These should NOT be collected for CSS linking:
import cssUrl from './style.css?url'     // Returns URL string
import cssInline from './style.css?inline' // Returns CSS as string
import cssRaw from './style.css?raw'     // Returns raw CSS content

// This SHOULD be collected:
import './style.css'  // Normal CSS import for linking

Solution

Modified the collectCss function in packages/plugin-rsc/src/plugin.ts to filter out CSS imports with special queries:

  • Added hasSpecialCssQuery() helper function that detects imports with ?url, ?inline, or ?raw queries
  • Updated the CSS collection condition from:
    if (isCSSRequest(next.id)) {
      cssIds.add(next.id)
    }
    to:
    if (isCSSRequest(next.id) && !hasSpecialCssQuery(next.id)) {
      cssIds.add(next.id)
    }

Implementation Details

  • Uses robust URL parsing with string matching fallback for edge cases
  • Handles complex URLs with multiple parameters correctly
  • Maintains full backward compatibility for normal CSS imports
  • Minimal change: only 13 lines of code added

Testing

  • Added comprehensive unit tests covering all edge cases
  • All existing tests continue to pass
  • Manual validation confirms correct behavior:
    • ✅ Normal CSS imports are still collected
    • ✅ CSS imports with special queries are properly excluded

This aligns with standard practices used by other frameworks like SvelteKit and ensures CSS collection works correctly for RSC server-side rendering.

Fixes #571.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Test and fix css import queries ?url ?inline ?raw Fix CSS import queries (?url, ?inline, ?raw) being incorrectly collected in RSC plugin Jul 16, 2025
@Copilot Copilot AI requested a review from hi-ogawa July 16, 2025 03:02
Copilot finished work on behalf of hi-ogawa July 16, 2025 03:02
* 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)

@Copilot Copilot AI requested a review from hi-ogawa July 16, 2025 03:16
Copilot finished work on behalf of hi-ogawa July 16, 2025 03:16
@@ -0,0 +1,23 @@
'use client'
Copy link
Contributor

Choose a reason for hiding this comment

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

add test for server component too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added server component test! Created TestCssQueriesServer component in server.tsx that tests the same CSS import scenarios as the client component. Also updated the e2e tests to verify both client and server component behavior. (8e97c2b)

@hi-ogawa
Copy link
Contributor

I also tested Claude code and it was easier to iterate. Closing in favor of #580

@hi-ogawa hi-ogawa closed this Jul 16, 2025
@hi-ogawa hi-ogawa deleted the copilot/fix-571 branch July 16, 2025 03:36
@Copilot Copilot AI requested a review from hi-ogawa July 16, 2025 03:38
Copilot finished work on behalf of hi-ogawa July 16, 2025 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test and fix css import queries ?url ?inline ?raw
2 participants