svelte-comp-lazyloader
v1.0.1
Published
Lazy-load Svelte 5 components with typed props, preload, and SSR
Maintainers
Readme
svelte-comp-lazyloader
Lazy-load Svelte 5 components. Factory returns a concrete component type, so wrapped props autocomplete.
Requires Svelte 5.36+ with compilerOptions.experimental.async.
Features
- Typed props from the wrapped component
- Optional
pending/failed/onerrorvia<svelte:boundary>(nopending→ renders nothing, like Solid'slazy) preload()to start the import early (hover, intent)ssrto render the full component on the server instead of the pending snippet, doesn't affect CSR
Installation
pnpm add svelte-comp-lazyloaderUsage
Call lazy() at module scope so identity stays stable across hydration.
<script lang="ts">
import { lazy } from "svelte-comp-lazyloader";
const Hello = lazy(() => import("./Hello.svelte"));
</script>
<Hello name="world" />With no pending snippet, the component renders nothing until the import resolves.
Pass pending (and optional failed / onerror) for loading UI:
<script lang="ts">
import { lazy } from "svelte-comp-lazyloader";
const Hello = lazy(() => import("./Hello.svelte"), { ssr: true });
</script>
<Hello name="world">
{#snippet pending()}
<p>Loading...</p>
{/snippet}
</Hello>Preload on hover
<script lang="ts">
import { lazy } from "svelte-comp-lazyloader";
const LazyTest = lazy(() => import("./Test.svelte"));
let show = $state(false);
</script>
<button
onpointerenter={() => LazyTest.preload()}
onclick={() => (show = !show)}>{show ? "Hide" : "Show"}
</button>
{#if show}
<LazyTest name="Bah"/>
{/if}Once preload() settles, the first mount skips the pending snippet.
Options
lazy(loader, { ssr?: boolean });| option | default | meaning |
| ------ | ------- | ----------------------------------------------------------------------- |
| ssr | false | On the server, render the full component instead of the pending snippet |
preload()
LazyTest.preload(): Promise<void>Starts the same import the component uses. Fire-and-forget on hover, or await it.
Reserved props
pending, failed, and onerror belong to the lazy wrapper, not the wrapped component. If your component declares any of these props, they are omitted from the lazy component's typed props and cannot be forwarded — rename them on the wrapped component if you need them.
Source Code
Since this lib is MIT licensed, you can also contribute to it at it's repo on GitHub
