Skip to content
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

RSC preloading mechanism #258

Merged
merged 60 commits into from
May 15, 2024
Merged

RSC preloading mechanism #258

merged 60 commits into from
May 15, 2024

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Mar 27, 2024

@phryneas phryneas changed the title ApolloClient: make browser event-replaying logic available in SSR RSC preloading mechanism Mar 27, 2024
Copy link

github-actions bot commented Mar 27, 2024

size-limit report 📦

Path Size
{ ApolloNextAppProvider, NextSSRApolloClient, NextSSRInMemoryCache } from '@apollo/experimental-nextjs-app-support/ssr' (Browser ESM) 7.88 KB (+1.2% 🔺)
{ WrapApolloProvider, ApolloClient, InMemoryCache } from '@apollo/client-react-streaming' (Browser ESM) 1.5 KB (+7.48% 🔺)
{ buildManualDataTransport } from '@apollo/client-react-streaming/manual-transport' (Browser ESM) 6.36 KB (+1.5% 🔺)
@apollo/client-react-streaming (Browser ESM) 2.23 KB (+4.91% 🔺)
@apollo/client-react-streaming (SSR ESM) 2.58 KB (+47.44% 🔺)
@apollo/client-react-streaming (RSC ESM) 1.65 KB (+20.62% 🔺)
@apollo/client-react-streaming/manual-transport (Browser ESM) 6.55 KB (+1.5% 🔺)
@apollo/client-react-streaming/manual-transport (SSR ESM) 7.14 KB (+12.55% 🔺)
@apollo/experimental-nextjs-app-support/ssr (Browser ESM) 8.49 KB (+1% 🔺)
@apollo/experimental-nextjs-app-support/ssr (SSR ESM) 9.12 KB (+9.49% 🔺)
@apollo/experimental-nextjs-app-support/ssr (RSC ESM) 867 B (0%)
@apollo/experimental-nextjs-app-support/rsc (RSC ESM) 626 B (0%)

Copy link

relativeci bot commented Mar 27, 2024

#194 Bundle Size — 1.03MiB (+2.47%).

74c2fd2(current) vs afa5bcc main#189(baseline)

Warning

Bundle contains 1 duplicate package – View duplicate packages

Bundle metrics  Change 9 changes Regression 3 regressions Improvement 1 improvement
                 Current
#194
     Baseline
#189
Regression  Initial JS 898.87KiB(+0.82%) 891.55KiB
Regression  Initial CSS 70B(+100%) 0B
Change  Cache Invalidation 88.27% 0.04%
Change  Chunks 29(+20.83%) 24
Change  Assets 53(+17.78%) 45
Change  Modules 528(+3.13%) 512
Regression  Duplicate Modules 47(+56.67%) 30
Change  Duplicate Code 2.57%(+99.22%) 1.29%
Improvement  Packages 26(-10.34%) 29
No change  Duplicate Packages 1 1
Bundle size by type  Change 3 changes Regression 3 regressions
                 Current
#194
     Baseline
#189
Regression  JS 1.02MiB (+2.28%) 1MiB
Regression  Other 8.06KiB (+34.41%) 5.99KiB
Regression  CSS 70B (+100%) 0B

Bundle analysis reportBranch pr/rsc-preloadProject dashboard

