Releases: vercel/swr
1.2.1
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
- @sairajchouhan made their first contribution in #1816
Full Changelog: 1.2.0...1.2.1
1.2.0
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
- feat: Optimistic mutation with error rollback by @shuding in #1745
- fix: Required return type when mutating by @RKatarine in #1772
- fix: use the latest reference of fetcher with suspense mode by @koba04 in #1803
- test: add delay for flaky focus test by @huozhi in #1762
- test: remove flaky focus test case by @huozhi in #1793
- test: use @swc/jest by @huozhi in #1790
- chore: Ignore coverage report by @shuding in #1744
- chore: Merge mutation states by @shuding in #1748
- chore: Rename CONCURRENT_REQUESTS to FETCH by @shuding in #1749
- chore: Merge extra request states in cache by @shuding in #1752
- chore: support mjs exports by @huozhi in #1760
- chore: remove useless react-native field by @huozhi in #1761
- chore: comment helper by @huozhi in #1757
- chore: display name in devtool by @huozhi in #1779
- chore: simplify example development by @huozhi in #1787
- chore: build: improve watch commands by @huozhi in #1788
- chore: Update examples and dependencies by @shuding in #1789
- chore: zeit -> vercel by @huozhi in #1791
- chore: Clean up configurations by @shuding in #1792
- chore: move community health files to .github dir by @huozhi in #1794
- chore: Add link to security email directly by @huozhi in #1795
New Contributors
- @RKatarine made their first contribution in #1772
Full Changelog: 1.1.2...1.2.0
1.2.0-beta.1
What's Changed
- doc: comment helper by @huozhi in #1757
- build: support mjs exports by @huozhi in #1760
- chore: remove useless react-native field by @huozhi in #1761
- test: add delay for flaky focus test by @huozhi in #1762
- Required return type when mutating by @RKatarine in #1772
- polish: display name in devtool by @huozhi in #1779
New Contributors
- @RKatarine made their first contribution in #1772
Full Changelog: 1.1.2...1.2.0-beta.1
1.2.0-beta.0
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
- feat: Optimistic mutation with error rollback by @shuding in #1745
- chore: Ignore coverage report by @shuding in #1744
- chore: Merge mutation states by @shuding in #1748
- chore: Rename CONCURRENT_REQUESTS to FETCH by @shuding in #1749
- Merge extra request states in cache by @shuding in #1752
Full Changelog: 1.1.2...1.2.0-beta.0
1.1.2
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 tomutate
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
- @thomaspaulmann made their first contribution in #1707
- @houkanshan made their first contribution in #1714
Full Changelog: 1.1.1...1.1.2
1.1.2-beta.1
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
- type(infinite): export SWRInfinteHook and InfiniteKeyLoader from infinite 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
New Contributors
- @houkanshan made their first contribution in #1714
Full Changelog: 1.1.2-beta.0...1.1.2-beta.1
1.1.2-beta.0
What's Changed
- Make polyfill for
requestAnimationFrame
more robust by @thomaspaulmann in #1707
New Contributors
- @thomaspaulmann made their first contribution in #1707
Full Changelog: 1.1.1...1.1.2-beta.0
1.1.1
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
- feat: refreshInterval as a function by @Andrewnt219 in #1690
- test: add refreshInterval function returns 0 by @Andrewnt219 in #1691
- fix: If
newData
is deeply to the latest state, broadcast the latest state by @icyJoseph in #1697 - fix: Return
undefined
data if key's falsy under suspense mode by @icyJoseph in #1698
New Contributors
- @Andrewnt219 made their first contribution in #1690
- @icyJoseph made their first contribution in #1697
Full Changelog: 1.1.0...1.1.1
1.1.0
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
What's Changed
- Feature: make return state(data, error and isValidating) readonly by @anirudh1713 in #1431
- Stable hash by @shuding in #1429
- test: add render utilities and remove TestSWRConfig by @koba04 in #1435
- Revert "Feature: make return state(data, error and isValidating) readonly" by @huozhi in #1443
- Fix initial isValidating when isPaused() returns true by @anothertempore in #1440
- build: no emit on error by @huozhi in #1446
- chore: remove unused typings by @huozhi in #1448
- enhance: provider release handlers by @huozhi in #1449
- test: refactor use-swr-cache-test by @koba04 in #1442
- Chore: refactor code related to unsubscribing global listeners by @shuding in #1451
- Chore: code refactoring for swr/infinite by @shuding in #1452
- test: refactor concurrent rendering tests by @koba04 in #1457
- test: refactor config tests by @koba04 in #1458
- Revert "feat: add a warning for invalid arguments with suspense mode β¦ by @koba04 in #1460
- test: refactor mutation tests by @koba04 in #1459
- fix: Support IE11 by @amannn in #1463
- test: refactor immutable and infinite hook tests by @koba04 in #1466
- chore: Add React 18 to peerDependencies by @shuding in #1467
- test: refactor integration and loading tests by @koba04 in #1473
- test: refactor key, middlewares, and suspense tests by @koba04 in #1474
- test: refactor offline and refresh tests by @koba04 in #1483
- build(deps): bump tmpl from 1.0.4 to 1.0.5 by @dependabot in #1484
- Upgrade typescript to 4.4.3 by @huozhi in #1486
- chore: Code refactoring by @shuding in #1482
- ci: multiple dir type checking by @huozhi in #1492
- Type useSWR fetcher by @promer94 in #1477
- chore: Improve project file structure by @shuding in #1497
- chore: Refine type interfaces by @shuding in #1496
- chore: Upgrade eslint and add local cache by @shuding in #1499
- fix: Invalidate the key when mutating without revalidating by @shuding in #1498
- example: add ref checking condition by @huozhi in #1504
- fix: Refactor core revalidate function and fix isValidating state bug by @shuding in #1493
- chore: remove duplicate configs by @huozhi in #1507
- typing: fix-type-generation by @promer94 in #1509
- chroe: use yalc for local development by @promer94 in #1508
- chore: Code refactoring by @shuding in #1521
- feat: Add new onDiscarded option by @shuding in #1523
- fix: Error events should be deduplicated by @shuding in #1522
- chore: Output test code coverage by @shuding in #1511
- chore: Mark returned states and mutate callback argument as readonly by @shuding in #1514
- test: Add tests for reconnect and merge-config by @shuding in #1530
- fix: Only trigger the success event if not discarded by @shuding in #1529
- chore: Code refactoring by @shuding in #1533
- Add revalidateFirstPage option to swr/infinite (#1401) by @timas130 in #1538
- fix: Reusing cache provider by @shuding in #1539
- chore: fix ts comments by @huozhi in #1540
- fix: Ensure mutate accepts undefined as the data by @shuding in #1515
- chore: Fix comment and startAt type by @shuding in #1541
- chore: Refactor cleanupState by @shuding in #1544
- feat: using latest version of
axios
with fetch interceptor by @danestves in #1548 - test: Add test case for mutation deduplication by @shuding in #1549
- test: speed up with @swc-node/jest by @huozhi in #1569
- chore: Optimizations by @shuding in #1578
- fix react-native issue by @aFINKndreas in #1583
- fix: Support for functions that return false by @TakahiroHimi in #1586
- fix: adds undefined as valid Key type to allow falsy Key or Key() by @morgs32 in #1594
- fix: can't find variable removeEventListener by @zhaomengfan in #1595
- Revert "chore: Mark returned states and mutate callback argument as readonly" by @shuding in #1570
- fix: change config alteration process in withMiddleware to be immutable by @junsushin-dev in #1556
- fix: Allow empty mutate argument list for infinite by @shuding in #1602
- chore: Add
type: "module"
and use.mjs
extension by @shuding in #1604 - Revert "chore: Add type: "module" and use .mjs extension (#1604)" by @huozhi in #1618
- enhance: bind removeEventListener by @huozhi in #1596
- fix: typos by @nguyenyou in #1647
- build: output without minification by @huozhi in #1650
- Tweak types by @promer94 in #1640
- fix: supports ssr in ie11(#1657) by @Sh031224 in #1659
- type: disbale inference when data generic is provided by @promer94 in #1658
- chore: rename major branch to main by @huozhi in #1664
- fix keyloader index and previousPageData type by @promer94 in #1669
- Add defaults for SWRResponse by @TakahiroHimi in #1609
- example: update typescript examples with strict ts mode by @huozhi in #1673
- chore: upgrade eslint to v8 and prettier to v2 by @huozhi in #1674
- fix: remove unnecessary async by @cut0 in #1676
- types: remove duplicated typing def by @huozhi in #1675
- chore: Add test case for
getKey
inuseSWRInfinite
by @shuding in #1681
New Contributors
- @willheslam made their first contribution in #1393
- @louisgv made their first contribution in #1406
- @origin-master made their first contribution in #1409
- @anirudh1713 made their first contribution in #1431
- @amannn made their first contribution in #1463
- @timas130 made their first contribution in #1538
- @danestves made their first contribution in #1548
- @aFINKndreas made their first contribution in #1583
- @TakahiroHimi made their first contribution in #1586
- @zhaomengfan made their first contribution in #1595
- @junsushin-dev made their first contribution in #1556
- @nguyenyou made their first contribution in #1647
- @Sh031224 made their first contribution in #1659
- @cut0 made their first contribution in #1676
Full Changelog: 1.0.1...1.1.0