Allow inclusion of global styles
See original GitHub issueDescribe the problem
Currently the recommended way to include global styles is to add it to root __layout.svelte
, as was suggested by Rich and supported with this PR (https://github.com/sveltejs/kit/pull/726):
<!-- __layout.svelte -->
<script context="module">
import '../app.scss'
</script>
The issue is that these styles are not linked in rendered HTML (like start.css
), but rather are included at runtime with JS.
This means that
- the styles are loaded with delay resulting in FOUC
- the app is completely unstyled when JS is disabled
Describe the proposed solution
- There could be a convention for including global styles from
app.[scss|less|css]
done by SvelteKit
- src
- app.html
- app.scss
- A more generalized solution would be to allow
app.ts
living alongside app.html, included by SvelteKit. This file may import global styles, but also some scripts, which wouldn’t make sense to include in layouts.
- src
- app.html
- app.ts
// app.ts
import './lib/global.scss' // include global styles
import Sentry from '@sentry/browser' // run global scripts
Sentry.init()
- Make
app.html
smarter (Parcel style), allowing it to include files in whatever format, which would then be compiled to js/css
<!-- app.html -->
<html>
<script src="./app.ts"></script>
<link rel="stylesheet" href="./app.scss">
</html>
Alternatives considered
An alternative is to compile CSS separately from SvelteKit and include hashed filename in app.html during build.
Importance
nice to have
Additional Information
Related issues: https://github.com/sveltejs/kit/issues/714
Issue Analytics
- State:
- Created 2 years ago
- Reactions:16
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to apply global styles with CSS modules in a react app?
The first rule should be to include all global styles, assuming it's kept in /styles/ or similar directory. The second rule is to...
Read more >How To Use Global Styles In SvelteKit - YouTube
How to use global styles in SvelteKit.👉️ Support▶️ YouTube Membershiphttps://youtube.com/@joyofcodedev/join ...
Read more >Global Styles & theme.json - Full Site Editing
Global styles is a system created to help users change the overall style of their site, without having to edit individual blocks or...
Read more >Creating a Global Style Sheet - meyerweb.com
Global style sheets can be applied either across your entire site, or across a subset of pages, and doing so means that you...
Read more >How to Style Your Site with Global Styles - Understrap
Global Styles let developers use a new theme settings file called theme.json to define website and block defaults.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ebeloded Hi, I have this problem too, the stylesheet file I import in __layout.svelte doesn’t exist in the initial markup. How did you fix this?!
not quite sure where to put this – there are several issues (open and closed) discussing this and (relatedly) possible changes to the root
__layout.svelte
– but in case it helps anyone, or furthers the discussion, i did a bit of testing today on the different ways to load global styles, and how global styles in the root__layout.svelte
worked with js on and off, in both dev and preview, and with a__layout.reset.svelte
somewhere in the path to another routeimport
<link rel="stylesheet">
<style global lang="scss">
at the bottom of the file<link rel="stylesheet">
@use "...";
in a<style lang="scss">
in<svelte:head>
<link rel="stylesheet">
to the file in<svelte:head>
@use "...";
the styles in a<style lang="scss">
in<svelte:head>