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

This commit is contained in:
Daniel Roe 2022-10-15 12:02:23 +01:00 committed by GitHub
parent 6dcff8e428
commit d862a6bfdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 8 deletions

View File

@ -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`

View File

@ -30,7 +30,7 @@ type AsyncDataOptions<DataT> = {
}
interface RefreshOptions {
override?: boolean
dedupe?: boolean
}
type AsyncData<DataT, ErrorT> = {

View File

@ -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>
}

View File

@ -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> {
@ -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]
}

View File

@ -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')