-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Top-level await in script and / or nuxt-like server data fetching #3043
Comments
Top level await would at least be consistent with the newer versions of node but there might be design choices at play here |
Top-level await does not need newer versions of node since the code inside '<script>' is put inside a function. |
Related: #2301 |
I'd love to see top-level await in Svelte, i think it would be a very elegant api to allow a streaming api and a great alternative to React's suspend. export let params;
$: post = await fetch(`/blog/${params.slug}`) it also unlocks using But although the usage is simple, the impact on the svelte codebase is non-trivial: It moves the responsibilities of async behavior outside of the component:
It complicates things because mounting, #if, #each & #key are no longer synchronous. Hydration is also affected, you don't want to see the server rendered page and go into a pending state on the client and reload everything again. So all top-level awaits should be preresolved in the hydration step and use the values from the server. I think this is doable. Speaking of rejected promises, i'd suggest to only serialize the successful promises:
That a lot of work, but it definitely on my wishlist for Svelte, just imagine how much it will improve working with promises. |
Using |
Hi, total Noob here This is a critically underestimated feature. Javascript Modules are the new default, used by every modern tool. (One of my personal favorites Firebase). top-level cya |
Describe the problem
The actual way SvelteKit loads data from server before loading the page has two defaults:
This is a typical example of server-side rendering with SvelteKit:
So we've written a lot of code just to return a string, and it is absolutely not safe. You have to indicate the type first in the load function and then in the main script section.
Linters won't complain if we write something like this:
Describe the proposed solution
I won't describe a full proposed solution as I don't know about Svelte and SvelteKit internals, but instead I will share raw ideas that need refinement and further thinking.
With Vue and the
script setup
syntax, it is actually possible to useawait
in the main script section.This wonderful feature brings that kind of syntax:
Which is extremely concise and type-safe.
I know for sure this is not a small feature, it would need changes even in the Svelte internal parts, not just SvelteKit.
Just making
await
working will not be a magical solution ; there should be a way to prevent data-fetching first from server and then from client after hydration. To achieve this in a clean way, Nuxt 3 uses theuseFetch
composable:Which is not exactly the same approach as the
await
but quite close. Maybe SvelteKit should define its ownuseFetch
/useServerLoader
- or whatever it is named - utility function to deal with asynchronous server-side loading?To deal with page parameters and similar stuff, I would use the same syntax as in client-side, ie with
$page
store. That simplifies things as you share one syntax for server and client.I think this feature should be thought taking in consideration other issues about SvelteKit loading, as it might resolve them in an elegant way as well:
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: