-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt): automatically generate unique keys for keyed composables #4955
Conversation
β Deploy Preview for nuxt3-docs canceled.
|
): AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, DataE | null | true> { | ||
// eslint-disable-next-line prefer-const | ||
let [key, handler, options] = (typeof _key === 'string' ? [_key, _handler, _options] : typeof _options === 'string' ? [_options, _key, {}] : [fallback, _key, _options]) as [string, (ctx?: NuxtApp) => Promise<DataT>, AsyncDataOptions<DataT, Transform, PickKeys>] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perf: We don't need array creation and shift on normal (string, fn, obj?)
usage.
We can use a variable swap for (fn, obj?)
(is this even valid and handled when magic-keys pluign is not working ?!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this works even if the plugin isn't working, as long as the user provides a key. If they don't, they will get an error message: 'asyncData key must be a string'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you're right, we could swap variables around, but we would lose internal typing for those variables by doing it that way.
@pi0 Let me know when you plan to review this and I will resolve conflicts. |
You can resolve :) |
30f2699
to
b3bde78
Compare
Redoing merge as we can drop #5710 in this PR. There is no need for a warning as a key is automatically provided in this case. |
π Linked issue
resolves nuxt/nuxt#13963, resolves nuxt/nuxt#13951, resolves nuxt/nuxt#13583
β Type of change
π Description
This PR automatically generates fallback keys for
useState
,useAsyncData
,useFetch
,useLazyAsyncData
anduseLazyFetch
. If no key is provided by the user, or one can't be used (if, for example, functions are passed in the options touseFetch
oruseLazyFetch
) then the fallback key is used instead.Fallback keys are unique to the file & line number of the call to one of the keyed composables above. (So if a user uses one of them in their own composable, the fallback key will be shared across all possible calls to this composable, rather than being unique to each call.)
I've also updated the types in the documentation for
useAsyncData
anduseFetch
where there were some typos.Further note: It would be nice to have native support inunplugin
for parsing using acorn-compatible AST: unjs/unplugin#90. When that's possible, we can refactor this slightly into a native unplugin rather than just using it as a utility within the webpack builder...π Checklist