-
-
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
Global styles or scripts #3127
Comments
I think another way is to add it to |
Related: #1530 |
@bluwy Yep SK doesn't process app.html - already tried |
cant you just add a style tag inside your __layout.svelte to head with vanilla js on first render? |
that wouldn't solve any of the issues I brought up @Kapsonfire-DE |
From what I See , the only thing which isnt covered up is the js part. Otherwise you can add the global CSS in Layout and reflect it to the head. This ist permanent and dont have the other mentioned issues.
Holen Sie sich Outlook für Android<https://aka.ms/AAb9ysg>
…________________________________
From: GHOST ***@***.***>
Sent: Monday, January 3, 2022 1:15:20 AM
To: sveltejs/kit ***@***.***>
Cc: Dennis Kaspar ***@***.***>; Mention ***@***.***>
Subject: Re: [sveltejs/kit] Global styles or scripts (Issue #3127)
that wouldn't solve any of the issues I brought up @Kapsonfire-DE<https://github.com/Kapsonfire-DE>
—
Reply to this email directly, view it on GitHub<#3127 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AP7SMRHACALFCK3NAQ3SPV3UUDTBRANCNFSM5K6EJEUA>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
If you read my original post then you will see why layout related styling isn't optimal. @Kapsonfire-DE |
What I'm missing? If you reflect the style in the head (outside of svelte) it wont be removed. Nothing will flicker, resets wont remove it and so on. The Idea to put this in in __layout and reflect it outside of svelte is that the css will be processed by SK - and multiple to the style will not be loaded multiple times since its cached |
This is very much wanted 👍 |
@Kapsonfire-DE While putting styles in your |
I didnt talk about app.svelte. |
Again even if you can make that work (at a initial guess it would be additional script(s)) it's terrible DX. Would love to know what your root issue is with this proposed solution @Kapsonfire-DE |
It would be great if app.html could just be processed by Vite in some way after SvelteKit is done with it. Vite already handles all the bundling etc. properly and works the way you would expect if you for example include a Typescript file instead of a Javascript one. My specific use case is for pages with |
I have updated my post to add in that ideas @ratorx Sorry for the mis-tag @borntofrappe - github mobile being slow |
Would like to see a solution to this as well. I have a naive question between these two proposed options:
Would choosing At the moment I'm building a single
It gets rid of my FOUC, but of course the css file is large since there is no tree shaking. The whole approach feels clunky, and |
@bseib Hopefully the FOUC issue at this point is close to nothing, you will always get it in dev mode but in production hopefully it's limited to slower connection (not much kit can do about this anyway and when building static or ssr the issue shouldn't be present since it should be loading styles in the head of the page). Yes putting the global ts/js file would mean you put the scripts inside of that with with a import statement, this is how it's done in vanilla svelte + vite projects for example. We could even combine the global style and global js so that you wouldn't need to do any importing and there is a content seperation. If you are currently already building your css file then you should put it in static and link to it in your app.html as then it will be global, if you don't wish to do this you might as well get rid of your extra build step as svelte kit can handle this for you Code splitting global styles doesn't make sense as even if you wanted to strip styles that don't apply then it would essentially no longer be global but scoped to your enitre app, which while that is a interesting concept wouldn't be global styling |
Today I'm having the same problem to apply a .scss file globally to reset the css. It would be very important to have a way to apply styles and scripts globally. |
Hi all, I'm a little late to the party, but hopefully there's still a lot of fun to be had ^^. I just created my first SvelteKit skeleton project and currently have my global CSS reset in
I suppose this is fine for simple projects, but what if I want to replace |
Had the same problem, no global.css found. Changed the link in app.html to ‘%svelte.assets%/css/global.css” for dev path static/css/global.css and its working. |
I worked around this issue by doing the following:
// src/App.svelte
<slot />
<style lang="scss" global>
@use './global.scss';
</style>
// src/routes/index.svelte
<script lang="ts">
import App from '../App.svelte'
</script>
<App>
<p>content</p>
</App> The main downside is that you have to import It would be nicer if SvelteKit provided a way to just have a global scss file. |
People usually (if not always) import global CSS inside <html>
<body>
<Layout>
<main> Why are we setting CSS for Also, if you have A layout is a visual representation of a page, it shouldn't be responsible for setting variables or resetting css among other things. I think we need an entrypoint for global CSS that is outside of layouts, for example an option inside const config = {
kit: {
'globalCss or cssEntrypoint': 'src/lib/styles/index.css',
}
} I tried using Having |
Don't forget that the solution must support Sass/Less as well |
that file can be read and preprocessed |
I found this article and it really works. |
Someone may have already mentioned this but the best way to get global styles in my experience is to include something like this in your __layout.svelte:
|
Just want to add, for anyone using the |
I have been facing this issue while trying to import an external dependency under
What I have realised is that though vite handles this without any issue. However, since sveltekit takes over html processing, it throws For the time being I have linked external dependency file from node_modules to my static folder and then linking it with script tag inside Would be great if svelte kit allows vite to preprocess |
Heads up: Under the new layouts proposal, the root |
That will be awesome. @dummdidumm Currently all the scripts are loaded by sveltekit as Currently I am using |
FOUC should not be possible for styles imported by the root layout, since those styles are injected into the document (as a Prior to #6174 it was possible to end up in a FOUCy situation because you might land on a page that bypassed the root layout. That's no longer possible — under the new system, every page inherits from the root layout, which means it's a perfect place to add global scripts and styles: <!-- src/routes/+layout.svelte -->
<script>
import '$lib/javascript-that-should-run-before-hydration.js';
import './global.css';
import { afterNavigate } from '@sveltejs/kit';
afterNavigate(() => {
// this will run after every single navigation, making it an
// ideal place to put e.g. analytics tracking code etc
});
</script> Note that I'm pretty sure this issue can therefore be closed — any objections? |
The problem with FOUC still exists for a SPA, as was outlined in this issue: #1948 If I create a new SvelteKit project and set Reproduction: https://github.com/ebeloded/sveltekit-spa-global-styles |
That's not FOUC. FOUC means content is present but unstyled, and results in a jarring transition. This is just a blank page showing until the content and styles are rendered simultaneously. If you don't want a blank page, don't use an SPA. Looks like we can close this issue — thanks all |
Describe the problem
Currently in Svelte Kit the recommended way to import global styles or scripts is in your root layout, this is a problem for the following reasons:
Describe the proposed solution
I was reading up in the discord and issues here and I believe one of the following is the best solution:
A global js/ts file and global css/scss file
this would be my initial guess at a solution and it was even shown here Allow inclusion of global styles #1948 but the downsides are that users might want more than provided options such as less or another javascript superset that isn't provided and adding a option might add extra complexity
A global js/ts file
Vite already allows for this easily and can just be treated as a secondary entry point I guess. This is already what you do in vanilla vite, for example on my personal site which is currently in routify
A global svelte file
This might be familiar for those using vanilla svelte, having a App.svelte (this case would probably be better for a global.svelte file). This can be where you import your global styles but also global scripts and logic. Again this is how things are done on sites in Routify, for example global keybinds
Processing app.html
It might be also possible to process the scripts and links inside of app.html and have this the place your global files are placed (processing is necessary since the script or style might be in a superset such as typescript or sass)
Alternatives considered
Writing a script or vite plugin to do this for me but this is just bad for the community
Importance
i cannot use SvelteKit without it
Additional Information
No response
The text was updated successfully, but these errors were encountered: