You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: Better structural sharing in tanstack-query #6306
TRPC uses superjson by default to provide types beyond json-serializable ones, however this breaks default structural sharing in tanstack-query.
The most obvious impact is that any native Date field in a response object will cause the data object returned by a useQuery hook to have a different identity.
Given that trpc uses superjson by default, I think I'd like to see a default structuralSharing function that handles equality for the types handled by superjson.
Describe alternate solutions
Userland functions
An external package that provides the functionality and can be easily dropped in. This might already exist but I was not able to find a suitable lib.
Lobby popular parsing packages (superjson, I use devalue) to add replaceEqualDeep equivalent functions, since the logic of the two operations are tightly related. Using two different algos could introduce very hard-to-detect bugs.
Additional information
Adding a default function would be a breaking change :)
I don't know what the performance impact of everything would be.
👨👧👦 Contributing
🙋♂️ Yes, I'd be down to file a PR implementing this feature!
We only support json serializable values on purpose. If we start to support date, we also need Set / Map and everything else. You can always pass your own function to structuralSharing
I created a proposal for remeda for a replaceEqualDeep function that handles dates, maps, sets, and regexes: remeda/remeda#1369, you could copy/paste that one.
Though I don't use SuperJSON or tRPC, I ran into this in TanStack Query when returning these more exotic objects from select functions.
Is this still wanted? I'd like to pick it up. #6311 was closed in the Nov 2025 cleanup, and the classic package won't get significant new features, so I'd like to confirm where this goes before writing code.
It's still needed on main. TanStack's replaceEqualDeep returns next for anything that isn't a plain object or array (utils.ts#L396-L398). So with superjson, every Date/Map/Set gets a new identity on each refetch, and a useEffect(..., [data.values]) form reset fires on window refocus. TanStack accepts a function instead (replaceData), and tRPC doesn't ship one.
Proposal:
A pure, dependency-free function exported from @trpc/tanstack-react-query, opt-in:
Scope: TanStack's plain object/array rules, plus Date, Map, Set and RegExp. That's a structured-clone subset, not tied to superjson, so devalue users get it too. BigInt and NaN are primitives and compared with Object.is.
Unit tests, plus a superjson integration test where a refetch keeps data identity.
This builds on @aldebout's work in #6311, and I'll credit it. I'd start fresh from TanStack's replaceEqualDeep rather than revive that branch. I ran its final version: [{ a: 1 }] → [{ a: 2 }] returns the old array (the toString check flagged in review), and a Map with a changed value returns the old map (entries read with prev[key], not .get()). @aldebout, happy to step aside if you'd rather finish it.
Questions:
@trpc/tanstack-react-query only, or also a re-export from @trpc/react-query?
Is that scope right, or should it match superjson's type list exactly?
Naming: structuralSharing / createStructuralSharing, or closer to TanStack's replaceEqualDeep?
(AI-assisted: drafted with Claude, reviewed by me.)
🤖 TanStack Query's default structural sharing only compares plain objects and arrays, so with a transformer like superjson, a Date, Map or Set gets a new reference on every refetch. You can already pass your own structuralSharing function through queryOptions() today. In v12, queryOptions() sets one that treats equal Dates, Maps and Sets as unchanged, and exports it for other queries: https://v12.trpc.io/v12/docs/integrations/tanstack-query#structural-sharing. v12 isn't released yet and there's no ETA.
Activity
KATT commentedon Dec 10, 2024
Happy to have our react-query package to include a utility function that worked for this, would you be down to making it happen?
aldebout commentedon Dec 11, 2024
Happy to take a stab at it!
Two main questions for which direction to go:
@juanrgon happy to get your input on this :)
juanrgon commentedon Dec 11, 2024
@aldebout
Personally, I would prefer this to be built-in behavior of
trpcI don't think it should be specific to
supersjson, because other transformers besidessuperjsonare supported by trpc.I like @gaearon's suggestion of using a subset of Structured Clone Javascript Types: TanStack/query#6481 (comment)
KATT commentedon Dec 11, 2024
(please don't @ random people that aren't related to thread or tRPC, it's not nice to their GitHub notifications)
aldebout commentedon Dec 11, 2024
@KATT I DMed you on discord :)
zeorin commentedon Jun 10, 2026
I created a proposal for remeda for a
replaceEqualDeepfunction that handles dates, maps, sets, and regexes: remeda/remeda#1369, you could copy/paste that one.Though I don't use SuperJSON or tRPC, I ran into this in TanStack Query when returning these more exotic objects from select functions.
IslamElsayed commentedon Oct 20, 2026
Is this still wanted? I'd like to pick it up. #6311 was closed in the Nov 2025 cleanup, and the classic package won't get significant new features, so I'd like to confirm where this goes before writing code.
It's still needed on main. TanStack's
replaceEqualDeepreturnsnextfor anything that isn't a plain object or array (utils.ts#L396-L398). So with superjson, everyDate/Map/Setgets a new identity on each refetch, and auseEffect(..., [data.values])form reset fires on window refocus. TanStack accepts a function instead (replaceData), and tRPC doesn't ship one.Proposal:
@trpc/tanstack-react-query, opt-in:Date,Map,SetandRegExp. That's a structured-clone subset, not tied to superjson, so devalue users get it too. BigInt andNaNare primitives and compared withObject.is.createStructuralSharing({ isEqual })for custom leaf types such asTemporal.PlainDate, as you asked for in Better structural sharing for complex types #6311.dataidentity.This builds on @aldebout's work in #6311, and I'll credit it. I'd start fresh from TanStack's
replaceEqualDeeprather than revive that branch. I ran its final version:[{ a: 1 }]→[{ a: 2 }]returns the old array (thetoStringcheck flagged in review), and aMapwith a changed value returns the old map (entries read withprev[key], not.get()). @aldebout, happy to step aside if you'd rather finish it.Questions:
@trpc/tanstack-react-queryonly, or also a re-export from@trpc/react-query?structuralSharing/createStructuralSharing, or closer to TanStack'sreplaceEqualDeep?(AI-assisted: drafted with Claude, reviewed by me.)
KATT commentedon Nov 2, 2026
🤖 TanStack Query's default structural sharing only compares plain objects and arrays, so with a transformer like superjson, a
Date,MaporSetgets a new reference on every refetch. You can already pass your ownstructuralSharingfunction throughqueryOptions()today. In v12,queryOptions()sets one that treats equalDates,Maps andSets as unchanged, and exports it for other queries: https://v12.trpc.io/v12/docs/integrations/tanstack-query#structural-sharing. v12 isn't released yet and there's no ETA.IslamElsayed commentedon Nov 3, 2026
Thanks, that covers it, so I won't start on this.
(AI-assisted: drafted with Claude, reviewed by me.)