-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure deterministic SSR builds in @tailwindcss/vite
#13457
Conversation
It looks like when running `astro build` we only run this `build` step and not the `dev` step where we already use the `waitForRequestsIdle` code. Adding this to the `build` part as well does generate the correct result.
I don't think this is the right fix.
Even if Our setup works in a normal Vite project, and even a basic Astro project. The problem in the project in the issue is caused by a few things:
I'm not sure yet if this is a problem specific to Astro or is in Vite itself. I need to do some more investigating. |
Hi. I'm the person who filed the issue. I did a bit more testing with my project and removed the The issue still seems to persist when using the latest version of The screenshot below shows that I commented out the component usage but I also completely deleted the file. https://github.com/Brian-Pob/tailwind-v4-vite/tree/alpha-11-bug-2 |
And please feel free to reach out to me in the Tailwind Discord if you would like me to try anything or run any tests. |
@Brian-Pob Importing a Svelte component also seems to do it. Even then, it appears that when a component statically imports images, the build's CSS is non-deterministic. Sometimes our plugin appears to run at the appropriate time and sometimes it does not. fwiw, I stress-tested this over ~500 runs and this did not happen for a vanilla Vite installation. Only when astro components are involved. |
Notes for me later: I stripped down the astro build to just enable the following plugins:
My guess is that |
Something interesting I've discovered in my stripped down test is that anytime Which means if we delay resolution to make sure the CSS file is transformed after all images things start working. In my stripped down reproduction a delay of ~100ms is sufficient. In the original reproduction that was provided the delay has to be a good bit longer. So, in the world of "how and why does this work", this plugin solves the issue: {
name: "delay-the-css",
enforce: "pre",
async resolveId(id) {
if (!id.includes(".css")) return;
await new Promise((resolve) => setTimeout(resolve, 3000));
},
}, I'm still investigating the root of the problem. I also tested this in a Vite + Vue project and could not reproduce the issue. Even when manually delaying images such that they were transformed after CSS files. |
Okay @RobinMalfait I figured this one out. During SSR there is a server with a module graph (when normally there isn't during a build) and we're deleting references to modules that don't exist. Normally this is fine but during an SSR build the module won't get reloaded unless its transform is handled after everything else. This is what's causing the problem — and the "after everything else" is the cause of the non-deterministic behavior. With this the problem should be resolved entirely. |
@tailwindcss/vite#build
waits for all files before generating the CSS@tailwindcss/vite
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense what you did here @thecrypticace, nice debugging job on the SSR stuff!
This PR fixes an issue where running
astro build
doesn't generate the correct CSS when using@tailwindcss/vite
. This issue only seems to happen when running a build, and not when running indev
mode.To fix this, we apply the same code (
waitForRequestsIdle
) as we do in theapply: "server"
step.Adding this to the
apply: "build"
step as well does generate the correct result.While this does fix the issue, I'm not 100% convinced that this is the correct solution /cc @thecrypticace.
Fixes: #13441