Skip to content

Releases: vercel/swr

1.2.1

02 Feb 18:45
c63cafc
Compare
Choose a tag to compare

Highlights of This Release

shouldRetryOnError accepts a function

Previously shouldRetryOnError is either true or false. Now it accepts a function that conditionally determines if SWR should retry. Here's a simple example:

const fetcher = url => fetch(url).then(res => {
  // Fetcher throws if the response code is not 2xx.
  if (!res.ok) throw res
  return res.json()
})

useSWR(key, fetcher, {
  shouldRetryOnError: (error) => {
    // We skip retrying if the API is returning 404:
    if (error.status === 404) return false
    return true
  }
})

Thanks to @sairajchouhan for contributing!

What's Changed

  • shouldRetryOnError accepts a function that can be used to conditionally stop retrying by @sairajchouhan in #1816
  • build(deps-dev): bump next from 12.0.8 to 12.0.9 by @dependabot in #1821
  • fix: useSWRInfinite revalidates with revalidateOnMount by @koba04 in #1830

New Contributors

Full Changelog: 1.2.0...1.2.1

1.2.0

26 Jan 17:26
bfb9edc
Compare
Choose a tag to compare

Highlights of This Release

Optimistic Updates with Auto Error Rollback

There are now some new options in mutate:

mutate(patchUser(user), {
  optimisticData: user,
  populateCache: true,
  rollbackOnError: true,
  revalidate: true,
})

Here the cache will be immediately updated to user, the β€œoptimistic value”. And then a request (remote mutation) is started via patchUser(user) and the response will be written to the cache. If that request fails, the original result will be rolled back safely so the optimistic value will be gone. And after all those finish, a revalidation will start to fetch the latest value.

This is extremely helpful for building the optimistic UI pattern.

You can do the same for the global mutate, just remember to pass the key. Also, the current mutate APIs stay unchanged so mutate(data, false) works the same.

Here's an example: https://codesandbox.io/s/swr-basic-forked-k5hps.

CleanShot.2022-01-27.at.15.39.58.mp4

.mjs Support

SWR now has .mjs exported for bundlers that prefer this format.

This doesn’t break environments that don’t support .mjs. An alternative .esm.js and CJS bundle are also published.

You can read more about ES modules here.

What's Changed

New Contributors

Full Changelog: 1.1.2...1.2.0

1.2.0-beta.1

12 Jan 17:12
d45b05d
Compare
Choose a tag to compare
1.2.0-beta.1 Pre-release
Pre-release

What's Changed

New Contributors

Full Changelog: 1.1.2...1.2.0-beta.1

1.2.0-beta.0

28 Dec 22:52
d1a76c9
Compare
Choose a tag to compare
1.2.0-beta.0 Pre-release
Pre-release

Highlights of This Release

Dedicated API for Optimistic Updates with Auto Rollback on Error

There are now some new options in mutate:

mutate(patchUser(user), {
  optimisticData: user,
  populateCache: true,
  rollbackOnError: true,
  revalidate: true,
})

Here the cache will be immediately updated to user, the β€œoptimistic value”. And then a request (remote mutation) is started via patchUser(user) and the response will be written to the cache. If that request fails, the original result will be rolled back safely so the optimistic value will be gone. And after all those finish, a revalidation will start to fetch the latest value.

This is extremely helpful for building the optimistic UI pattern.

You can do the same for the global mutate, just remember to pass the key. Also, the current mutate APIs stay unchanged so mutate(data, false) works the same.

Here's an example: https://codesandbox.io/s/swr-basic-forked-k5hps.

What's Changed

Full Changelog: 1.1.2...1.2.0-beta.0

1.1.2

26 Dec 20:32
49ee4e9
Compare
Choose a tag to compare

Highlights of This Release

Use the Latest Fetcher Function

SWR will now use the latest fetcher function passed to the hook, when sending the request. Previously it uses the initial snapshotted fetcher.

Avoid Unnecessary Auto Revalidations

When refocusing on the window with state changes (like clicking a button that changes the SWR key immediately), SWR now avoids revalidations if they're not necessary. Details can be found in #1720.

New Types for useSWRInfinite

Two types for useSWRInfinite are added: SWRInfinteHook and SWRInfinteKeyLoader. You can use them to type the hook and the getKey function.

