This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
useNuxtData
composable (#9262)
Co-authored-by: Daniel Roe <daniel@roe.dev> Co-authored-by: Pooya Parsa <pooya@pi0.io>
- Loading branch information
1 parent
a597524
commit b054a26
Showing
4 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# `useNuxtData` | ||
|
||
`useNuxtData` gives you access to the current cached value of `useAsyncData`, `useLazyAsyncData`, `useFetch` and `useLazyFetch` with explicitly provided key. | ||
|
||
::Alert | ||
This feature is not released yet. You can beta test using [Edge Channel](https://nuxt.com/docs/guide/going-further/edge-channel). | ||
:: | ||
|
||
## Type | ||
|
||
```ts | ||
useNuxtData<DataT = any> (key: string): { data: Ref<DataT | null> } | ||
``` | ||
## Examples | ||
### Show stale data while fetching in the background | ||
The example below shows how you can use cached data as a placeholder while the most recent data is being fetched from the server. | ||
```ts [archive.vue] | ||
// We can access same data later using 'posts' key | ||
const { data } = await useFetch('/api/posts', { key: 'posts' }) | ||
``` | ||
|
||
```ts [single.vue] | ||
// Access to the cached value of useFetch in archive.vue | ||
const { data: posts } = useNuxtData('posts') | ||
|
||
const { data } = await useFetch(`/api/posts/${postId}`, { | ||
key: `post-${postId}`, | ||
default: () => { | ||
// Find the individual post from the cache and set it as the default value. | ||
return posts.value.find(post => post.id === postId) | ||
} | ||
}) | ||
``` | ||
|
||
### Optimistic Updates | ||
|
||
We can leverage the cache to update the UI after a mutation, while the data is being invalidated in the background. | ||
|
||
```ts [todos.vue] | ||
// We can access same data later using 'todos' key | ||
const { data } = await useFetch('/api/todos', { key: 'todos' }) | ||
``` | ||
|
||
```ts [add-todo.vue] | ||
const newTodo = ref('') | ||
const previousTodos = ref([]) | ||
|
||
// Access to the cached value of useFetch in todos.vue | ||
const { data: todos } = useNuxtData('todos') | ||
|
||
const { data } = await useFetch('/api/addTodo', { | ||
key: 'addTodo', | ||
method: 'post', | ||
body: { | ||
todo: newTodo.value | ||
}, | ||
onRequest () { | ||
previousTodos.value = todos.value // Store the previously cached value to restore if fetch fails. | ||
|
||
todos.value.push(newTodo.value) // Optimistically update the todos. | ||
}, | ||
onRequestError () { | ||
todos.value = previousTodos.value // Rollback the data if the request failed. | ||
}, | ||
async onResponse () { | ||
await refreshNuxtData('todos') // Invalidate todos in the background if the request succeeded. | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters