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

Warn if ssrMode or ssrForceFetchDelay are used. #358

Merged
merged 4 commits into from
Oct 21, 2024

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Sep 4, 2024

I've seen this a few times too often now.

@phryneas phryneas requested a review from a team as a code owner September 4, 2024 12:01
Copy link

netlify bot commented Sep 4, 2024

Deploy Preview for apollo-client-nextjs-docmodel ready!

Name Link
🔨 Latest commit 197ba3e
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-nextjs-docmodel/deploys/6716073220d61c000845567e
😎 Deploy Preview https://deploy-preview-358--apollo-client-nextjs-docmodel.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines +98 to +100
for (const warning of warnings) {
console.warn(warning, info.pkg, "ApolloClient");
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is down here because we need access to info, which relies on this, which is only available after the super call - but we want to remove these options before that call.

Copy link
Contributor

github-actions bot commented Sep 4, 2024

size-limit report 📦

Path Size
{ ApolloNextAppProvider, ApolloClient, InMemoryCache } from '@apollo/experimental-nextjs-app-support' (Browser ESM) 4.93 KB (+2.19% 🔺)
{ WrapApolloProvider, ApolloClient, InMemoryCache } from '@apollo/client-react-streaming' (Browser ESM) 1.8 KB (+6.46% 🔺)
{ buildManualDataTransport } from '@apollo/client-react-streaming/manual-transport' (Browser ESM) 3 KB (+3.55% 🔺)
@apollo/client-react-streaming (Browser ESM) 2.51 KB (+4.43% 🔺)
@apollo/client-react-streaming (SSR ESM) 7.34 KB (+1.35% 🔺)
@apollo/client-react-streaming (RSC ESM) 7.18 KB (+1.52% 🔺)
@apollo/client-react-streaming/manual-transport (Browser ESM) 3.21 KB (+3.4% 🔺)
@apollo/client-react-streaming/manual-transport (SSR ESM) 8.3 KB (+1.18% 🔺)
@apollo/experimental-nextjs-app-support (Browser ESM) 5.52 KB (+1.99% 🔺)
@apollo/experimental-nextjs-app-support (SSR ESM) 10.68 KB (+0.97% 🔺)
@apollo/experimental-nextjs-app-support (RSC ESM) 7.23 KB (+1.42% 🔺)
@apollo/experimental-nextjs-app-support/rsc (RSC ESM) 6.38 KB (0%)

Copy link

relativeci bot commented Sep 4, 2024

#264 Bundle Size — 1.1MiB (+0.03%).

197ba3e(current) vs e6bfd3a main#263(baseline)

Warning

Bundle contains 1 duplicate package – View duplicate packages

Bundle metrics  Change 1 change Regression 1 regression
                 Current
#264
     Baseline
#263
Regression  Initial JS 924.27KiB(+0.04%) 923.9KiB
No change  Initial CSS 70B 70B
No change  Cache Invalidation 22.66% 22.66%
No change  Chunks 33 33
No change  Assets 57 57
No change  Modules 588 588
No change  Duplicate Modules 99 99
No change  Duplicate Code 6.23% 6.23%
No change  Packages 26 26
No change  Duplicate Packages 1 1
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#264
     Baseline
#263
Regression  JS 1.09MiB (+0.03%) 1.09MiB
No change  Other 8.78KiB 8.78KiB
No change  CSS 70B 70B

Bundle analysis reportBranch pr/warn-on-useless-optionsProject dashboard


Generated by RelativeCIDocumentationReport issue

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.

Good call on this one!

Copy link

pkg-pr-new bot commented Oct 21, 2024

yarn@berry undefined https://pkg.pr.new/apollographql/apollo-client-nextjs/@apollo/client-react-streaming@358
yarn@berry undefined https://pkg.pr.new/apollographql/apollo-client-nextjs/@apollo/experimental-nextjs-app-support@358

commit: 197ba3e

@phryneas phryneas merged commit 4834f55 into main Oct 21, 2024
23 of 24 checks passed
@phryneas phryneas deleted the pr/warn-on-useless-options branch October 21, 2024 07:53
@t-tiger
Copy link

t-tiger commented Dec 25, 2024

@phryneas
May I ask a question about this implementation?

Actually, this change is causing an issue with the application we are using. Specifically, with PreloadQuery, a query is first executed on the RSC, and then the same query is executed again on the client side. This is causing duplicate requests in all places where we are using useSuspenseQuery.

Until the PR is shipped, I had been able to prevent the client-side query execution after the RSC query by setting ssrMode and ssrForceFetchDelay. However, since these options have been removed, duplicate requests are now occurring.

I think there may be others facing the same issue. Do you know of any way to prevent duplicate requests between RSC and the client side? Or is there any point I might be overlooking?

@phryneas
Copy link
Member Author

phryneas commented Jan 6, 2025

@phryneas May I ask a question about this implementation?

Actually, this change is causing an issue with the application we are using. Specifically, with PreloadQuery, a query is first executed on the RSC, and then the same query is executed again on the client side. This is causing duplicate requests in all places where we are using useSuspenseQuery.

Until the PR is shipped, I had been able to prevent the client-side query execution after the RSC query by setting ssrMode and ssrForceFetchDelay. However, since these options have been removed, duplicate requests are now occurring.

I think there may be others facing the same issue. Do you know of any way to prevent duplicate requests between RSC and the client side? Or is there any point I might be overlooking?

Hi @t-tiger,
most of the purpose of the @apollo/experimental-nextjs-app-support package is to transport that data over and prevent that duplicate network request from happening - so if you see a double network request, you have run into a bug we have to fix and ssrForceFetchDelay would only add a band-aid, hiding the real culprit here.
Could you maybe create a small reproduction of the problem and open a new issue?

@t-tiger
Copy link

t-tiger commented Jan 22, 2025

@phryneas

I'm sorry for the delayed response. Allow me to ask the follow-up question 🙇

I think I could understand the design intent of the library. After reading this comment, I see that @apollo/experimental-nextjs-app-support is designed to make the client transparent to server-side and use cache-first for fetchPolicy.

Duplicate requests can be reproduced by setting the fetchPolicy to network-and-cache, where PreloadQuery triggers once when the page loads, and then useSuspenseQuery triggers it again.

However, my use case differs. Here are the underlying conditions:

  1. We use both useSuspenseQuery and useQuery. Since fetchPolicy for useQuery needs to be network-and-cache, default fetchPolicy is also set to network-and-cache.
  2. When browser back/forward occurs, server components are not re-rendered. But we need to ensure the data remains up-to-date, client-side has to refetch.

For point 1, it's difficult for developers to keep specifing cache-first for useSuspenseQuery and network-and-cache for useQuery in a large-scale development project. Therefore, I would like to unify the default fetchPolicy.

For point 2, when browser back/forward occurs, Next.js does not re-render page.tsx and stale data will be displayed if fetchPolicy is cache-first. However, due to the nature of our application, we need to refetch the data.

When these two cases combine, using ssrMode and ssrForceFetchDelay is desired. Without them, it's not possible to achieve both.

I think this kind of situation can occur, but is the design intent of the library is to use cache-first and remain transparent to server-side?

@phryneas
Copy link
Member Author

phryneas commented Jan 22, 2025

@t-tiger

Since the fetch policy for useQuery needs to be network-and-cache

That fetch policy is the right choice only in very rare cases, I would not recommend to use it as a default, especially not without combining it with a nextFetchPolicy. It might lead to a lot more network requests than you might expect.

When browser back/forward occurs, server components are not re-rendered. But we need to ensure the data remains up-to-date, client-side has to refetch.

As a user, honestly I would be quite taken aback if I were to press the back button and get different information than I saw previously - but this is your call to make in the end.

When these two cases combine, using ssrMode and ssrForceFetchDelay is desired.

You might have gotten your desired result on accident, but you are really relying on undefined behaviour here that might change on a whim. This library is not designed to be used with these options and they are not supported.


What I read here is that you are running into problems using the hooks in certain situations with certain network policies - this seems like a XY-Problem: We are discussing ssrMode where we should actually discuss fetch policies.
We might need to adjust the hooks to enforce certain fetch policies during SSR (useQuery already is pinned to "cache-only" during SSR as it would otherwise make network requests that never wait for a result) - could you please open an issue on that topic so we can discuss that instead? (And please, let's do this in an issue and not in a PR that was closed months ago).

@t-tiger
Copy link

t-tiger commented Jan 22, 2025

Thank you for your response. Understood. It would have been better to open an issue rather than asking in the PR. My apologies for it.

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.

3 participants