【发布时间】:2019-07-06 17:27:09
【问题描述】:
我注意到,当我在客户端本地更新缓存并将其路由到另一个页面时,缓存会与数据一起保留。
但是,当我刷新该页面时,缓存被清除。有没有办法在刷新后保持缓存状态?
【问题讨论】:
标签: react-apollo next.js apollo-link-state
我注意到,当我在客户端本地更新缓存并将其路由到另一个页面时,缓存会与数据一起保留。
但是,当我刷新该页面时,缓存被清除。有没有办法在刷新后保持缓存状态?
【问题讨论】:
标签: react-apollo next.js apollo-link-state
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 更友好。
【讨论】: