【问题标题】:apollo-state-link does not hold state in cache when page is refreshed刷新页面时,apollo-state-link 不在缓存中保存状态
【发布时间】:2019-07-06 17:27:09
【问题描述】:

我注意到,当我在客户端本地更新缓存并将其路由到另一个页面时,缓存会与数据一起保留。

但是,当我刷新该页面时,缓存被清除。有没有办法在刷新后保持缓存状态?

【问题讨论】:

    标签: react-apollo next.js apollo-link-state


    【解决方案1】:

    Apollo 的 InMemoryCache 是在内存中的,所以它不会在页面加载之间持久化。持久化缓存的推荐方法是使用apollo-cache-persist。示例用法:

    import { InMemoryCache } from 'apollo-cache-inmemory'
    import { persistCache } from 'apollo-cache-persist'
    
    const cache = new InMemoryCache({...})
    
    persistCache({
      cache,
      storage: window.localStorage,
    });
    
    const client = new ApolloClient({
      cache,
      // other client options
    })
    

    有关高级配置和用法,请查看 repo。另外,请注意,如果您使用的是 SSR,则有 known issues 使用此库。您也可以查看apollo-cache-instorage,这可能对 SSR 更友好。

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 2019-06-30
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 2018-06-08
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多