Skip to content

Conversation

naaa760
Copy link

@naaa760 naaa760 commented Jul 27, 2025

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?

  • Add webpack rules in CSS configuration to handle raw-loader imports before CSS processing
  • Support multiple raw-loader patterns: ?raw, ?raw-loader, !!raw-loader!
  • Set module type to 'asset/source' for raw-loader imports to return raw content
  • Add test fixture to verify the fix works correctly

fixes #82000

nehhhhha6 and others added 2 commits March 29, 2025 18:38
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.
@ijjk
Copy link
Member

ijjk commented Jul 27, 2025

Allow CI Workflow Run

  • approve CI run for commit: 46da4fd

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')
Copy link
Contributor

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) {
Copy link
Contributor

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:

  1. TypeScript will never allow undefined to be passed to the function due to the string type
  2. The undefined check is unreachable code that serves no purpose
  3. 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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Next.js still imports as css even when loaded with raw-loader
2 participants