New populateCache option for mutate

A new option to control if the mutation data should be written to the cache. You can find the details in #1729.

What's Changed

  • Make polyfill for requestAnimationFrame more robust by @thomaspaulmann in #1707
  • type(infinite): export SWRInfinteHook and SWRInfiniteKeyLoader from infinite, rename InfiniteFetcher to SWRInfiniteFetcher by @houkanshan in #1714
  • feat: Ensure auto revalidations are executed after state updates by @shuding in #1720
  • Add moduleNameMapper to jest by @shuding in #1724
  • Improve test by @promer94 in #1725
  • feat: Use the latest reference of fetcher by @shuding in #1727
  • fix: Re-export InfiniteFetcher by @shuding in #1728
  • feat: Add populateCache option to mutate by @shuding in #1729
  • Fix local state sharing example deps by @huozhi in #1737
  • Merge states for concurrent requests by @shuding in #1741
  • Skip error retrying when document is not active and improve tests by @shuding in #1742

New Contributors

Full Changelog: 1.1.1...1.1.2

1.1.2-beta.1

23 Dec 19:33
68dfcc1
Compare
Choose a tag to compare
1.1.2-beta.1 Pre-release
Pre-release

Highlights of This Release

1. More Tests and Better Code Coverage

We now have 23 test suits of 202 total test cases, with a 98.06% test coverage. The core files and main functionalities are now 100% covered. Kudos to @promer94 for improving it!

2. Use the Latest Fetcher Function

SWR will now use the latest fetcher function passed to the hook, when sending the request. Previously it uses the initial snapshotted fetcher.

3. Avoid Unnecessary Auto Revalidations

When refocusing on the window with state changes (like clicking a button that changes the SWR key immediately), SWR now avoids revalidations if they're not necessary. Details can be found in #1720.

4. New Types for useSWRInfinite

Two types for useSWRInfinite are added: SWRInfinteHook and SWRInfinteKeyLoader. You can use them to type the hook and the getKey function.

What's Changed

New Contributors

Full Changelog: 1.1.2-beta.0...1.1.2-beta.1

1.1.2-beta.0

15 Dec 15:44
fd3a649
Compare
Choose a tag to compare
1.1.2-beta.0 Pre-release
Pre-release

What's Changed

New Contributors

Full Changelog: 1.1.1...1.1.2-beta.0

1.1.1

10 Dec 16:06
b78937f
Compare
Choose a tag to compare

Highlights of This Release

Dynamic refreshInterval

You can now pass a function as refreshInterval, to dynamically return the interval (in millisecond) til the next request, based on the current data:

useSWR('key', fetcher, {
  refreshInterval: function (data: Data | undefined) {
    if (!data) return 3000 // Initial request 
    return data.next_update
  }
})

If return 0, polling will be stopped.

What's Changed

New Contributors

Full Changelog: 1.1.0...1.1.1

1.1.0

30 Nov 16:05
16778a7
Compare
Choose a tag to compare

Highlights for This Release

1. Built-in & stable serialization for SWR keys

useSWR({ query: graphql`...`, variables }, fetcher)
useSWR({ query: graphql`...`, variables }, fetcher)
useSWR({ variables, query: graphql`...` }, fetcher)
// ^all recognized as the same resource

For array keys too, it's safe to do the following:

useSWR([ `...`, { variables } ], fetcher)

2. revalidateFirstPage for useSWRInfinite

This is a new added option for useSWRInfinite (defaults to true), you can use it to control if the first page should be revalidated when changing the size or doing a mutation.

3. Better TypeScript support

  • Key / arguments
  • Conditional / dependent
  • Constant keys
  • Returned data

image

What's Changed

New Contributors

Full Changelog: 1.0.1...1.1.0

1.1.0-beta.12

26 Nov 15:14
b3e6e20
Compare
Choose a tag to compare
1.1.0-beta.12 Pre-release
Pre-release

Patches

  • Chore: rename major branch to main: #1664
  • Fix keyloader index and previousPageData type: #1669
  • Fix: Add defaults for SWRResponse: #1609

Credits

Huge thanks to @huozhi, @promer94, and @TakahiroHimi for helping!