Web Development

Tailwind classes generated from runtime template literals missing in production build (Next.js / React)

Environment

Environment

Next.js 13 production build on Vercel using Tailwind CSS JIT; components build className by joining arrays and template literals (e.g., ['bg-', variant, '-500'].join('')). Development server shows correct styles.

Symptoms

Symptoms

In production only, some components render with no Tailwind styles (buttons and badges lack background and text color). Dev builds display them correctly. The stripped classes are those constructed from runtime variables.

Observations

Observations

Generated CSS in production bundle lacks the class selectors for the missing components. Tailwind purge/scanner did not detect the dynamic patterns. Adding one explicit example of the class in a test JSX file temporarily prevents purging for that specific class.

Already attempted

Already attempted

Added safelist entries in tailwind.config.js for specific classes (worked but not scalable), tried embedding classes in hidden spans (worked for subset), refactored some templates to explicit strings but many variants would be cumbersome.

Question

Question

What is the recommended way to ensure Tailwind retains dynamically constructed classes in production builds without enumerating every variant manually?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Tailwind purgeer cannot detect classes created from runtime concatenation/template literals and removes them from production CSS 95%
Safelist configured but pattern too narrow or incorrectly formatted for production scanner 53%
A build-time minifier or transpiler rewrote template literals making scanner miss classes 37%

Summary

Tailwind's purge (content scanner) runs at build time and cannot detect class names built by runtime expressions or concatenated template literals. The typical remedies are: provide explicit safelist patterns that match dynamic possibilities, generate classes using a known finite map (object lookup) so the build-time parser sees the keys, or use the @apply directive in component CSS. Minification/build transforms that inline or mangle template strings can also hide patterns from the scanner; using string literals or explicit mappings is robust.

Suggested Fixes

Diagnostic step 1
Replace runtime concatenation with a mapping: create an object map like const bg = { primary: 'bg-blue-500', ... } and reference map[variant] so build-time analysis sees literal strings.
Diagnostic step 2
If mapping is impractical, add a safelist array in tailwind.config.js with regex patterns that cover the dynamic variants (e.g., safelist: [{ pattern: /bg-(red|blue|green)-500/ }]).
Diagnostic step 3
Avoid relying on ephemeral DOM-only injections; where possible use @apply in a component CSS file to define reusable classes that Tailwind compiles.

Continue troubleshooting

Matched from the title, description and tags