-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Should useAsyncData
emit warn in dev mode when returning false
or ''
values?
#28096
Comments
Start a new pull request in StackBlitz Codeflow. |
@danielroe This change introduced the warning for every non-truthy value. It should check explicitly for |
PR welcome to check only for |
What is your use-case for returning |
For example, we have this function from a Pinia store: async function isLogged(): Promise<boolean> {
if (!loggedIn.value) {
try {
user.value = await $fetch<User>('/api/user');
loggedIn.value = true;
} catch (error: unknown) {
if (error instanceof FetchError) {
if (error.status !== 401 && error.status !== 403) {
console.error('No user found', error);
}
}
}
}
return loggedIn.value;
} that we use this way in a plugin: await useAsyncData('isLogged', () => isLogged(), {dedupe: 'defer'}); I need to wrap it like this if I want to avoid the warn: await useAsyncData(
'isLogged',
async () => {
return {isLogged: await isLogged()};
},
{dedupe: 'defer'},
); |
Ah, that is exactly what we are trying to warn about. useAsyncData is not for dispatching side effects. There are situations when it might run at a different time than you are expecting. |
Are you referencing the states assignations in the store through But even without this, I assume we could easily find some use-cases as such: const {data: isLogged} = await useAsyncData(() => $fetch<boolean>('/api/user/logged')); And then do something depending on |
A use case for explicitly returning // fetchMyPost returns Post | null
const { data: post } = await useAsyncData(() => fetchMyPost(route.params.id)) |
Wouldn't you rather return a |
Depends on the use case. Sometimes you still want to render the page and just don't show the loaded value or indicate that it does not exist. |
I also encountered it and do not fully understand why I am not allowed to return 0 or false. If I want to fetch the count of posts of category X and display something special for 0 posts I have to go the detour of throwing an error? Would appreciate it very much to get more insights, as "must be truthy" is not so telling for me, something with timing, any examples, use-cases? Also useFetch docs does not state this (directly), so I was even more confused and searched my code for an abandoned useAsyncFetch until I remembered that useFetch is a wrapper |
Also discovered the same problem out-of-nowhere on this code after doing some dependency updates. app.vue const { data: token } = await useFetch('/api/token', { headers }) server/api/token.get.ts export default eventHandler(event => getToken({ event })) The endpoint returns
I had to do a manual P.S. Will try to create a reproduction for it |
Environment
Reproduction
https://stackblitz.com/edit/nuxt-starter-nhpzcs?file=app.vue
Describe the bug
When returning
false
or''
fromuseAsyncData
, we get the following warn in dev mode:WARN [nuxt] useAsyncData must return a truthy value (for example, it should not be undefined, null or empty string) or the request may be duplicated on the client side.
The request is indeed duplicated on the client side when returning
null
orundefined
but it is not when returningfalse
or''
, as you can see on the reproduction link. Is there anything else that's preventingfalse
/''
to be returned formuseAsyncData
? Should this warn be displayed only when returningnull
/undefined
?(I'll happily fix/update this behaviour if it's confirmed)
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: