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

Apollo Client version 3 cache update delay #7572

Closed
sepehrrtl opened this issue Jan 13, 2021 · 2 comments
Closed

Apollo Client version 3 cache update delay #7572

sepehrrtl opened this issue Jan 13, 2021 · 2 comments

Comments

@sepehrrtl
Copy link

The React application that I have is using location.pathname to send a request to my graphql endpoint using useQuery It means whenever I change the route, it sends a request to server to receive the new data. The issue I have is while it's in loading state I would like to keep current data so it should act kind of like Youtube when you click on a video. There should be a progress bar on top of the page and when the data is received, then I will display the fresh new data.

But after migrating from version 2 to version 3 I lost this feature. Now it redirects to new page without data and after a few sec when the data is received, it displays the new data.

How can I keep current cache state while I'm sending a request to server?

BTW I have also SSR.

Below is my setup:

server.ts

const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
  ssrMode: true,
  link: from([errorLink, httpLink]),
  cache,
  typeDefs,
  ssrForceFetchDelay: 100,
});
client

const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
  link: from([errorLink, httpLink]),
  cache: cache.restore(apolloState || {}),
  typeDefs,
});
const usePage = <T = Record<'page', Page>>(options?: Options): QueryResult<T> => {
  const { pathname } = useLocation();

  return useQuery<T>(getQuery(options?.fragmentName, options?.fragment), {
    variables: {
      pathname: options?.pathname || pathname,
    },
    ...(options?.queryOptions || {}),
    errorPolicy: 'all',
  });
};
App

const App = () => {
  const content = usePage();

  if (content.loading) {
      return <ProgressBar />;
  }

  if (!content) {
      return <NoContent action={pageContent.refetch} />;
  }

  return <Page data={content.data} />
}
@gaurabsarkar
Copy link

@sepehrrtl
Copy link
Author

I found the answer here

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants