Features
Deduplication cooldown

Deduplication cooldown

By default, XSWR uses a 1000ms deduplication cooldown.

That means components won't refetch a resource if it was already fetched less than 1000ms ago.

You can use a custom cooldown to override or disable this behaviour (use 0 to disable).

Basically, the fetch is prevented if Date.now() < state.cooldown.

You can ignore the deduplication cooldown by using query.refetch().

Resource cooldown delay

You can set a cooldown delay for all resources in your core provider.

function MyWrapper() {
  return (
    <CoreProvider cooldown={5000}>
      <MyAwesomeApp />
    </CoreProvider>
  )
}

Or you can set a cooldown delay for some resource.

function getHelloSchema() {
  return getSchema<Hello>("/api/hello", fetchAsJson, { cooldown: 5000 })
}

Response cooldown time

You can also set a cooldown time directly in your fetcher.

async function fetchAsJson<T>(url: string, more: FetcherMore<T>) {
  const { signal } = more
  
  const res = await fetch(url, { signal })
  const cooldown = Date.now() + 5000
 
  if (!res.ok) {
    const error = new Error(await res.text())
    return { error, cooldown }
  }
 
  const data = await res.json() as T
  return { data, cooldown }
}

For more control, cooldown is a time, not a delay (you must add Date.now() if you use a delay).

You can also use response headers like Cache-Control to set the cooldown time accordingly.