Skip to content

Lazily imported components suffer from FOUC #5137

@Rich-Harris

Description

@Rich-Harris

Describe the bug

A useful pattern is to dynamically import a component inside a load function:

<script context="module">
  export async function load({ params }) {
    return {
      props: {
        Thing: params.thing = 'this'
          ? (await import('$lib/ThisThing.svelte')).default
          : (await import('$lib/ThatThing.svelte')).default
      }
    };
  }
</script>

<script>
	export let Thing;
</script>

<Thing/>

The problem is that results in FOUC, because SvelteKit only injects CSS for static dependencies when server-side rendering. The styles don't appear until the component is imported.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-qstyga?file=src%2Froutes%2Findex.svelte,src%2Flib%2FLazy.svelte&terminal=dev

Logs

No response

System Info

latest

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions