Skip to content
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

Lazily imported components suffer from FOUC #5137

Closed
Rich-Harris opened this issue Jun 1, 2022 · 3 comments
Closed

Lazily imported components suffer from FOUC #5137

Rich-Harris opened this issue Jun 1, 2022 · 3 comments

Comments

@Rich-Harris
Copy link
Member

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

Rich-Harris added a commit that referenced this issue Jun 1, 2022
Rich-Harris added a commit that referenced this issue Jun 1, 2022
Rich-Harris added a commit that referenced this issue Jul 5, 2022
* add failing test for #5137

* partial fix for #5137

* defer gathering of styles

* fix typechecking

* try and nudge turbo into not breaking everything

* include dynamically imported styles during dev SSR

* changeset

* rename some stuff to make it clearer

* simplify, and dont follow dynamic imports from entry

* tidy up

* tidy up

* tidy up
@PlopTheReal
Copy link

PlopTheReal commented Jul 13, 2022

Not sure if that's related but as of 1.0.0-next.370, all the CSS of all the dynamic components gets loaded from the first page load.

@PlopTheReal
Copy link

Sorry I haven't precised that I'm not loading the components with that pattern (lazy load when the component has mounted).
I've tried such pattern but I'm getting the following errors:

in dev:
/web/svelttest/node_modules/vite/dist/node/chunks/dep-80fe9c6b.js:50310
            const err = new Error(`Cannot find module '${id}' imported from '${importer}'`);

in preview: 
Error: <Test> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

@Rich-Harris
Copy link
Member Author

This was fixed by #5138.

@PlopTheReal it's unclear what your last comment has to do with this thread — if you're experiencing a bug, please open a new issue with a repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants