我正在使用react-apollo和apollo-client开发nextJS。我正在使用apollo-cache-persist来保存我的数据。但是当我刷新页面时,数据会丢失。
我尝试了此处提供的解决方案,但遗憾的是没有用。
import React from "react";
import Head from "next/head";
import App, { Container } from "next/app";
import { ApolloClient } from "apollo-client";
import {ApolloProvider} from "react-apollo";
import { InMemoryCache } from "apollo-cache-inmemory";
import { createHttpLink } from "apollo-link-http";
import { withClientState } from "apollo-link-state";
import fetch from "node-fetch";
import { resolvers, defaults } from "../Container/resolvers";
import { ApolloLink } from "apollo-link";
const cache = new InMemoryCache();
const stateLink = withClientState({
cache,
defaults,
resolvers
});
const httpLink = new createHttpLink({
fetch,
uri: "http://localhost:4444/",
headers: {
"Content-Type": "application/json"
}
});
export const client = new ApolloClient({
link: ApolloLink.from([stateLink, httpLink]),
cache
});
export default class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
return {
pageProps: {
// Call page-level getInitialProps
...(Component.getInitialProps
? await Component.getInitialProps(ctx)
: {})
}
};
}
async ComponentWillMount() {
await persistCache({
cache
});
}
render() {
const { Component, pageProps, store } = this.props;
return (
<ApolloProvider client={client}>
<Container>
<Head>
<title>HELLO WORLD</title>
</Head>
<Component {...pageProps} />
</Container>
</ApolloProvider>
);
}
}
我希望缓存能够保留,但是重新加载时缓存什么也不会返回。