Skip to content

Cache persists but doesn't prevent network requests #538

Open
@70nyIT

Description

@70nyIT

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions