-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte Suspense #1736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
hmmm... in any case, we can just move h2 into await block. But I like next feature:
really help me |
I hadn't heard of React Suspense before, so I'm still absorbing this - is the idea that you have the capability to say "display this other component as a placeholder until all If I understand the goal, then this seems like something that should go in Sapper rather than Svelte itself. I feel like these kinds of more-than-a-few-ms-loading-time state changes should be serialized in the url, and be contained in a route/component nesting level. That said, Svelte does have |
The point is that you could have multiple async dependencies at arbitrary depths in your tree: <h2>Header that should not appear yet</h2>
<!-- Thing contains a component, that contains two components that contain async deps -->
<Thing/> Worth watching the Iceland talk to understand the motivations more clearly.
Not possible — it's something the compiler needs to be aware of. Sapper is, at heart, just a thing that constructs a tree of data containing components each time you navigate. (I haven't yet thought about the interplay between this and
There are lots of counter-examples — for example 'this modal is open' probably isn't something you want to serialize in the URL, in most cases. And an async dependency could be as trivial as 'wait until this image has loaded, so we know the dimensions before we render these new elements, to avoid the layout jumping'. An |
Can't suspense be implemented with Svelte with state machines? Wait x seconds for a request, if arrived, display that, if not display this -> that is a state machine. Some behaviour which comes up all the time could be turned into predefined state machines with the stuff to render as children with slot corresponding to the state of the state machine (loaded, waiting, etc.). Just a quick idea, not sure if it makes sense but that would be standard Svelte templating with no additional syntax -> no debugging on svelte/sapper side! |
If not with state machines, take a look into statecharts (e.g. xstate). |
@Ryuno-Ki I have my own state machine library which i used for about three years now, and deals with these cases reasonably nicely. I am in the process of integrating it with Svelte which is why I came to think about using it also for suspense-like features. Once that is done, I will take a shot at it. I may be naive but for what I understood suspense is just about adding a conditional filter or proxy between a rendering request and a rendering execution. That should be able by intercepting the rendering requests (e.g. state changes as one means the other) and a simple state machine with three or four states should suffice. I don't think there is any need for statecharts here. But anyways, more news when I get to it. |
I finalized a proof of concept. This example was useful and outlined some benefits and some difficulty of Svelte's compiler approach. I will address this in a new issue. |
Closing in favour of new issue linked by @brucou - and potentially an RFC. |
A few people have asked whether Svelte will ever support something like React Suspense (if you're not familiar with the idea, Dan did some helpful tweets tonight).
With Svelte's current approach Suspense is impossible. I haven't been too concerned about that — it's not generally a problem in Sapper apps because of
preload
, for example, and I have some concerns about the mechanism by which it's implemented (i.e. throwing promises, which seems likely to create tricky-to-identify-or-reason-about situations where things that should be happening in parallel end up happening serially) — but it is a cool idea, and I get a lot of pleasure out of saying 'of course we have [x] feature' to sceptical React devs.Consider an app like this:
It'd be great if, on toggling the checkbox, Svelte waited for the
foo
promise to resolve before updating anything inside the<svelte:placeholder>
. But the code Svelte generates makes this impossible, because there's no intermediate virtual DOM — state changes are applied immediately to the real DOM:If we changed that to something along these lines...
...then we could decide whether or not to apply those operations based on whether there were any unresolved async data dependencies. (I'm glossing over a lot of detail here but that's the basic concept.)
In React, async data dependencies are declared by throwing a promise, as mentioned above. In Svelte, we could use
await
blocks, which has the crucial advantage that you don't need to jump through somewhat confusing hoops like this:The downside is that it involves generating slightly more code, and doing slightly more work, than if the operations are applied immediately. I'm curious about whether people think the trade-off would be worth it.
The text was updated successfully, but these errors were encountered: