Top-level await
See original GitHub issueThis is continuation for #5351.
The problem is that there’s no way to render an asynchronously loaded component on SSR. My use-case for asynchronously loaded components is rendering JAM-stack site’s content from JSON files. My JSON-files describe page content as blocks, that are different components (type) and their props (data).
I want to have top-level await support for component’s <script>
that will allow awaiting for promises before component’s initialization is finished.
I’ve done an implementation for this, but changing initialization asynchronous is quite a breaking change. After the change it’s not possible to initialize component with constructor, but instead we have to use a static async builder method Component.init(options)
. Not being able to initialize synchronously breaks custom elements. Component updating also becomes asynchronous so assignments to props don’t get reflected to DOM synchronously. That will also break a lot of code.
If I make async initialization a compiler option, so that it doesn’t trash backwards compatibility, would maintainers be willing to merge the changes? Is there any demand for this feature?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:30
- Comments:27 (4 by maintainers)
@joas8211 Sorry for my noobness, could it be possible to add syntax for async instead of a compiler option? Like this:
@antony What do you mean? Acorn already supports “top-level await” with
allowAwaitOutsideFunction: true
. And I’m not trying to have a real top-level await (top-level of module). I’m just trying to have await for component’s script-tag (init script) that is actually wrapped in a function that can be changed to an async function just like I did in my fork.