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

feat(nuxt)!: refresh to override previous requests by default #8190

Merged
merged 1 commit into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/content/1.getting-started/6.data-fetching.md
Expand Up @@ -152,10 +152,10 @@ function next() {

The key to making this work is to call the `refresh()` method returned from the `useFetch()` composable when a query parameter has changed.

By default, `refresh()` will not make a new request if one is already pending. You can override any pending requests with the override option. Previous requests will not be cancelled, but their result will not update the data or pending state - and any previously awaited promises will not resolve until this new request resolves.
By default, `refresh()` will cancel any pending requests; their result will not update the data or pending state. Any previously awaited promises will not resolve until this new request resolves. You can prevent this behaviour by setting the `dedupe` option, which will instead return the promise for the currently-executing request, if there is one.

```js
refresh({ override: true })
refresh({ dedupe: true })
```

### `refreshNuxtData`
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.api/1.composables/use-async-data.md
Expand Up @@ -30,7 +30,7 @@ type AsyncDataOptions<DataT> = {
}

interface RefreshOptions {
override?: boolean
dedupe?: boolean
}

type AsyncData<DataT, ErrorT> = {
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.api/1.composables/use-fetch.md
Expand Up @@ -32,7 +32,7 @@ type UseFetchOptions = {
type AsyncData<DataT> = {
data: Ref<DataT>
pending: Ref<boolean>
refresh: (opts?: { override?: boolean }) => Promise<void>
refresh: (opts?: { dedupe?: boolean }) => Promise<void>
execute: () => Promise<void>
error: Ref<Error | boolean>
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxt/src/app/composables/asyncData.ts
Expand Up @@ -39,7 +39,7 @@ export interface AsyncDataExecuteOptions {
* not be cancelled, but their result will not affect the data/pending state - and any
* previously awaited promises will not resolve until this new request resolves.
*/
override?: boolean
dedupe?: boolean
}

export interface _AsyncData<DataT, ErrorT> {
Expand Down Expand Up @@ -122,7 +122,7 @@ export function useAsyncData<

asyncData.refresh = asyncData.execute = (opts = {}) => {
if (nuxt._asyncDataPromises[key]) {
if (!opts.override) {
if (opts.dedupe === false) {
// Avoid fetching same key more than once at a time
return nuxt._asyncDataPromises[key]
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/basic/pages/useAsyncData/override.vue
Expand Up @@ -15,14 +15,14 @@ if (count || data.value !== 1) {
}

timeout = 100
const p = refresh()
const p = refresh({ dedupe: true })

if (process.client && (count !== 0 || data.value !== 1)) {
throw new Error('count should start at 0')
}

timeout = 0
await refresh({ override: true })
await refresh()

if (process.client && (count !== 1 || data.value !== 1)) {
throw new Error('override should execute')
Expand Down