Skip to content

feat: Better structural sharing in tanstack-query #6306

Open

Description

@aldebout

Describe the feature you'd like to request

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.

Discussion for context: TanStack/query#6481

Describe the solution you'd like to see

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!

Activity

  1. KATT commented on Dec 10, 2024

    @KATT
    Member

    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

    Originally posted by TkDodo in TanStack/query#6481 (comment)

    Happy to have our react-query package to include a utility function that worked for this, would you be down to making it happen?

  2. added
    ✅ accepted-PRs-welcome
    on Dec 10, 2024
  3. aldebout commented on Dec 11, 2024

    @aldebout
    Author

    Happy to take a stab at it!

    Two main questions for which direction to go:

    • Do we want this to be an export that people can use or are we ok with introducing a breaking change?
    • What scope should we aim for? Same as superjson or a subset?

    @juanrgon happy to get your input on this :)

  4. juanrgon commented on Dec 11, 2024

    @juanrgon

    @aldebout

    Do we want this to be an export that people can use or are we ok with introducing a breaking change?

    Personally, I would prefer this to be built-in behavior of trpc

    What scope should we aim for? Same as superjson or a subset?

    I don't think it should be specific to supersjson, because other transformers besides superjson are supported by trpc.

    I like @gaearon's suggestion of using a subset of Structured Clone Javascript Types: TanStack/query#6481 (comment)

  5. KATT commented on Dec 11, 2024

    @KATT
    Member

    (please don't @ random people that aren't related to thread or tRPC, it's not nice to their GitHub notifications)

  6. aldebout commented on Dec 11, 2024

    @aldebout
    Author

    @KATT I DMed you on discord :)

  7. zeorin commented on Jun 10, 2026

    @zeorin

    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.

  8. IslamElsayed commented on Oct 20, 2026

    @IslamElsayed

    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:
      new QueryClient({ defaultOptions: { queries: { structuralSharing } } })
    • 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.
    • createStructuralSharing({ isEqual }) for custom leaf types such as Temporal.PlainDate, as you asked for in Better structural sharing for complex types #6311.
    • 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:

    1. @trpc/tanstack-react-query only, or also a re-export from @trpc/react-query?
    2. Is that scope right, or should it match superjson's type list exactly?
    3. Naming: structuralSharing / createStructuralSharing, or closer to TanStack's replaceEqualDeep?

    (AI-assisted: drafted with Claude, reviewed by me.)

  9. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 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.

  10. added
    next-major/definite
    on Nov 2, 2026
  11. IslamElsayed commented on Nov 3, 2026

    @IslamElsayed

    Thanks, that covers it, so I won't start on this.

    (AI-assisted: drafted with Claude, reviewed by me.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite✅ accepted-PRs-welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions