Tailwind classes generated from runtime template literals missing in production build (Next.js / React)
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
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
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
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
What is the recommended way to ensure Tailwind retains dynamically constructed classes in production builds without enumerating every variant manually?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development53% match
Next.js production build omits Tailwind classes generated from className arrays joined at runtime
Solved1 answer - Web Development41% match
Tailwind JIT classes stripped in Next.js production build when using dynamic template literals for class names
Open0 answers - Web Development37% match
Tailwind JIT utilities missing in SvelteKit production build when class names use runtime template literals
Open1 answer - Mobile21% match
React Native Android release APK crashes on cold start only when R8 minification is enabled — index.android.bundle missing at runtime
Open0 answers