Web Development
VERIFIED FIXES

Next.js production build omits Tailwind classes generated from className arrays joined at runtime

tildatech
1mo ago
Environment

Environment

Next.js 13 app using Tailwind CSS JIT (tailwindcss 3.x). Components build className lists as arrays (['text-sm', sizeClass, colorClass].join(' ')) with sizeClass and colorClass computed from props at runtime. Production build (next build) served on Vercel; local dev styles fine.

Symptoms

Symptoms

In production several components render with correct class attribute values in DOM but no CSS rules apply — e.g., 'text-lg text-red-500' present in markup but corresponding Tailwind utilities not found in compiled CSS file. Visual regressions only in production build.

Observations

Observations

Tailwind output CSS size smaller than expected. tailwind CLI's purge scanning log does not list dynamically constructed class names. Adding static class strings fixes the styles. Using a safelist with individual classes works but is unwieldy for combinatoric variants.

Already attempted

Already attempted

Added safelist regexes to tailwind.config.js (worked but generated large CSS). Tried using classnames library with template literals; still not picked up. Ensured content paths include all components. Rebuilt and cleared caches.

Question

Question

Why are classes generated from joined arrays missing from production CSS and what reliable approach preserves Tailwind utilities without enormous safelist bloat?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
pixelpelle Answer
user
Posted 1mo ago • 0 confirmations

Tailwind removes classes it can't see at build time. Instead of joining arrays of computed strings, switch to a small static mapping so the compiler sees explicit strings: const size = {sm:'text-sm',lg:'text-lg'}; <div className={${size[props.size]} ${color[props.color]}}>. If you truly have many valid combos, add targeted regex safelist entries in tailwind.config.js that match only the variants you need (not a catch-all), then rebuild and verify by inspecting the generated CSS or rendering the page. To roll back, revert to the previous CSS/safelist; keep changes minimal to avoid huge CSS payloads.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Tailwind purge cannot detect class names built dynamically via array.join at compile time 95%
tailwind.config.content paths missing some component files at build time 40%
Production minifier or postcss removing unused classes incorrectly configured 20%

Summary

Tailwind's purge/scanner finds only literal class name strings at build time. When you build class names dynamically (array.join, string concatenation of computed values, or runtime mapping), the scanner cannot infer the set of utilities to include, so they are removed from the final CSS. Safelisting works but can balloon file size. Better patterns are to keep the set of possible class names statically analyzable (mapping objects, conditional expressions with explicit strings) or generate a limited safelist with regex that matches the valid variants. Alternatives include using twin.macro, cx helpers, or using the new JIT with explicit safelist generation.

Suggested Fixes

Diagnostic step 1
Replace runtime array.join with a mapping helper that returns known class strings. Example: const sizeMap = { small: 'text-sm', large: 'text-lg' }; className={`${sizeMap[size]} ${colorMap[color]}`};
Diagnostic step 2
If mapping isn't possible, create a concise safelist with a regex that only matches the finite set you actually use, e.g., safelist: [/^text-(sm|md|lg|xl)$/, /^text-(red|blue|green)-500$/].
Diagnostic step 3
Run tailwindcss --content <paths> --output debug.css in a dev environment to confirm which classes are emitted.

Continue troubleshooting

Matched from the title, description and tags