iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔬

Async SSR Is Now Available in Async Svelte

に公開
2026/01/16

Introduction

Asynchronous Svelte has been a hot topic lately. Although it is still experimental, it is now possible to perform SSR on components containing await without a svelte:boundary. I would like to introduce it.

https://github.com/sveltejs/svelte/pull/16748

Project Setup

bunx sv create

Don't forget to configure Experimental Async Svelte as well.

svelte.config.js
import adapter from "@sveltejs/adapter-auto";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://svelte.dev/docs/kit/integrations
  // for more information about preprocessors
  preprocess: vitePreprocess(),

  kit: {
    // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
    // If your environment is not supported, or you settled on a specific environment, switch out the adapter.
    // See https://svelte.dev/kit/adapters for more information about adapters.
    adapter: adapter(),
  },
+  compilerOptions: {
+    experimental: {
+      async: true,
+    },
+  },
};

export default config;

Without Async SSR

Before Async SSR, in Async Svelte, fetching asynchronous data required execution from scratch on the client side.

src/routes/+page.svelte
<script lang="ts">
  function sleep(ms: number, context: string) {
    return new Promise((resolve) => {
      console.log(`I'm running at ${context}!`);
      return setTimeout(() => resolve(Math.random()), ms);
    });
  }
</script>

<section>
  {#await sleep(1000, "#await block")}
    Loading #await block...
  {:then data}
    using #await block: {data}
  {/await}
</section>

<section>
  <svelte:boundary>
    {#snippet pending()}
      Loading svelte:boundary...
    {/snippet}
    await inside component: {await sleep(1500, "await inside svelte:boundary")}
  </svelte:boundary>
</section>

This is not ideal for the following reasons:

  • Initial rendering takes time because the client must receive the JavaScript, wait for it to be parsed, and then trigger the request again.
  • Data fetching is impossible if the client has JavaScript disabled.

Therefore, it was recommended to perform data fetching in +page.server.ts or +page.ts even in Async Svelte.

Async SSR

With Async SSR, you can use await outside of the svelte:boundary tree. In this case, SSR will wait for the await and include the results in the SSR output.

src/routes/async-ssr/+page.svelte
<script lang="ts">
  function sleep(ms: number) {
    return new Promise((resolve) => {
      console.log(`I'm running!`);
      return setTimeout(() => resolve(Math.random()), ms);
    });
  }
</script>

await outside component: {await sleep(2000)}

You will notice that this page takes 2 seconds to load. Instead, the returned HTML already contains the rendering result:

$ curl localhost:5173/async-ssr
# excerpt
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents"><!--[--><!--[--><!----><!----><!---->await outside component: 0.6573334609064831<!----><!----><!----><!--]--><!--[--><!--]--><!--]-->

With Async SSR, the likely pattern will be to use svelte:boundary (with {#snippet pending()}) for lazy fetching on the client, and to use direct await outside of it for fetching during SSR.

Real-world Use Case

If you open the async-ssr example above in your browser, you will notice that the random number changes after 2 seconds (meaning sleep is executed twice, once on the server and once on the client). This is inefficient as data fetching occurs twice even in a real application.

In the Async SSR discussion, elliott-with-the-longest-name-on-github explains it as follows:

This really isn’t very desirable behavior. During hydration, you typically want to stick with the stuff you rendered on the server unless it actually needs to change! What we need to be able to do is render something asynchronously on the server, then hydrate it synchronously on the client based on what we rendered on the server. This requires serialization at some point. Somehow, we have to get the data you’ve rendered over the wire in such a way that we can retrieve it on the client and hydrate it into reactive JavaScript.

React solves this problem by making the serialization boundary be the client component. Data passed from a server component to a client component has to be serializable so that it can be sent over the wire and used in hydration. The downside of this approach is that React has to live in two worlds, where some of your components are server components (and therefore only run on the server) and some of them are client components (and therefore run on both the server, for initial renders, and the client for subsequent client-side ones). Svelte has always been a very isomorphic framework — we believe the Svelte code you write should be able to run equally well on the server and the client. Keeping to these roots, we think the more logical place to put this serialization boundary is at the resource level — at the actual place you’re fetching the data.

Thankfully, we’ve already taken care of this problem for you with remote functions in SvelteKit. Given a setup like this:

export const getUser = query(v.string(), (id) => getUserFromDatabase(id))
<script>
 import { getUser } from './get-user.remote.js';
 import { page } from '$app/state';
</script>

<h1>Hi, {(await getUser(page.params.userId).name}</h1>

…when server rendering, the result of getUser will be inlined into the resulting HTML, and during hydration, getUser will simply return the data synchronously — no delays or additional requests involved.

“But Elliott, I don’t want to use SvelteKit!” That’s fine! Remote functions have been our exploration into what the best possible version of data-fetching in SvelteKit could be. Now that we’ve had that exploration, we’re working on bringing some of that goodness into Svelte core, so that other framework authors can build similar solutions, or you can make your own.

Summary:

This behavior isn't what we expect.
When using Remote Functions in SvelteKit, the results are serialized into HTML, eliminating hydration delays and redundant requests!
We are currently working on bringing this "goodness" into the Svelte core to make it easier to implement in non-SvelteKit environments as well.

So, it seems we should use Remote Functions.

Closing

Please feel free to leave a comment if there are any inaccuracies or unclear points!

Changelog

2026-1-16: Changed syntax highlighting from HTML to Svelte, as Zenn.dev now supports Svelte highlighting (Thank you!)

Discussion