Apollo 客户端缓存和 Next.js

时间:2021-01-16 16:08:15

标签: reactjs next.js server-side-rendering apollo-client apollo-cache-inmemory

大家!

我使用 Next.js 和 getStaticProps 函数来获取服务器端的数据。它可以很好地获取数据,但有一个例外。

我在执行函数 Can't find field '<field>' on ROOT_QUERY object 后收到错误 client.readQuery({ query: QUERY })

当我在客户端查看 client.cache.data.data 时,我注意到我试图从缓存中获取并且在服务器端获取的字段不在 ROOT_QUERY 中,而是在它附近(红色)。

在 ROOT_QUERY 中,我只能看到在客户端获取的字段(绿色)。 enter image description here

但在服务器端,ROOT_QUERYenter image description here 中有所有字段。

当然我可以通过 props 传递获取的数据并使用它们,但我想改用缓存。

如果有人遇到这个问题,我想收到如何处理这个问题的建议。 谢谢。

Here is my apollo-client configuration.

1 个答案:

答案 0 :(得分:0)

抱歉,我错过了 _app.tsx 中的 initialApolloState

enter image description here

getStaticProps()

enter image description here

相关问题