Issues encountered with certain Tailwind styles functioning improperly when deployed in a production environment with Next.js

It seems that there are some style issues occurring in the production build hosted on Netlify for a specific component. The problematic component is a wrapper located at ./layout/FormLayout.tsx. Below is the code for this 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>
    )
}

This wrapper is used in the following component:

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

Below are some of the configuration files being used:

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: {},
  },
}

An example demonstrating the issue can be seen in the following images:

https://i.sstatic.net/29MPf.png

https://i.sstatic.net/ddJOb.png

The build command used is

next build && next export
, and Netlify deploys the directory /out.

You can find all the code here on GitHub

Answer №1

If you come across this message later on, simply update the Tailwind config by including the path to any new folder in the purge array like so:

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

December 2021 Update:

Post-TailwindCSS version 3, there's a slight change in the configuration file format. The updated setup would look like this:

module.exports = {
  content: [
    "./src/**/*.{js,ts,jsx,tsx}",
    // Include additional paths here
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

NextJS App Directory

In NextJS 13, TailwindCSS is fully compatible with just a simple inclusion as a dependency.

module.exports = {
  content: [
    // using ./src/ directory
    "./src/**/*.{js,ts,jsx,tsx}",
    // using ./ directory
    "./app/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
    // Add more paths as needed
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

Answer №2

I encountered a similar problem.

I made the following modifications :

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

to this instead :

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

and voila! Issue resolved. strange situation

Answer №3

In my situation, I encountered an issue where a specific directory was not included in the content property of the tailwind.config.js file. As a result, any component within that directory was not being analyzed by tailwind.

The problem arose when I added a new component to a folder named providers, but forgot to include it in the content list.

To resolve this issue, I updated the content list to:

 content: [
     // ...
     "./components/**/*.{js,ts,jsx,tsx}",
     "./providers/**/*.{js,ts,jsx,tsx}",  // crucial line
 ]

By making this change, the providers directory was successfully recognized by tailwind and components within it were now being checked as well. Therefore, any directory housing components utilizing tailwind utility classes should be included in this configuration list.

Answer №4

If you are transitioning to NextJs version 13, don't forget to make changes in your tailwind.config.js file by including the app directory as shown below:

module.exports = {
  content: [
    './pages/**/*.{js,ts,jsx,tsx}',
    "./app/**/*.{js,ts,jsx,tsx}",
    './components/**/*.{js,ts,jsx,tsx}'
  ]
};

Answer №5

the issue I encountered was due to my incorrect use of the address in the purge section of the tailwind.config.css

Answer №6

If you're facing problems with your DOM updates not reflecting correctly, make sure to check for consistency in casing between imports and file names!

For example, Header.js is not the same as header.js.

Answer №7

I experienced an issue where sporadically, the deployed site was missing a responsive attribute class that I had changed from md:w-1/4 to sm:w-1/4. Interestingly, adding a new path (even if fictitious) to the purge: [... array and redeploying resolved the issue for me.

Answer №8

This puzzle had me stumped - it all came down to the sequence of the string

Success with {js,ts,jsx,tsx}, failure with {ts,tsx,js,jsx}

Snippet from tailwind.config.js:

export const content = [
  "./pages/**/*.{js,ts,jsx,tsx}",
  "./components/**/*.{js,ts,jsx,tsx}",
  "./app/**/*.{js,ts,jsx,tsx}",
  "./src/**/*.{js,ts,jsx,tsx}",
];

Answer №9

Personally, I found success by removing the .next folder and then running yarn build again.

It could potentially be a caching problem causing the issue.

Answer №10

Consider incorporating the useEffect() hook and importing "tw-elements" in your _app.js file:

import { useEffect } from "react";
import Layout from "../components/Layout"

import "../styles/globals.css";

function MyApp({ Component, pageProps }) {

    useEffect(() => {
        import('tw-elements');
    }, []);

    return (
        <Layout className="scroll-smooth transition-all">
            <Component {...pageProps} />
        </Layout>

    )
}

export default MyApp

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for changing the background color depending on the value

I am creating a table displaying the results of a tournament. Each team's final placement and original seeding will be listed. I plan to include a small bubble next to each team showing how their final placement compares to their initial seeding. To v ...

Ways to reload the page following the submission of a form

I implemented a fade dialog to present a form, and successfully submitted the form using ajax to create a new record in the database. Now, I am facing an issue where I cannot refresh the page after the ajax call finishes. Despite attempting to use JavaScr ...

"Troubleshooting the lack of functionality in nested ajax calls, click events, or event

Initially, a navigation click event was created: $('#inner-navigation li a') .on('click', function (e) { e.preventDefault(); AjaxNavUrl.checkURL(this.hash); }); This event triggers an ajax call and respo ...

How can I remove the color of a button in jquery after it's been clicked?

<button style="background-color:lightblue" id="extractv"> <b> Extract<br>v </b> </button> ... $("#extractv").click(function () { $("#extractv").removeAttr("style"); }); Upon clicking the button, my ...

Choose not to alter the display value during keyup and keydown events, while still triggering the change event

$(function(){ $(".cls_user_details select").keyup(function(){ $(".cls_user_details select").val("Profile"); }) }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cls_user_setti ...

What is the best way to send HTML tag content to mark.js and delimit them with a space or comma?

I have been utilizing the mark.js library to highlight keywords on a webpage, and it's been working well. However, I now need to insert an extra space or a comma after each tag such as h1, h2, etc. Initially, I thought about using a loop like the one ...

Conceal an absolutely positioned element outside its relatively positioned parent

I have a relative position parent element, <div style="position:relative" id="a"> and within that div, I'm creating some absolute positioned elements using jQuery, <div style="position:absolute;right:0" id="b"> These elements are anima ...

The HTML attribute "hasbox" specifies the presence of a box within the element

I am currently working on resolving some rendering issues specifically in IE9 and have encountered a tag attribute that I am not familiar with - hasbox. Upon further investigation, it seems like IE is injecting this attribute at runtime as it does not app ...

Is the use of ReactDom.render in <iframe> triggering a DOMException stating that the operation is unsafe?

When I run my code, it throws an error that says DOMException: The operation is insecure.. The issue seems to originate from the following initialization code: import React from 'react'; import ReactDOM from 'react-dom'; import App fro ...

Customize the HTML language attribute in NextJS

After recently transferring a website from React to NextJs, the site is now up and running in Sweden and Norway with the URLs; domain.com/se and domain.com/no During the migration, we decided to use se and no as locales. (next-18next.config.js) module.ex ...

Is it possible to incorporate a PHP line within a Handlebars context array?

I'm currently utilizing handlebars along with a handlebars register helper to iterate through an array that I've included in my context. The result is then displayed as an unordered list (ul). However, I'm facing an issue when attempting to ...

Columns that adapt to different screen sizes while maintaining a fixed aspect ratio, limited to a maximum of two columns

I'm facing a challenge trying to blend a few elements together: My goal is to have two columns take up 100% of the browser width with a height that adjusts relative to this width. I want these columns to switch to one column when the viewport is sma ...

Refreshing SQL Server data using an HTML form

In the table below: <table id="skuTable" role="grid"> <thead> <th class="skuRow">Order</th> <th>Fab. Date</th> <th class="skuRow">Norder</th> <th>Color</th> ...

Learn how to invoke a method of a particular subchild in the component hierarchy using React

As I delve into the world of React, a theoretical problem caught my attention. It involves figuring out how to solve a particular issue within a set of components. Let's say we have the following components: class Universe extends React.Component { ...

I was able to resolve the fixed position issue of a button on Safari for iPhone 3G

Here is the setup I have on jsfiddle: http://jsfiddle.net/zZDqH/ Currently, there is a button fixed at bottom:0px. You can scroll up and down the page, and the button will stay at the bottom of the page. However, when testing in Safari on an iPhone 3G ( ...

`Get the height of a specific element within a FlatList using the index property in React Native`

I'm currently exploring a workaround for the initialScrollIndex issue in FlatList that is not functioning correctly. I attempted to use getItemLayout to address this, but encountered a new problem - my elements inside the FlatList have varying heights ...

Why Isn't the Element Replicating?

I've been working on a simple comment script that allows users to input their name and message, click submit, and have their comment displayed on the page like YouTube. My plan was to use a prebuilt HTML div and clone it for each new comment, adjustin ...

Configure environment files for different environments in React using webpack: .env, .env.test, and .env.production

Here is the package.json setup for my React app: "scripts": { "dev": "webpack-dev-server --mode=development", "prod": "webpack --mode=production" } I am looking to set environment variables in .env, .env.t ...

Issue with Cross-Origin Request in ReactJS and Java: Preflight request failing access control validation

While working on fetching data from a Java backend using axios, I encountered a CORS issue leading to failure (Refer to the attached screenshot). Interestingly, I found that if I remove the AuthToken part in my axios file, it works perfectly with a status ...

The slowness is being caused by replicating the state in the reducer

My reducer is causing some slowness in my application. Upon investigation, I have pinpointed the step that clones/copies a portion of the state as the one causing the slowdown. export default function itemReducer(state = initialState, action) { case IT ...