Skip to content

Commit 9a8700a

Browse files
docs: update nuxt docs to recommend callOnce (#2916)
1 parent 072569a commit 9a8700a

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

packages/docs/ssr/nuxt.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,23 @@ And that's it, use your store as usual!
4343

4444
## Awaiting for actions in pages
4545

46-
As with `onServerPrefetch()`, you can call a store action within `asyncData()`. Given how `useAsyncData()` works, **make sure to return a value**. This will allow Nuxt to skip running the action on the client side and reuse the value from the server.
46+
As with `onServerPrefetch()`, you can call a store action within the `callOnce()` composable.
47+
This will allow Nuxt to run the action only once and avoids refetching data that is already present.
4748

4849
```vue{3-4}
4950
<script setup>
5051
const store = useStore()
5152
// we could also extract the data, but it's already present in the store
52-
await useAsyncData('user', () => store.fetchUser())
53+
await callOnce('user', () => store.fetchUser())
5354
</script>
5455
```
5556

56-
If your action doesn't resolve a value, you can add any non nullish value:
57+
Depending on your requirements, you can choose to run the action only once on the client, or on every navigation (which is closer to data fetching behavior of `useFetch()`/`useAsyncData()`)
5758

5859
```vue{3}
5960
<script setup>
6061
const store = useStore()
61-
await useAsyncData('user', () => store.fetchUser().then(() => true))
62+
await callOnce('user', () => store.fetchUser(), { mode: 'navigation' })
6263
</script>
6364
```
6465

0 commit comments

Comments
 (0)