-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(hydration): move hydration utilities to core, keeping old exports #2497
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
feat(hydration): move hydration utilities to core, keeping old exports #2497
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tannerlinsley/react-query/F8EYs2PR87N4j8d1x2fgcDyLPy7F |
|
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 7670638:
|
TkDodo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since hydration.ts is now part of core, shouldn't some of the imports change?
for example, it currently imports:
import type { QueryClient } from '../core/queryClient'
shouldn't that now just be:
import type { QueryClient } from './queryClient'
?
|
also, just questioning if this is really a |
Good catch 🥇
Well, we are exporting new things from the core. Therefore I would say it's a feature. |
src/hydration/index.ts
Outdated
| @@ -1,11 +1,12 @@ | |||
| export { dehydrate, hydrate } from './hydration' | |||
| export { dehydrate, hydrate } from '../core/hydration' | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the hydration package is built as a standalone package, this re-export will result in these functions being included in both core and hydration. I don't think this is a big problem since it's meant to be deprecated, there are no singletons there that could mess things up if you import things from both places, and the payload is small anyway.
This double payload will affect everyone using the React bindings though since those are currently only included in /hydration. I wonder if this can be solved by changing this to export { dehydrate, hydrate } from 'react-query'? Not sure how re-exporting from a package configured as external works though, so would need to be tested.
|
I'm afk until next Wednesday so only gave this a quick glance and didn't test it, but looks good to me if it's thoroughly tested. Probably good to check that the bundle sizes haven't changed unexpectedly much as well, though I don't expect they should have. 😄 |
|
Hey there, any eta when this will land? Would be really cool! |
|
@benbender there is an open question from @Ephem that I would ideally like to be resolved before merging |
|
@Ephem can you take another look please? I'm really no expert in this area 😅 |
|
@DamianOsipiuk Sorry for this taking so long, I've been pretty busy the last few weeks. I'll have a look within the next few days! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! I verified the build is still working in a real project and that the output of the build was as expected (no duplicated code). I think Core grows around 3% or so, probably with some variance for which build and conditions (compression etc).
I noticed the React parts that were left in the hydration package were really tiny, even compared to the things that were already moved, so I took the liberty of pushing a commit to your branch that moved those into the react package (and re-tested after of course). This will let us entirely remove the separate hydration package in a future major.
So to summarize, this MR:
- Moves core hydration utils into the core package
- Moves React hydration-integration into the react package
- Keeps the exports around in the hydration package to avoid breaking change
- UMD and es-builds are both tested to work
- Unlocks hydration for libraries other than React! 🎉
The only downside is that the core and React packages are slightly larger (unless tree-shaking, which should remove this), and any future additions to hydration functionality will affect those instead of a separate package. Since any future changes are likely to be small and of the bug fix variety and the alternative to moving this into core is to maintain a whole bunch of extra packages I think this is the right way to go.
@TkDodo If this seems reasonable to you too, could you do a final check so I haven't missed anything obvious (I'm a bit tired right now..) and then merge this when the timing is good? 😄
@DamianOsipiuk Great work on this! 🌹 Sorry again to have kept you waiting.
I'm personally not so concerned about bundle size, but other people very much are, especially with competition (see swr) is coming with smaller bundle sizes. Which absolute numbers are we talking here, and can we somehow verify that treeshaking (like a standard create-react-app or rollup build) removes the hydration code if not needed? I'm also not so sure if future changes will be small. The persistor plugins use hydration under the hood, and we might need some additional functionality like allowlist / blocklist per query key or so that we might need to build into hydration itself.... |
|
I built master and then rebased this branch to master locally and built that. Here's the comparison numbers for the UMD builds (only including gzip size since we usually look at that): The alternative if we want to support hydration in other frameworks is to have one
Slightly off topic perhaps, but would the
I linked this branch to the basic React Query example, which uses react-scripts. Building the example resulted in this vendor chunk:
I then added a
So treeshaking does seem to work, at least to a big extent. |
|
@Ephem I just build the basic example locally on master, and it results in: so there seems to be some difference to your numbers? |
|
My numbers was this PR, first without using any hydrate functionality (treeshaking condition), second using useHydrate (using some but not all of the hydration functionality). I guess your number from master means there is a whopping 0.01kB that can't be treeshaken then. 😄 Edit: Note that this is after gzip which is more effective on larger files though, so details may vary of course, but definitely looks like treeshaking works at least with react-scripts. |
|
yep, I know. I wanted to have a comparison between the basic example on master vs the basic example on this PR. I tried it out locally now with this PR, depending on and it actually got me down to so, does this mean that this PR actually make the lib 164 bytes smaller? 🤷♂️ |
Oh, I see, sorry, I misunderstood. 😄
This PR is missing a couple of commits from master which is probably why. When I tried this I first rebased locally and used that version. Didn't want to force push the rebase to someone elses branch though. 😄 |
|
right, it's based on 3.19.1, so I compared with that: master, 3.19.1: so, two bytes more, but one byte less after gzip or so. Anyhow, I think this is negligible and since treeshaking seems to work, it will also not affect future additions. Good work 👍 |
|
🎉 This PR is included in version 3.22.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
This appears to cause error for |
|
Can you elaborate a bit please? |
|
I am using
I was able to bypass the error by changing the following import in my local npm package: My fix was a hack during my debugging. I haven't gone deeper but I believe the error is associated with changes made in v3.22.0 |
|
@Ephem @DamianOsipiuk would we need to change the imports for |
|
@TkDodo This could potentially solve the issue, but I do not understand why it stopped working, to be honest. |
|
Can you PR it please, then we can try it out |
|
@cloudmu Could you provide a codesandbox with a reproduction of the issue? Cause I just tried to do it locally and everything works for me. I will provide PR to change the imports anyway, but I want to understand what caused this issue on your setup. |
Following: #2370
Move hydration utilities to the core.
Retain old exports for backward compatibility.
Export additional type that was missed before.