CSS Configurations

1. cssChunking

cssChunking is an experimental feature in Next.js that controls how CSS is split, ordered, and loaded. It's designed to optimize performance by reducing the amount of CSS the browser has to download and parse for each route.

In large applications, CSS can grow significantly. By default, without chunking, all CSS may be bundled together. This results in large initial page loads, even if many styles aren't used on the first render.

Chunking reduces this overhead by splitting CSS based on usage and allowing route-level or component-level styles to be loaded only when needed, reducing render-blocking resources and improving load performance.

Instead of shipping a single large CSS file with styles for the entire application, CSS chunking enables per-route CSS delivery, allowing users to download only what's necessary.

// next.config.ts import type { NextConfig } from 'next' const nextConfig = { experimental: { cssChunking: true, // true(default), false, or 'strict' }, } satisfies NextConfig; export default nextConfig

🔧 Available Options:

Use the 'strict' mode if:

With 'strict', the import order is preserved, ensuring CSS dependencies load correctly—at the cost of generating more chunks and requests.

This experimental feature works best with CSS Modules and CSS-in-JS patterns.

Chunking logic is based on module import graphs—if import paths are inconsistent, results may vary.

Some global styles (like third-party libraries) may not benefit from chunking.



2. inlineCss

The inlineCss configuration in Next.js is an experimental feature that enables inlining CSS directly in the <head> of your HTML document, rather than linking to external .css files.

inlineCss changes how CSS is delivered to the browser by replacing <link rel="stylesheet"> tags with <style> tags in the HTML head. This means CSS is directly embedded in the HTML, reducing the need for additional network requests.

//next.config.ts import type { NextConfig } from 'next' const nextConfig: NextConfig = { experimental: { inlineCss: true, }, } export default nextConfig;

With inlineCss enabled, all global and scoped CSS is included in <style> tags in the HTML head. No external stylesheets are loaded on initial load. When navigating between static pages, CSS will fall back to using <link> tags to avoid duplication.

Inlining CSS can dramatically improve performance in certain scenarios:

Inlining isn't always a good idea:

💡 Global Only: You cannot inline CSS on a per-page basis—it's either all inline or all external.

🌀 Duplication: On the first page load: CSS appears inline in the <style> tag. It is also included in the React Server Component (RSC) payload.

🔄 Navigation Behavior: When navigating between statically generated pages, Next.js falls back to external CSS to avoid duplicating styles in each HTML page.

⚙️ Production Only: This feature is disabled in development mode. It only works in production builds.

💡 Best Practices:



3. sassOptions

The sassOptions configuration in Next.js allows you to customize how the Sass compiler is used in your application. This is useful when you want more control over how styles are processed, such as injecting global variables, customizing import paths, or specifying which Sass implementation to use.

You can:

// next.config.ts // additionalData injects the given Sass code (e.g., defining $var: red) into every .scss or .sass file automatically. // implementation: 'sass-embedded' tells Next.js to use the Embedded Sass compiler, which can offer performance improvements or different behaviors from the default. import type { NextConfig } from 'next' const sassOptions = { additionalData: ` $var: red; `, } const nextConfig: NextConfig = { sassOptions: { ...sassOptions, implementation: 'sass-embedded', }, } export default nextConfig;

Although only implementation is typed in Next.js, you can still use any valid Sass options, including:

//This automatically includes styles/globals.scss in all stylesheets. sassOptions: { additionalData: `@import "styles/globals";` } //Now you can do @import "buttons"; instead of @import "../../styles/buttons";. sassOptions: { includePaths: ['styles', 'components'] }

✅ Good to Know: Only the implementation key is typed in Next.js. Others work, but are not autocompleted or type-checked.

⚠️ Path Issues: Be careful with includePaths—it may require absolute paths (path.join(__dirname, 'styles')) depending on your project structure.

🧪 Embedded Sass: The sass-embedded implementation is still evolving; check for compatibility or bugs with specific syntax/features.



4. useLightningcss

The useLightningcss configuration option in Next.js enables experimental support for using Lightning CSS  — a high-performance CSS parser, transformer, and minifier written in Rust. This option is part of the experimental configuration namespace and is not yet recommended for production environments due to its evolving nature.

Lightning CSS is a CSS toolchain developed by the creators of Parcel, designed to be:

When you enable useLightningcss, you're instructing Next.js to replace its default PostCSS-based pipeline with Lightning CSS for CSS transformations.

//next.config.ts //This minimal config tells Next.js to process and minify CSS using Lightning CSS during the build. import type { NextConfig } from 'next'; const nextConfig: NextConfig = { experimental: { useLightningcss: true, }, } export default nextConfig;

🔧 Features of Lightning CSS

⚠️ This feature is still being tested. APIs and behavior might change in future releases. Some PostCSS plugins or custom PostCSS config (e.g., Tailwind with plugins) may not work with Lightning CSS. There is no fine-grained control or plugin system like PostCSS yet. In some versions, Lightning CSS may not be used during next dev, only during production builds.

💡You might want to try enabling useLightningcss if:

❌ Avoid enabling useLightningcss if: