Skip to content

Commit f39e1ba

Browse files
committed
[React SDK] Feature: Improved React Query Caching (#4446)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR improves React Query caching performance by optimizing structural sharing in the `ThirdwebProviderCore`. ### Detailed summary - Added `structuralSharing` function to optimize caching performance - Implemented `deepEqual` function for deep comparison - Updated `ThirdwebProviderCore` to use `structuralSharing` for better caching efficiency > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent a2d2291 commit f39e1ba

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

.changeset/two-bottles-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Improve React Query caching performance

packages/thirdweb/src/react/core/providers/thirdweb-provider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isBaseTransactionOptions } from "../../../transaction/types.js";
66
import type { Hex } from "../../../utils/encoding/hex.js";
77
import { isObjectWithKeys } from "../../../utils/type-guards.js";
88
import type { ConnectionManager } from "../../../wallets/manager/index.js";
9+
import { structuralSharing } from "../utils/structuralSharing.js";
910
import { SetRootElementContext } from "./RootElementContext.js";
1011
import { ConnectionManagerCtx } from "./connection-manager.js";
1112
import { invalidateWalletBalance } from "./invalidateWalletBalance.js";
@@ -63,6 +64,7 @@ export function ThirdwebProviderCore(props: {
6364
// With SSR, we usually want to set some default staleTime
6465
// above 0 to avoid refetching immediately on the client
6566
staleTime: 60 * 1000,
67+
structuralSharing,
6668
},
6769
},
6870
}),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { replaceEqualDeep } from "@tanstack/react-query";
2+
3+
/** Forked from https://github.com/epoberezkin/fast-deep-equal */
4+
// biome-ignore lint/suspicious/noExplicitAny: This function by nature takes any object
5+
export function deepEqual(a: any, b: any) {
6+
if (a === b) return true;
7+
8+
if (a && b && typeof a === "object" && typeof b === "object") {
9+
if (a.constructor !== b.constructor) return false;
10+
11+
let length: number;
12+
let i: number;
13+
14+
if (Array.isArray(a) && Array.isArray(b)) {
15+
length = a.length;
16+
if (length !== b.length) return false;
17+
for (i = length; i-- !== 0; ) if (!deepEqual(a[i], b[i])) return false;
18+
return true;
19+
}
20+
21+
if (a.valueOf !== Object.prototype.valueOf)
22+
return a.valueOf() === b.valueOf();
23+
if (a.toString !== Object.prototype.toString)
24+
return a.toString() === b.toString();
25+
26+
const keys = Object.keys(a);
27+
length = keys.length;
28+
if (length !== Object.keys(b).length) return false;
29+
30+
for (i = length; i-- !== 0; )
31+
// biome-ignore lint/style/noNonNullAssertion: We know its there
32+
if (!Object.prototype.hasOwnProperty.call(b, keys[i]!)) return false;
33+
34+
for (i = length; i-- !== 0; ) {
35+
const key = keys[i];
36+
37+
if (key && !deepEqual(a[key], b[key])) return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
// true if both NaN, false otherwise
44+
// biome-ignore lint/suspicious/noSelfCompare: <explanation>
45+
return a !== a && b !== b;
46+
}
47+
48+
export function structuralSharing<T>(oldData: T | undefined, newData: T) {
49+
if (deepEqual(oldData, newData)) {
50+
return oldData as T;
51+
}
52+
return replaceEqualDeep(oldData, newData) as T;
53+
}

0 commit comments

Comments
 (0)