Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/main' into docs/extract-composable
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 10, 2022
2 parents 8415e17 + 2bc3c18 commit 0610369
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
62 changes: 57 additions & 5 deletions docs/content/3.api/3.utils/refresh-nuxt-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,67 @@ description: refreshNuxtData refetches all data from the server and updates the

# `refreshNuxtData`

`refreshNuxtData` refetches all data from the server and updates the page.
`refreshNuxtData` re-fetches all data from the server and updates the page as well as invalidates the cache of `useAsyncData`, `useLazyAsyncData`, `useFetch` and `useLazyFetch`.

::ReadMore{link="/getting-started/data-fetching"}
::
## Type

```ts
refreshNuxtData(keys?: string | string[])
```

Available options:
**Parameters:**

* `keys`:

**Type**: `String | String[]`

`refreshNuxtData` accepts a single or an array of strings as `keys` that are used to fetch the data. This parameter is **optional**. All `useAsyncData` and `useFetch` are re-fetched when no `keys` are specified.

## Examples

### Refresh All data

This example below refreshes all data being fetched using `useAsyncData` and `useFetch` on the current page.

```vue [pages/some-page.vue]
<template>
<div>
<button :disabled="refreshing" @click="refreshAll">
Refetch All Data
</button>
</div>
</template>
<script setup>
const refreshing = ref(false)
const refreshAll = async () => {
refreshing.value = true
try {
await refreshNuxtData()
} finally {
refreshing.value = false
}
}
</script>
```

### Refresh Specific Data

This example below refreshes only data where the key matches to `count`.

```vue [pages/some-page.vue]
<template>
<div>
{{ pending ? 'Loading' : count }}
</div>
<button @click="refresh">Refresh</button>
</template>
* `keys`: Provides an array of keys that are used in `useAsyncData` to refetch. When it's not specified, all `useAsyncData` and `useFetch` will be refetched.
<script setup>
const { pending, data: count } = useLazyAsyncData('count', () => $fetch('/api/count'))
const refresh = () => refreshNuxtData('count')
</script>
```

::ReadMore{link="/getting-started/data-fetching"}
::
6 changes: 6 additions & 0 deletions docs/content/3.api/4.advanced/2.kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ description: Nuxt Kit provides composable utilities to help interacting with Nux

- `useNuxt()`

### Pages

[source code](https://github.com/nuxt/framework/blob/main/packages/kit/src/pages.ts)

- `extendPages (callback: pages => void)`

### Plugins

[source code](https://github.com/nuxt/framework/blob/main/packages/kit/src/plugin.ts)
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxi/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const writeTypes = async (nuxt: Nuxt) => {
join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'),
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [],
...nuxt.options.typescript.includeWorkspace && nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : []
],
exclude: [
// nitro generate output: https://github.com/nuxt/framework/blob/main/packages/nuxt/src/core/nitro.ts#L186
relative(nuxt.options.buildDir, resolve(nuxt.options.rootDir, 'dist'))
]
})

Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/composables/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function useFetch<
if (!request) {
throw new Error('[nuxt] [useFetch] request is missing.')
}
const key = '$f' + _key
const key = _key === autoKey ? '$f' + _key : _key

const _request = computed(() => {
let r = request
Expand Down
2 changes: 2 additions & 0 deletions packages/test-utils/src/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const isNuxtApp = (dir: string) => {
return existsSync(dir) && (
existsSync(resolve(dir, 'pages')) ||
existsSync(resolve(dir, 'nuxt.config.js')) ||
existsSync(resolve(dir, 'nuxt.config.mjs')) ||
existsSync(resolve(dir, 'nuxt.config.cjs')) ||
existsSync(resolve(dir, 'nuxt.config.ts'))
)
}
Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ describe.skipIf(process.env.NUXT_TEST_DEV || isWindows)('payload rendering', ()
it('renders a payload', async () => {
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
expect(payload).toMatch(
/export default \{data:\{\$frand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/
/export default \{data:\{rand_a:\[[^\]]*\]\},prerenderedAt:\d*\}/
)
})

Expand Down

0 comments on commit 0610369

Please sign in to comment.