tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

[JIT] Tailwind does not update classes after saving in NextJS

Open mwarger opened this issue 5 years ago • 36 comments

What version of Tailwind CSS are you using?

2.1

What build tool (or framework if it abstracts the build tool) are you using?

"next": "10.1.3", 

What version of Node.js are you using?

v14.15.1

What browser are you using?

chrome

What operating system are you using?

macOS for M1

Reproduction repository

https://github.com/mwarger/next-tailwind-jit-repro

Describe your issue

I've also put reproduction instructions in the repro repo.

It seems that changes made while in jit mode are not being picked up. As it stands now, with jit mode enabled, only initial styles are applied, and kept. Any changes or additions result in loss of styles. Please advise. Thanks!

mwarger avatar Apr 15 '21 13:04 mwarger

See #4073

Same issue.

kanidjar avatar Apr 15 '21 16:04 kanidjar

Creating a .env file and setting TAILWIND_MODE=watch fixed this for me. I'm not sure why this should be required, but I can at least move forward for now...

mwarger avatar Apr 15 '21 16:04 mwarger

I can confirm having this bug in NextJS intermittently with no way to tell if it's going to occur or not. (Apparently unrelated to #4073).

It does occur (without any visible error) if I include a Tailwind plugin that is missing or PostCSS configuration file that is invalid. But it also occurs for no apparent reason, and with no obvious output that's different.

JIT not working -- no JIT message, webpack warning: image

JIT working -- JIT message, no webpack warning: image

These are the same project with npm run dev (next start).

Windows 10 x64, tailwindcss 2.1.1, next 10.1.3 using webpack 5 option (I updated tailwind to 2.1.2@latest yesterday to monitor the issue further, it could have been fixed) (I'm also going to update to next 10.2 today)

The error will come & go while working in Next.js -- seems to be working fine, then suddenly JIT fails without any visible output. Restarting the next dev server will generally produce the right styles one time but is no guarantee JIT will start working.


Here's the list of everything I tried, but nothing seemed to make a difference:

  • Remove all Tailwind plugins from tailwind.config.js
  • Add variants: ["responsive"] to tailwind.config.js
  • Turn it off & turn it back on (this one works! lol)
  • Create .env file with TAILWIND_MODE=watch per @mwarger
  • Swap npm run dev from next dev to env TAILWIND_MODE=watch NODE_ENV=development next dev

I think @Danon is on the right track with there being some type of a race condition (see #4073). Nice job with that research! 👍

Environment: node v15.14.0 windows x64 chrome latest stable

DoctorDerek avatar Apr 30 '21 20:04 DoctorDerek

In my case, the problem is temporarily solved by changing the file tailwind.config.js after starting the project. tailwind-trigger

PainteR avatar May 02 '21 15:05 PainteR

Interesting, thanks for the tip!

For me, it also works normally with webpack 4, i.e. turning off the webpack 5 option in next.config.js

On Sun, May 2, 2021, 10:38 AM Ivan Reshetnikov @.***> wrote:

In my case, the problem is temporarily solved by changing the file tailwind.config.js after starting the project. [image: tailwind-trigger] https://user-images.githubusercontent.com/17311456/116818714-67724f00-ab75-11eb-8e63-595c3c2845a3.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tailwindlabs/tailwindcss/issues/4081#issuecomment-830828257, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFZ3D2VONYMFNNGQXJOFYTTLVWXXANCNFSM427P472A .

DoctorDerek avatar May 02 '21 17:05 DoctorDerek

Dropping in to also comment how JIT will intermittently stop working when using NextJS. Just started a new project today, Tailwind with JIT was working flawlessly, then all of a sudden my new css classes weren't being applied. Restarting the dev server and restarting VSCode did not fix the problem either

EDIT: I can also confirm that changing text in tailwind.config.js also temporarily fixes the problem

uncvrd avatar May 04 '21 07:05 uncvrd

Same here! 🤚 (Next 10.2 with Webpack 5) Seems only occur with Webpack 5 for me.

simonedelmann avatar May 04 '21 09:05 simonedelmann

Same problem here (Next 10.1.3 and Next 10.2). The problem occurs with both Webpack 4 and 5 for me and none of the above suggestions like the watch mode environment variables have helped.

sct avatar May 04 '21 13:05 sct

Had to do this to make JIT work:

TAILWIND_MODE=watch webpack serve

Akryum avatar May 04 '21 18:05 Akryum

Maybe watch mode should be enabled if NODE_ENV !== 'production'? It's common to not specify NODE_ENV=development

Akryum avatar May 04 '21 18:05 Akryum

Reverting to webpack 4 improves but doesn't fix the reliability issue for me. I haven't tried the edit config file when it stops working suggestion.

Guillaume, could you elaborate? You're using webpack serve instead of next dev?

DoctorDerek avatar May 04 '21 19:05 DoctorDerek

I was having the same issue of JIT not updating the CSS classes with barebone webpack 5.

Akryum avatar May 04 '21 20:05 Akryum

I've also had this happen to me seemingly randomly in my Nextjs project (Webpack5 and I believe Webpack 4 as well). Saving changes just won't update CSS. For instance, I applied w-32 and h-32, and started changing those values and was real confused why it wasn't working. I disabled JIT, everything started working, then re-enabled JIT and it was fine.

BButner avatar May 05 '21 13:05 BButner

I had the same issue, yesterday and today.

And my way to fix it was:

  1. Stop dev server
  2. Comment out mode: 'jit' line in tailwind.config.js
  3. Start dev server
  4. Stop dev server
  5. Uncomment mode: 'jit'

✨ And it works ✨ Follow me for more "Restart tutorials" 🤣 jk

eddsaura avatar May 07 '21 11:05 eddsaura

Just cloned down the reproduction — to get it working I needed to add TAILWIND_MODE=watch to the start script. Internally Tailwind will do this automatically if NODE_ENV=development so I guess for whatever reason with this setup NODE_ENV isn't set by default.

Going to close as resolved but if anyone else is having issues feel free to open new issues with your own reproduction. Eventually we will make all this TAILWIND_MODE stuff less clunky — unfortunately really no easy way to detect if the user is running a watch process or just doing a one-off build though so I suspect it will always require some kind of configuration if we can't depend on NODE_ENV.

adamwathan avatar May 10 '21 15:05 adamwathan

Maybe it would be a NODE_ENV !== 'production' so that it works if NODE_ENV isn't set?

Akryum avatar May 10 '21 16:05 Akryum

And then it can print out a warning message like

Tailwind JIT is in watch mode because NODE_ENV environment variable isn't set.

Akryum avatar May 10 '21 16:05 Akryum

In a lot of development environments NODE_ENV is not explicitly set for development, and set to production for production building.

Akryum avatar May 10 '21 16:05 Akryum

Thanks for looking into this!

Here's what I'm using now for Webpack 5 with Next.js on Windows 10: package.json

"dev": "env TAILWIND_MODE=watch NODE_ENV=development next dev",

or env TAILWIND_MODE=watch NODE_ENV=development npx next dev

npm run dev image

DoctorDerek avatar May 11 '21 02:05 DoctorDerek

I still have this problem once or twice a day.

But my config is mega simple, I have nothing changed, it is out-of-the-box with nextjs.

But yeah, from time to time I just have to comment the line mode: 'jit' and reset dev server. It is strange.

eddsaura avatar May 20 '21 13:05 eddsaura