sveltejs / sveltejs/kit

Can `load` return async iterators?

Open
#13,156 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

In the era of generative AI, we often need to stream data to the client. In SvelteKit routes can return promises inside load transparently, that's good. I think it won't be hard to support async generators too.

For example, I use Svelte in a lot of GenAI related sites of mine. Usually I invoke streaming responses on the client side, via an API endpoint, but that's neither the most performant way, nor the besy UX. The API calling only happens after the full hydration, which is even slower on hardwares with bad network environment. If we can stream async iterators inside page load functions, that would save several seconds from users for each page load!

For now, there is actually a possible workaround:

Let's say you want to return a iterator which wouldn't yield more than 10,000 times. You can return a list with 10,000 promise instead, inside which the [i]-th promise will resolves when the async iterator yield the [i]-th times, or the iterator exhausted before that.

Describe the proposed solution

The same as how promises inside route loaders in sveltekit, like using trailing <script> with SSR.

Importance

would make my life easier

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SvelteKit route load functions and the SSR path, comparing how promises returned from load are handled. Examine the relationship between page load, hydration, and API endpoints. Done means async generators can stream through load with documented behavior and coverage for exhaustion and yielded values.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.