dev-resources.site
for different kinds of informations.
A short guide to Async Components in Svelte 5
Published at
12/25/2024
Categories
svelte
webcomponents
tutorial
javascript
Author
digitaldrreamer
Author
15 person written this
digitaldrreamer
open
I couldn't find any working solution for this online, so I thought to share it when I got it to work.
The Problem: Asynchronous Components
I needed a maintenance page that would take over the entire site when enabled, but loading it on every page visit seemed wasteful. The component should only load when actually needed.
The Solution: Combining {#await} with Dynamic Imports
The {#await}
block in Svelte lets you handle promises right in your template. Pair that with dynamic import()
for lazy-loading, and you've got yourself a concise and clear way to handle async components.
Here's the code:
<script>
// info.maintenance (boolean) && info.maintenance_ends (timestamp)
export let info;
const MaintenanceComponent = info?.maintenance
? import("$lib/components/Maintenance.svelte")
: null;
</script>
{#if MaintenanceComponent}
{#await MaintenanceComponent then M}
{@const Maintenance = M.default}
<Maintenance time={info.maintenance_ends} />
{:catch error}
<p>Failed to load maintenance page: {error.message}</p>
{/await}
{/if}
-
Dynamic Import: I used
import()
to load theMaintenance.svelte
component asynchronously. This makes sure the component is only loaded when maintenance mode is turned on. -
{#await}
Block: This block allows me to await the import. -
{@const}
: The{@const}
block allows you to extract the default export (M.default
) into a local variable.
Happy Hacking!
svelte Article's
30 articles in total
🚀 I have released Eurlexa!!! EU Regulation at Your Fingertips!
read article
Building "Digital DSA": The Journey of Crafting a Smarter Loan Comparison Platform
read article
Tower defense clicker game built with Svelte 5, without canvas. Only CSS transitions and the power of Runes
read article
Optimize SvelteKit performance with brotli compression
read article
Migrating the VSCode theme generator to oklch
read article
Beatbump: Exploring Svelte Best Practices for Dynamic Web Applications
read article
SvelteJs vs ReactJs : But both can co-exists!
read article
Build a Simple Chatbot with Svelte and ElizaBot
read article
A minimalist password manager desktop app: a foray into Golang's Wails framework (Part 2)
read article
The Perfect Trio: Wails, Go & Svelte in Action
read article
Svelte 5: Share state between components (for Dummies)
read article
Integrating SvelteKit with Storyblok (Using Svelte 5)
read article
Changelog 2024/48 aka We 🖤 Svelte 5
read article
My first Micro SaaS | Automated, SEO-friendly Changelogs
read article
Quit Using Anonymous Functions in Props!
read article
From Svelte 4 to Svelte 5: Understanding Slots (default and named)
read article
Make EditorJS work in Svelte(kit) SSR
read article
Shadcn UI Theme generator, with OKLCH colors and ancient sacred geometry.
read article
A short guide to Async Components in Svelte 5
currently reading
New Post here!🍻
read article
Svelte vs Vue: Choosing the Best Front-End Framework
read article
Step-by-Step Tutorial: How to Use ALLAIS for Effortless UI Generation
read article
Schritt-für-Schritt-Anleitung: So verwenden Sie ALLAIS für die einfache UI-Generierung
read article
ALLAIS: Your Ultimate Personal UI Generator for Modern Web Development
read article
Пошаговый учебник: Как использовать ALLAIS для легкой генерации UI
read article
Svelte
read article
A minimalist password manager desktop app: a foray into Golang's Wails framework (Part 1)
read article
When Goliath Fell to David’s Smallest Stone: The Innocent Dropdown Tweak That Shattered React, Vue, and Svelte
read article
Deploying Your SvelteKit App to GitHub Pages with a Custom Domain (Ditch GoDaddy for Porkbun)
read article
Syncing DOM State and Data without IDs
read article
Featured ones: