-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check if dates are equal in structural sharing #6481
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 8743ce8:
|
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 |
@TkDodo I understand this rationale, but:
I'm currently doing this in my project. However, it took me over 6 months to understand this was happening. I had the false assumption that react-query by design always triggered a render when any data was loaded, because virtually all of my queries include a date object (e.g. I don't want to push too hard because there is a work-around, but I'm assuming that I'm not the only one who has this issue (regardless of if others know this or not) Also, thank you for the quick reply! 🙇🏽 |
Not really. The main use-case for TanStack Query is still to manage async state that comes from data fetching, where only JSON is supported. You're using superjson with trpc, and that's a different story. If we would need to support everything that superjson supports, we would need to probably add superjson itself, which means another 3kb of bundle size, which is not acceptable for a somewhat niche use-case. My suggestion here is that this is something that trpc would need to handle if you don't want to handle this in user land. They create the QueryClient for you, so if they know that you use the superjson transform, they should also provide a better For "vanilla" react query, this is definitely out of scope, sorry. |
Is there a better way to customize the |
use |
superjson is much slower than I don't think it makes sense to add another dependency, when all we want is to add a few line to the built-in
|
then just copy paste our code into your own, it's fine. Or don't send dates from the backend but serialize it to a ISO-8601 string and parse it again on the frontend.
Really, we're not going to go the route of starting to serialize non json data. I hope I made that clear. You're free to disagree with that of course, and you have multiple options to work around this limitation. |
This may be very tangential but I wanted to note that RSC has built-in support for more things than JSON — partially inspired by what the Although RQ by itself is unrelated to RSC, for people using the two together, it won't be uncommon to plumb some dates down from the server — given that they can serialize well and are unambiguous (and RSC supports it). So it's a bit unfortunate that RQ becomes a limiting factor here breaking a default optimization. It's easy to miss why it broke without very targeted debugging. Will become more of an issue as passing dates down becomes more common. Another case in which we'd bump into that is custom SDKs that might include dates in their hydrated response objects. It's fair to say that these might be too custom but that's how we accidentally regressed on this (bluesky-social/social-app#3785 is the fix). A date crept in, the code kept working, the optimization broke. So I wanted to voice support for raising the bar from JSON-serializable to some superset. Of course you'd have to choose which superset, and there could be an endless debate where nobody goes away happy. But you could consider a pragmatic subset of Structured Clone, maybe a narrower one than React did. Curiously, RQ is already technically doing that because its comparison algorithm already handles I think that makes the argument for adding Thanks for considering! |
Proposal. We could start with Structured Clone JavaScript Types and narrow it down. Let's exclude a few for a similar reason React excluded them pragmatically:
Then let's exclude binary and low-level stuff:
We're left with |
Thanks for taking the time to post these details. We currently have multiple places where we say that we need json serializable data:
All of these places can be customized:
If we draw the line at I usually recommend superjson because they provide a thin wrapper around I can see that this is difficult for |
Closes: #6482