Skip to content

Instantly share code, notes, and snippets.

@jpvincent
Created October 24, 2023 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpvincent/bc1a2327d14869685f2e2c78e3ab6c28 to your computer and use it in GitHub Desktop.
Save jpvincent/bc1a2327d14869685f2e2c78e3ab6c28 to your computer and use it in GitHub Desktop.
Lazy loading svelte component
<script>
let lazyModalComponent;
let isPending = false;
function openModal() {
isPending = true;
lazyModalComponent = import(`./Modal.svelte`);
lazyModalComponent.catch((error) => {
// gestion en cas d'erreur de connexion ici
}).finally(() => {
isPending = false;
});
}
</script>
<button on:click={openModal}>
{#if isPending}
Loading
{:else}
Open
{/if}
</button>
{#if lazyModalComponent}
{#await lazyModalComponent then { default: Modal }}
<Modal />
{/await}
{/if}
@jpvincent
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment