question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Persisting InMemoryCache when using SSR

See original GitHub issue

Hi there,

What is the recommended way to persist InMemoryCache when using server side rendering?

If my app was not server side rendered I could just persist to localStorage.

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

Having trouble trying to figure out how to persist InMemoryCache when using NextJS.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
ajhoolcommented, Nov 1, 2019

In my app, I’m using:

import { persistCache } from 'apollo-cache-persist';
import { InMemoryCache } from 'apollo-cache-inmemory';

const initCache = (initialState?: any) => {
  const cache = new InMemoryCache().restore(initialState || {});
  
  /**
   * Cache uses localStorage to save data.
   * 
   * This cache is used by Apollo (graphql client).
   */
  if(typeof window !== 'undefined') {
    persistCache({
      cache,
      storage: window.localStorage
    });
  }
  
  return cache;
}

export default initCache;

I believe this approach means that SSR won’t be able to access the cache but the client will access the cache rather than making network calls once the initial SSR has completed. All calls made from the client will have access to the cache.

1reaction
adrianos10commented, Oct 14, 2021

I think I’ve managed to add apollo cache persist based on existing example in Next.js - https://github.com/vercel/next.js/pull/29718

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced topics on caching in Apollo Client
You can persist and rehydrate the InMemoryCache from a storage provider like AsyncStorage or localStorage . To do so, use the apollo3-cache-persist library....
Read more >
How to use the apollo-cache-persist.persistCache function in ...
To help you get started, we've selected a few apollo-cache-persist. ... const cache = new InMemoryCache({ fragmentMatcher }) // TODO what should happen...
Read more >
Server Side Rendering – Angular - GraphQL Code Generator
class AppModule { cache: InMemoryCache; constructor( apollo: Apollo, ... SSR works out of the box when using HttpLink from ...
Read more >
API with NestJS #23. Implementing in-memory cache to ...
In this article, we look into in-memory caching to improve the performance of our application.
Read more >
apollo-state-link does not hold state in cache when page is ...
Apollo's InMemoryCache is, well, in-memory, so it's not persisted ... Also, be aware if you're using SSR, there are known issues with using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found