-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Importing CSS file in route applies it on hover, and the style is not cleaned up when navigating away from the route #12933
Comments
Is there anything reasonable to do here besides document it? The many other times this has come up (e.g. #10595), our answer has been that you shouldn't be loading global styles from a component. @benmccann What exactly are you thinking would happen in 3.0? |
Right now workaround seems to be to use Adding support for the |
Pretty sure this is a |
The workaround is to use <script>
import style from '$lib/style1.css?url'
</script>
<svelte:head>
<link rel="stylesheet" href={style} />
</svelte:head> This will add and remove stylesheets as you switch in and out of pages using the above code. Also agreed with Vite behaviour since all modules become part of application state the moment they're loaded. Unless there's a way to 'unload' ESM, I don't think that behaviour should change. |
Note that this workaround will result in FOUC (for client-side navigation) and duplication |
I feel like we could probably at least catch this during build and issue a warning pointing to some documentation on the issue I believe we already store a list of which CSS files are used in which layout files. I would kind of expect that we could use that to detect which should be removed when navigating away from a layout, but maybe I didn't catch why that wouldn't work or we don't even want to go down the path of supporting global CSS in components in the first place |
Describe the bug
If you do something like
import '$lib/layout1.css';
in a +layout or +page, the CSS is incorrectly applied on hover (probably due to the preload functionality).Additionally when navigating away from the route, the style tag is not properly unmounted, leaving the CSS in place.
For reference, I also included a css import using svelte-preprocess, which shows that scoped CSS does work as intended.
This is a question that has come up many times in the Svelte Discord, and is usually unexpected by the user.
svelte-css.mov
Reproduction
https://github.com/khromov/sveltekit-css-import-repro
npm i && npm run dev
Logs
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: