-
Notifications
You must be signed in to change notification settings - Fork 29.5k
fix: handle raw-loader CSS imports correctly #82095
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
base: canary
Are you sure you want to change the base?
Conversation
Fixes vercel#82000 - Next.js still imports CSS as styles even when loaded with raw-loader - Add webpack rules to handle raw-loader imports before CSS processing - Support multiple raw-loader patterns: ?raw, ?raw-loader, git add packages/next/src/build/webpack/config/blocks/css/index.ts test/integration/css-fixtures/raw-loader-import/next.config.js test/integration/css-fixtures/raw-loader-import/pages/index.js test/integration/css-fixtures/raw-loader-import/pages/styles.css test/integration/css-fixtures/raw-loader-import/test/raw-loader.test.jsraw-loader! - Return raw source content instead of processed CSS when using raw-loader - Add test fixture to verify raw-loader behavior When users import CSS with raw-loader (e.g., require('!!raw-loader!./foo.css')), they now get raw source content as expected instead of having styles applied.
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
const html = await res.text() | ||
|
||
// The page should NOT have a red background (which would indicate CSS was applied) | ||
expect(html).not.toContain('background-color: red') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test assertion may be unreliable because it checks for the absence of background-color: red
in the HTML, but this exact string appears in the raw CSS content that should be displayed on the page. When the feature works correctly, the raw CSS is shown as text content rather than being applied as styles.
Consider using a more specific assertion that differentiates between CSS being applied versus displayed as text. For example:
- Check for CSS being applied using computed styles in a browser test
- Look for the string in a specific context (like within a
<pre>
tag) - Check for the absence of a style tag containing this CSS rule
- Verify the page doesn't visually have a red background using screenshot comparison
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
import { redirect as nextRedirect } from 'next/dist/client/components/redirect' | ||
|
||
export function redirect(path: string): ReturnType<typeof nextRedirect> { | ||
if (path === '' || path === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function parameter is typed as string
but the runtime check includes undefined
, creating a type inconsistency.
View Details
Analysis
The redirect
function is typed to accept a string
parameter: redirect(path: string)
, but the runtime validation checks for both empty string AND undefined
: if (path === '' || path === undefined)
.
This creates a type inconsistency because:
- TypeScript will never allow
undefined
to be passed to the function due to thestring
type - The
undefined
check is unreachable code that serves no purpose - If
undefined
values are expected, the type signature should reflect that
This could indicate either:
- The type is wrong and should be
string | undefined
- The undefined check is unnecessary dead code
Recommendation
Choose one of these approaches:
Option 1 - If undefined values should be accepted:
export function redirect(path: string | undefined): ReturnType<typeof nextRedirect> {
if (!path || path === '') {
throw new Error('Redirect path cannot be empty. This would cause an infinite loop.');
}
return nextRedirect(path);
}
Option 2 - If only strings are expected (remove dead code):
export function redirect(path: string): ReturnType<typeof nextRedirect> {
if (path === '') {
throw new Error('Redirect path cannot be empty. This would cause an infinite loop.');
}
return nextRedirect(path);
}
What?
Fix raw-loader CSS imports to return raw source content instead of processed CSS styles.
Why?
Currently, when users import CSS files with raw-loader (e.g.,
require('!!raw-loader!./foo.css')
), Next.js still processes them as CSS and applies the styles to the page. This breaks the expected behavior where raw-loader should return the raw source content as a string.How?
?raw
,?raw-loader
,!!raw-loader!
'asset/source'
for raw-loader imports to return raw contentfixes #82000