-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat(nuxt): experimental native async context support #20918
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
Conversation
reverted |
2592ed6
to
5b069f5
Compare
@pi0 What do you mean by:
Is there something I can help with on this? |
@danielroe I have tried to explain it in the foot notes. ALS only works when we use |
Yes. Actually the only reason it is not universally enabled is that because some platforms don't support it yet. As soon as it is universal we move it out of the experimental state. |
yip, still doesnt work in vercel! but looking forward... |
π Linked issue
Related to #14269
β Type of change
π Description
See #14269 (comment) and unjs/unctx#async-context for some information.
When using async composables, Nuxt app context will be only available before the first
await
statement, and for nested composables, it should be manually passed which is a negative DX especially as composables are designed to have access to the context without manually passing it!Previously we had done some efforts in order to mitigate this DX issue:
<script setup>
to support async/await syntaxdefineNuxtPlugin
,defineNuxtRouteMiddleware
,defineNuxtComponent
(setup
andasyncData
keys),defineNuxtPlugin
(setup
key) anddefinePageMeta
(middleware
andvalidate
keys) for one level async support via transforms similar to Vue (configurable/extendable viaoptimization.asyncTransforms
option)nuxtApp.runWithContext(() => ...)
helper to make async context available (still one level only)Developing more complex logic and nested async composables, makes it harder and harder to use previous solutions. A simple and problematic example:
This PR enables an experimental feature using Node.js
AsyncLocalStorage
and new unctx support to make async context available natively to any nested async composable without needing a transform or manual passing/calling with context.Because
AsyncLocalStorage
is required for this feature and it is a Node.js only feature, we have already made a mock available via unjs/unenv for non-Node.js deployment presets (unjs/unenv#98).Also since this is a critical and new feature, I have made it available under an experimental flag to be enabled:
TODO:
node:async_hooks
Β unjs/unenv#98)nuxtApp.runWithContext
requirementNotes:
<script setup>
, we need to call first level composable withnuxtApp.runWithContext
since Vue itself does not make async context available or has async context support.π Checklist