From 8ea3d942e7bd720fede1b399c50e3c405038b223 Mon Sep 17 00:00:00 2001 From: Pierre Bertet Date: Tue, 24 Aug 2021 17:35:54 +0100 Subject: [PATCH] Upgrade SWR (#157) --- examples/ethereum/yarn.lock | 13 ++++++++----- examples/ethers/yarn.lock | 10 +++++----- examples/fetchWrapper/yarn.lock | 17 ++++++++++++----- package.json | 2 +- src/core.tsx | 22 ++++++++-------------- website/yarn.lock | 10 +++++----- yarn.lock | 10 +++++----- 7 files changed, 44 insertions(+), 40 deletions(-) diff --git a/examples/ethereum/yarn.lock b/examples/ethereum/yarn.lock index 9226d4c..46859b7 100644 --- a/examples/ethereum/yarn.lock +++ b/examples/ethereum/yarn.lock @@ -7062,14 +7062,14 @@ fsevents@~2.3.2: languageName: node linkType: hard -"swr@npm:^1.0.0-beta.10": - version: 1.0.0-beta.10 - resolution: "swr@npm:1.0.0-beta.10" +"swr@npm:1.0.0-beta.13": + version: 1.0.0-beta.13 + resolution: "swr@npm:1.0.0-beta.13" dependencies: dequal: 2.0.2 peerDependencies: react: ^16.11.0 || ^17.0.0 - checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8 + checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c languageName: node linkType: hard @@ -7352,11 +7352,14 @@ typescript@^4.3.2: version: 0.0.0-use.local resolution: "use-nft@portal:../..::locator=example-ethereum%40workspace%3A." dependencies: - swr: ^1.0.0-beta.10 + swr: 1.0.0-beta.13 peerDependencies: + "@ethersproject/contracts": ">=5.2.0 <6" ethers: ">=5.2.0 <6" react: ">=17" peerDependenciesMeta: + "@ethersproject/contracts": + optional: true ethers: optional: true languageName: node diff --git a/examples/ethers/yarn.lock b/examples/ethers/yarn.lock index e701796..2c77e27 100644 --- a/examples/ethers/yarn.lock +++ b/examples/ethers/yarn.lock @@ -6795,14 +6795,14 @@ resolve@~1.17.0: languageName: node linkType: hard -"swr@npm:^1.0.0-beta.10": - version: 1.0.0-beta.10 - resolution: "swr@npm:1.0.0-beta.10" +"swr@npm:1.0.0-beta.13": + version: 1.0.0-beta.13 + resolution: "swr@npm:1.0.0-beta.13" dependencies: dequal: 2.0.2 peerDependencies: react: ^16.11.0 || ^17.0.0 - checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8 + checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c languageName: node linkType: hard @@ -7067,7 +7067,7 @@ typescript@^4.3.2: version: 0.0.0-use.local resolution: "use-nft@portal:../..::locator=example-ethers%40workspace%3A." dependencies: - swr: ^1.0.0-beta.10 + swr: 1.0.0-beta.13 peerDependencies: "@ethersproject/contracts": ">=5.2.0 <6" ethers: ">=5.2.0 <6" diff --git a/examples/fetchWrapper/yarn.lock b/examples/fetchWrapper/yarn.lock index 57bff97..b2f4684 100644 --- a/examples/fetchWrapper/yarn.lock +++ b/examples/fetchWrapper/yarn.lock @@ -9287,14 +9287,14 @@ resolve@^2.0.0-next.3: languageName: node linkType: hard -"swr@npm:^1.0.0-beta.10": - version: 1.0.0-beta.10 - resolution: "swr@npm:1.0.0-beta.10" +"swr@npm:1.0.0-beta.13": + version: 1.0.0-beta.13 + resolution: "swr@npm:1.0.0-beta.13" dependencies: dequal: 2.0.2 peerDependencies: react: ^16.11.0 || ^17.0.0 - checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8 + checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c languageName: node linkType: hard @@ -9740,9 +9740,16 @@ resolve@^2.0.0-next.3: version: 0.0.0-use.local resolution: "use-nft@portal:../..::locator=fetchwrapper%40workspace%3A." dependencies: - swr: ^1.0.0-beta.10 + swr: 1.0.0-beta.13 peerDependencies: + "@ethersproject/contracts": ">=5.2.0 <6" + ethers: ">=5.2.0 <6" react: ">=17" + peerDependenciesMeta: + "@ethersproject/contracts": + optional: true + ethers: + optional: true languageName: node linkType: soft diff --git a/package.json b/package.json index 8b69af4..2bc439e 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "coverage": "npx live-server coverage/lcov-report" }, "dependencies": { - "swr": "^1.0.0-beta.10" + "swr": "1.0.0-beta.13" }, "devDependencies": { "@ethersproject/contracts": "^5.4.1", diff --git a/src/core.tsx b/src/core.tsx index 071fe6b..0ae60f8 100644 --- a/src/core.tsx +++ b/src/core.tsx @@ -20,7 +20,7 @@ import type { EthersFetcherConfig } from "./fetchers/ethers/types" import type { EthereumFetcherConfigDeclaration } from "./fetchers/ethereum/types" import React, { createContext, useCallback, useContext, useMemo } from "react" -import useSWR, { SWRConfig, createCache } from "swr" +import useSWR, { useSWRConfig, SWRConfig } from "swr" import ethersFetcher from "./fetchers/ethers" import ethereumFetcher from "./fetchers/ethereum" import { identity, ipfsUrlDefault } from "./utils" @@ -55,9 +55,9 @@ function normalizeFetcher(fetcher: FetcherProp): Fetcher { // ethereum if (isFetcherDeclarationEthereum(fetcher)) { - return ethereumFetcher(fetcher[1]) as Fetcher< - EthereumFetcherConfigDeclaration - > + return ethereumFetcher( + fetcher[1] + ) as Fetcher } // custom fetcher (or wrong value) @@ -65,7 +65,6 @@ function normalizeFetcher(fetcher: FetcherProp): Fetcher { } const NftContext = createContext<{ - cacheStorage: Map fetcher: Fetcher | null imageProxy: ImageProxyFn ipfsUrl: IpfsUrlFn @@ -89,13 +88,7 @@ const NftProvider: FC<{ throw new Error("Please set the fetcher prop on ") } - const [cacheStorage, { cache: swrCache }] = useMemo(() => { - const cache = new Map() - return [cache, createCache(cache)] - }, []) - const context = { - cacheStorage, fetcher: normalizeFetcher(fetcher), imageProxy, ipfsUrl, @@ -103,7 +96,7 @@ const NftProvider: FC<{ } return ( - + new Map() }}> {children} ) @@ -115,7 +108,7 @@ function useNft(contractAddress: Address, tokenId: string): NftResult { throw new Error("Please wrap your app with ") } - const { cacheStorage, fetcher, imageProxy, ipfsUrl, jsonProxy } = context + const { fetcher, imageProxy, ipfsUrl, jsonProxy } = context const fetchContext = useMemo( () => ({ imageProxy, ipfsUrl, jsonProxy }), [imageProxy, ipfsUrl, jsonProxy] @@ -127,7 +120,8 @@ function useNft(contractAddress: Address, tokenId: string): NftResult { : { ...NFT_METADATA_DEFAULT } }, [contractAddress, fetcher, fetchContext, tokenId]) - const cached = cacheStorage.has(contractAddress + tokenId) + const { cache } = useSWRConfig() + const cached = cache.has(contractAddress + tokenId) const result = useSWR( contractAddress + tokenId, diff --git a/website/yarn.lock b/website/yarn.lock index 82eb1cc..7211f3c 100644 --- a/website/yarn.lock +++ b/website/yarn.lock @@ -2387,14 +2387,14 @@ resolve@^1.20.0: languageName: node linkType: hard -"swr@npm:^1.0.0-beta.10": - version: 1.0.0-beta.10 - resolution: "swr@npm:1.0.0-beta.10" +"swr@npm:1.0.0-beta.13": + version: 1.0.0-beta.13 + resolution: "swr@npm:1.0.0-beta.13" dependencies: dequal: 2.0.2 peerDependencies: react: ^16.11.0 || ^17.0.0 - checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8 + checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c languageName: node linkType: hard @@ -2497,7 +2497,7 @@ typescript@^4.3.5: version: 0.0.0-use.local resolution: "use-nft@portal:..::locator=use-nft-website%40workspace%3A." dependencies: - swr: ^1.0.0-beta.10 + swr: 1.0.0-beta.13 peerDependencies: "@ethersproject/contracts": ">=5.2.0 <6" ethers: ">=5.2.0 <6" diff --git a/yarn.lock b/yarn.lock index 3004009..03bf62a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15331,14 +15331,14 @@ resolve@^2.0.0-next.3: languageName: node linkType: hard -"swr@npm:^1.0.0-beta.10": - version: 1.0.0-beta.10 - resolution: "swr@npm:1.0.0-beta.10" +"swr@npm:1.0.0-beta.13": + version: 1.0.0-beta.13 + resolution: "swr@npm:1.0.0-beta.13" dependencies: dequal: 2.0.2 peerDependencies: react: ^16.11.0 || ^17.0.0 - checksum: 6895e0bc2d849cfb805857fa1f8dbb792136b9bb5177e401f21b0e96e41b2e427de289ee820d4d75850b500e50d8695d89985cf8ea164a5db0e38f45e57bffb8 + checksum: fb552f2d4f4d0e15ee981ffab081ac8cc04a3684c5f60e90ec95949cd92b7006fc0ab8cfd36e2c1d1cf2892623d86753d7c2107d817148b71f044b370e718d0c languageName: node linkType: hard @@ -16215,7 +16215,7 @@ typescript@^4.3.5: react: ^17.0.2 react-dom: ^17.0.2 rollpkg: ^0.5.7 - swr: ^1.0.0-beta.10 + swr: 1.0.0-beta.13 ts-jest: ^27.0.4 typescript: ^4.3.5 peerDependencies: