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

Cache persists but doesn't prevent network requests #538

Open
70nyIT opened this issue Dec 12, 2024 · 0 comments
Open

Cache persists but doesn't prevent network requests #538

70nyIT opened this issue Dec 12, 2024 · 0 comments

Comments

@70nyIT
Copy link

70nyIT commented Dec 12, 2024

Description

While using apollo-cache-persist, the cache is successfully persisting across page refreshes, but it's not preventing unnecessary network requests as expected.

This is how I set up the client

    await persistCache({
      cache,
      storage: window.localStorage,
    });

    apolloClient = new ApolloClient({
      defaultOptions: {
        watchQuery: {
          fetchPolicy: 'cache-and-network',
          nextFetchPolicy: 'cache-first',
          skipPollAttempt: () => document.hidden,
        },
      },
      cache,
    });

Expected Behavior

  1. First query after page refresh: network request + cache
  2. Subsequent identical queries: cache-only, no network request

Actual Behavior

Every query results in a network request, even when querying the same endpoint with identical variables, regardless of whether it's the first or subsequent call.
I'm defining the same query in two different components, and when also the second component renders, the network call is performed, while I expect the second call to go under nextFetchPolicy (so cache-first).

Environment

  • apollo-cache-persist version: 0.15.0
  • Apollo Client version: 3.12.2
  • React version: 18.2.0

Additional Context

The cache appears to persist correctly, but Apollo Client isn't utilizing it to prevent network requests. This occurs even when the same query is executed in different components.

Any insights on how to configure apollo-cache-persist to achieve the expected caching behavior would be greatly appreciated.

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

No branches or pull requests

1 participant