Next.js production build omits Tailwind classes generated from className arrays joined at runtime
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
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
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
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
Why are classes generated from joined arrays missing from production CSS and what reliable approach preserves Tailwind utilities without enormous safelist bloat?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development36% match
Tailwind classes generated from runtime template literals missing in production build (Next.js / React)
Open0 answers - Web Development32% match
Tailwind JIT classes stripped in Next.js production build when using dynamic template literals for class names
Open0 answers - Web Development22% match
Tailwind JIT utilities missing in SvelteKit production build when class names use runtime template literals
Open1 answer