Skip to content

Commit

Permalink
fix(nuxt)!: refresh to override previous requests by default (nuxt#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Oct 15, 2022
1 parent 6dcff8e commit d862a6b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/content/1.getting-started/6.data-fetching.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit d862a6b

Please sign in to comment.