sveltejs / sveltejs/kit

Load css files on `+layout.svelte` conditionally based on server-side data

Open
#9,943 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

We are trying to import a.css on a layout file, and when a prop coming from +layout.server.js is met, load b.css.

CSS files imported from components load regardless of the svelte component getting rendered on the markup. This is a problem because we haven't found another way to load global css files based on SSR props.

Describe the proposed solution

Ideally we could simply do the following:

<script>
  // +layout.svelte
  import './styles/a.css';
  import Theme  from './Theme.svelte';

  export let data;
</script>

<!-- b.css is not loaded when condition is false  -->
{#if data.condition}
  <Theme />
{/if}

<slot />
<script>
  // Theme.svelte
  import './styles/b.css';
</script>
<script>
  // +layout.server.js
  export async function load(event) {
    return { condition: false }
  }
</script>
Alternatives considered

We have taken a look at the layout system. Unfortunately it seems to designed around routes and not really around props coming from a server.

Importance

would make my life easier

Additional Information

We are not sure how to proceed maintaining SSR. Would appreciate any suggestions or ideas!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported examples in +layout.svelte, Theme.svelte, and +layout.server.js, then trace how component CSS imports are processed during SSR. Determine whether the requested conditional loading can be supported while preserving SSR, and define tests that verify b.css is absent when the server-provided condition is false.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.