From 8dd77d7b6e250b3b8f4fb2db0c4b3b786392f1f5 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 29 Mar 2022 01:12:41 +0800 Subject: [PATCH] feat: `refreshNuxtData` function and `app:data:refresh` hook (#3929) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Chopin Co-authored-by: Pooya Parsa --- .../content/3.docs/1.usage/1.data-fetching.md | 33 +++++++++++++++++ examples/use-async-data/app.vue | 35 ++++++++++++++----- .../components/CounterExample.vue | 19 ++++++++++ .../components/MountainExample.vue | 9 +++++ packages/bridge/src/runtime/composables.ts | 2 +- .../nuxt3/src/app/composables/asyncData.ts | 21 ++++++++--- packages/nuxt3/src/app/composables/index.ts | 2 +- packages/nuxt3/src/app/nuxt.ts | 1 + packages/nuxt3/src/auto-imports/presets.ts | 1 + 9 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 examples/use-async-data/components/CounterExample.vue create mode 100644 examples/use-async-data/components/MountainExample.vue diff --git a/docs/content/3.docs/1.usage/1.data-fetching.md b/docs/content/3.docs/1.usage/1.data-fetching.md index 6ac4d6f601..75236470c0 100644 --- a/docs/content/3.docs/1.usage/1.data-fetching.md +++ b/docs/content/3.docs/1.usage/1.data-fetching.md @@ -168,6 +168,39 @@ watch(posts, (newPosts) => { ``` +## `refreshNuxtData` + +Invalidate the cache of `useAsyncData`, `useLazyAsyncData`, `useFetch` and `useLazyFetch` and trigger the refetch. + +This method is useful if you want to refresh all the data fetching for a current page. + +### Usage + +```ts +refreshNuxtData(keys?: string | string[]) +``` + +Available options: + +* `keys`: Provides an array of keys that used in `useAsyncData` to refetch. When it's not specified, all `useAsyncData` and `useFetch` will be refetched. + +### Example + +```vue + + + +``` + ## Isomorphic fetch When we call `fetch` in the browser, user headers like `cookie` will be directly sent to the API. But during server-side-rendering, since the `fetch` request takes place 'internally' within the server, it doesn't include the user's browser cookies, nor does it pass on cookies from the fetch response. diff --git a/examples/use-async-data/app.vue b/examples/use-async-data/app.vue index fa1a0be39d..6cf95b41d6 100644 --- a/examples/use-async-data/app.vue +++ b/examples/use-async-data/app.vue @@ -1,18 +1,35 @@