我注意到,当我在客户端本地更新我的缓存并将其路由到另一个页面时,缓存与数据一起存在。
但是,当我刷新该页面时,缓存被清除。有没有办法在刷新后保持缓存状态?
我注意到,当我在客户端本地更新我的缓存并将其路由到另一个页面时,缓存与数据一起存在。
但是,当我刷新该页面时,缓存被清除。有没有办法在刷新后保持缓存状态?
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,则使用此库存在已知问题。您还可以查看apollo-cache-instorage,它可能对 SSR 更友好。