[v4] vite plugin + svelte - inconsistent output style tags causing misaligned CSS layer order #16852
Unanswered
vnphanquang
asked this question in
Help
Replies: 1 comment 1 reply
-
Do you have a global CSS file anywhere? Typically, you should have
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Hi all, I bumped into an issue today with a vite, svelte+kit, tailwindcss setup, where the style tag containing tailwindcss build output where placed after another style tag after hydration, making the CSS layer declaration unreliable.
This only happens in a project I'm working on with a relatively complex setup, and I couldn't reproduce in a minimal setup. Nor do I know if this is specific to
@tailwindcss/vite
, or something to do with svelte / vite, or isn't a real problem at all. So this is my best attempt to document my findings in case someone runs into similar situation or has some better insight. Sorry in advance for the lengthy text.Reproduction
Imagine this setup:
where
app.css
is the typical entry CSS containing@import 'tailwindcss'
, andButton.svelte
is:Notice
@layer components
. In my particular setup, after hydration kicks in, the CSS output of theButton
component above were placed before the style tag containing tailwind CSS build output, something like this:So now,
@layer components
were used first before the actual layer declaration by tailwind, hencecomponents
has lower priority thanbase
, causing clash with tailwindcss preflight in this particular use case, becausebutton { color: inherit }
in thebase
layer gets applied instead.tailwindcss/packages/tailwindcss/preflight.css
Lines 246 to 260 in 66ef77c
Further Discussion
Important
Again, this particular problem only happens after hydration, in development for me. In other words, if JS is turned off (and the page is pre-rendered), the correct order is observed.
However, this suggests that the order for these style tags in build output is non-deterministic, or at least there are pitfalls that would cause similar problem reported above. One example would be loading another style tag from a third-party that uses the same CSS layers. And if so, I think this should be communicated somewhere in the docs (if it isn't already - I couldn't find it)
Workaround
For now, my workaround is to add the layer declaration manually before everything else, inline in the head tag for example:
Environment
Output of
npx envinfo --system --npmPackages svelte,vite,tailwindcss,@tailwindcss/vite --binaries --browser
:Beta Was this translation helpful? Give feedback.
All reactions