Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some Tailwind styles not working in production with Next.js

For some reason a few styles don't seem to be working in production build hosted on Netlify. This seems to only be happening on a single component. It's a wrapper located at ./layout/FormLayout.tsx (don't know if that changes anything). Here is the wrapper:

const FormLayout: React.FC<FormLayout> = ({ children, title, description }) => {
    return (
        <div className="w-screen mt-32 flex flex-col items-center justify-center">
            <div className="p-6 flex flex-col items-center justify-center">
                <h2 className="text-4xl font-semibold text-blue-400">
                    {title}
                </h2>
                {description && (
                    <h6 className="mt-4 text-md font-medium">{description}</h6>
                )}
                <div className="mt-12 w-max">{children}</div>
            </div>
        </div>
    )
}

and it's used here:

const Register: React.FC<RegisterProps> = () => {
    return (
        <FormLayout title="Register" description="Register with your email.">
            {/* form stuff. styles do work in here */}
        </FormLayout>
    )
}

Here are some of the config files:

tailwind config:

module.exports = {
    purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
    darkMode: 'class',
    theme: {
        extend: {},
    },
    variants: {
        extend: {},
    },
    plugins: [],
}

postcss config:

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Here is a graphical example of what is happening:

enter image description here

enter image description here

For my build command, I use next build && next export, and Netlify deploys the /out directory.

All the code is here via github

like image 809
Herbie Vine Avatar asked Mar 04 '21 22:03

Herbie Vine


People also ask

Does Tailwind work with Nextjs?

The quickest way to start using Tailwind CSS in your Next. js project is to use the Next. js + Tailwind CSS Example. This will automatically configure your Tailwind setup based on the official Next.

Is Tailwind CSS good for production?

Getting the most performance out of Tailwind CSS projects. Tailwind CSS is incredibly performance focused and aims to produce the smallest CSS file possible by only generating the CSS you are actually using in your project.

How do I add a Tailwind to next JS project?

Open the ./styles/globals. css file that Next. js generates for you by default and use the @tailwind directive to include Tailwind's base , components , and utilities styles, replacing the original file contents: /* ./styles/globals.

Why you should not use Tailwind CSS?

Tailwind forces an extra HTML-based abstraction layer for a CSS job. This is always going to be an anti-pattern. On balance, I think it's slightly worse than just using inline CSS – either via Styled Components or "vanilla CSS". At least inline CSS doesn't force me to memorize a bunch of new class names.


3 Answers

For anyone seeing this in the future, just add the path to any new folder in the purge array into the tailwind config like this:

module.exports = {
    purge: [
        "./src/**/*.{js,ts,jsx,tsx}",
        // Add more here
    ],
    darkMode: 'class',
    theme: {
        extend: {},
    },
    variants: {
        extend: {},
    },
    plugins: [],
}

December 2021 Update:

After TailwindCSS v.3, the config file is slightly different. The above configuration would be:

module.exports = {
  content: [
    "./src/**/*.{js,ts,jsx,tsx}",
    // Add extra paths here
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
like image 144
Herbie Vine Avatar answered Oct 18 '22 19:10

Herbie Vine


I had the same issue.

I changed these :

purge: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],

to these :

purge: ["./pages/**/*.js", "./components/**/*.js"],

and that's it. problem solved! weird issue

like image 44
alithecodeguy Avatar answered Oct 18 '22 19:10

alithecodeguy


In my case, it was a directory not being listed in the content property of tailwind.config.js file. so any component residing in that directory was not being checked by tailwind.

My new component was in a folder called providers, and it was not listed in content.

so after changing content list to:

 content: [
     // ...
     "./components/**/*.{js,ts,jsx,tsx}",
     "./providers/**/*.{js,ts,jsx,tsx}",  // the key was this line
 ]

The providers directory was also being checked by tailwind. So any directory which consists of a component that uses tailwind utility classes must be included in this list.

like image 23
Ali Baghban Avatar answered Oct 18 '22 18:10

Ali Baghban