@@ -9,7 +9,7 @@
"*"
],
"scripts": {
"trigger-rebuild": "find . -regextype posix-extended -regex '.*/node_modules/@apollo/(client-react-streaming|experimental-nextjs-app-support)' -printf 'rm -r %p\n' -exec rm -r {} +; glob \"../.yarn/cache/@apollo-*exec*\" \"$HOME/.yarn/berry/cache/@apollo-*exec*\" --cmd='rm -v' ; yarn"
"trigger:rebuild": "find . -regextype posix-extended -regex '.*/node_modules/@apollo/(client-react-streaming|experimental-nextjs-app-support)' -printf 'rm -r %p\n' -exec rm -r {} +; glob \"../.yarn/cache/@apollo-*exec*\" \"$HOME/.yarn/berry/cache/@apollo-*exec*\" --cmd='rm -v' ; yarn"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scripts with a : can be called from any child project in a yarn monorepo. Big DX improvement for a single char :D

@phryneas phryneas changed the base branch from main to v0.11.0 April 8, 2024 10:07
@phryneas phryneas added this to the 0.11.0 milestone Apr 8, 2024
@phryneas
Copy link
Member Author

phryneas commented Apr 9, 2024

Quick thought: we need some warning if people add cookies or headers to context here as that could result in secrets to be dumped into the stream on accident.

Copy link

vercel bot commented Apr 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
apollo-client-nextjs-experimental-nextjs-app-support ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 18, 2024 9:24am

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a few questions and a couple minor comments, but I am SO STOKED for this feature. Incredible work 🎉

@@ -1,5 +1,11 @@
import { ApolloLink, Observable } from "@apollo/client";

declare module "@apollo/client" {
export interface DefaultContext {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooo this is really something we should add to our docs. I don't think we recommend this at all and I'm sure lots of users would really appreciate being able to do this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

packages/client-react-streaming/src/PreloadQuery.tsx Outdated Show resolved Hide resolved
…rappedApolloClient.tsx

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
Comment on lines 105 to 114
const unwrapped = unwrapQueryRef<any>(queryRef);
useEffect(() => {
if (cacheKey) {
if (unwrapped.disposed) {
getSuspenseCache(client).add(cacheKey, unwrapped);
}
}
// Omitting the deps is intentional. This avoids stale closures and the
// conditional ensures we aren't running the logic on each render.
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerelmiller should I still do all this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it for useBackgroundQuery here to avoid issues where the queryRef gets disposed by the auto dispose timeout and the hook re-renders (which was a bug before that change in 3.9.10). I did update that check to use a setTimeout though since dispose is now again wrapped in a setTimeout.

I don't remember exactly where you use this hook, but if you're in the same situation as what I described above, then keeping this makes sense.

Copy link
Member Author

@phryneas phryneas May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wraps around useReadQuery and useQueryRefHandlers, and it can also create a queryRef by reviving a transportedQueryRef. We can remove these effects once React deduplicates transported objects, but for now they're in here.

yarn.lock Outdated
@@ -49,14 +49,14 @@ __metadata:
version: 0.0.0-use.local
resolution: "@apollo/client-react-streaming@workspace:packages/client-react-streaming"
dependencies:
"@apollo/client": "npm:3.9.9"
"@apollo/client": "npm:3.10.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to get apollographql/apollo-client#11821 out before we go live with the change in useTransportedQueryRef just to make sure its still compatible with the change in that PR.

Copy link
Member Author

@phryneas phryneas May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be included in the latest build from apollographql/apollo-client#11824

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Let's test this with apollographql/apollo-client#11821 just to make sure that doesn't break something here as well, but otherwise this is incredible work!

Comment on lines +105 to +124
useEffect(() => {
// We only want this to execute if the queryRef is a transported query.
if (!isTransported) return;
// We want to always keep this queryRef in the suspense cache in case another component has another instance of this transported queryRef.
// This effect could be removed after https://github.com/facebook/react/pull/28996 has been merged and we've updated deps to that version.
if (cacheKey) {
if (unwrapped.disposed) {
getSuspenseCache(client).add(cacheKey, unwrapped);
}
}
// Omitting the deps is intentional. This avoids stale closures and the
// conditional ensures we aren't running the logic on each render.
});
// Soft-retaining because useQueryRefHandlers doesn't do it for us.
// This effect could be removed after https://github.com/facebook/react/pull/28996 has been merged and we've updated deps to that version.
useEffect(() => {
if (isTransported) {
return unwrapped.softRetain();
}
}, [isTransported, unwrapped]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerelmiller could you please take a final look at this bit before I merge this in?

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@phryneas phryneas merged commit cc189b8 into v0.11.0 May 15, 2024
14 checks passed
@phryneas phryneas deleted the pr/rsc-preload branch May 15, 2024 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue: investigate prefetching from RSC to Client Components
2 participants