2022-10-06 09:15:30 +00:00
---
2023-10-18 10:59:43 +00:00
title: 'refreshNuxtData'
2022-10-06 09:15:30 +00:00
description: refreshNuxtData refetches all data from the server and updates the page.
2023-10-18 10:59:43 +00:00
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts
size: xs
2022-10-06 09:15:30 +00:00
---
2024-02-21 17:09:45 +00:00
::note
2023-07-07 16:24:09 +00:00
`refreshNuxtData` re-fetches all data from the server and updates the page as well as invalidates the cache of [`useAsyncData` ](/docs/api/composables/use-async-data ) , `useLazyAsyncData` , [`useFetch` ](/docs/api/composables/use-fetch ) and `useLazyFetch` .
2023-10-18 10:59:43 +00:00
::
2022-10-06 09:15:30 +00:00
2024-10-13 20:26:42 +00:00
::warning
`refreshNuxtData` only works with composition API. `asyncData` hook from the Options API does not work with `refreshNuxtData` .
::
2022-11-10 09:08:42 +00:00
## Type
2022-04-06 05:56:08 +00:00
```ts
refreshNuxtData(keys?: string | string[])
```
2022-11-10 09:08:42 +00:00
**Parameters:**
* `keys` :
**Type** : `String | String[]`
2023-07-07 16:24:09 +00:00
`refreshNuxtData` accepts a single or an array of strings as `keys` that are used to fetch the data. This parameter is **optional** . All [`useAsyncData` ](/docs/api/composables/use-async-data ) and [`useFetch` ](/docs/api/composables/use-fetch ) are re-fetched when no `keys` are specified.
2022-11-10 09:08:42 +00:00
2023-10-18 10:59:43 +00:00
## Refresh All Data
2022-11-10 09:08:42 +00:00
2023-07-07 16:24:09 +00:00
This example below refreshes all data being fetched using [`useAsyncData` ](/docs/api/composables/use-async-data ) and [`useFetch` ](/docs/api/composables/use-fetch ) on the current page.
2022-11-10 09:08:42 +00:00
```vue [pages/some-page.vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
2022-11-10 09:08:42 +00:00
const refreshing = ref(false)
const refreshAll = async () => {
refreshing.value = true
try {
await refreshNuxtData()
} finally {
refreshing.value = false
}
}
< / script >
2023-07-18 10:31:45 +00:00
< template >
< div >
< button :disabled = "refreshing" @click =" refreshAll " >
Refetch All Data
< / button >
< / div >
< / template >
2022-11-10 09:08:42 +00:00
```
2023-10-18 10:59:43 +00:00
## Refresh Specific Data
2022-11-10 09:08:42 +00:00
This example below refreshes only data where the key matches to `count` .
```vue [pages/some-page.vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
2024-07-03 22:05:15 +00:00
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
2023-07-18 10:31:45 +00:00
const refresh = () => refreshNuxtData('count')
< / script >
2022-11-10 09:08:42 +00:00
< template >
< div >
2024-07-03 22:05:15 +00:00
{{ status === 'pending' ? 'Loading' : count }}
2022-11-10 09:08:42 +00:00
< / div >
< button @click =" refresh " > Refresh</ button >
< / template >
```
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/getting-started/data-fetching"}