Tu *_*anh 3 apollo next.js apollo-client
我是 Nextjs 的新手,对 Nextjs 中的客户端渲染和服务器端渲染有一些疑问
useQuery钩子,但只能在 React 组件函数上调用。这是否意味着它仅在从客户端渲染页面时运行apolloClient到 Nextjs 的帖子。它说总是
apolloClient为 SSR创建一个新实例,并且只apolloClient为 CSR创建一个实例
这是示例代码
export function initializeApollo(initialState = null) {
const _apolloClient = apolloClient ?? createApolloClient();
// If your page has Next.js data fetching methods that use Apollo Client,
// the initial state gets hydrated here
if (initialState) {
// Get existing cache, loaded during client side data fetching
const existingCache = _apolloClient.extract();
// Restore the cache using the data passed from
// getStaticProps/getServerSideProps combined with the existing cached data
_apolloClient.cache.restore({ ...existingCache, ...initialState });
}
// For SSG and SSR always create a new Apollo Client
if (typeof window === "undefined") return _apolloClient;
// Create the Apollo Client once in the client
if (!apolloClient) apolloClient = _apolloClient;
return _apolloClient;
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下?如果问题很愚蠢,我很抱歉
在下一个 JS 中:
需要注意的是,SSG 是服务器端。
在客户端 (CSR) 上,您只想创建 Apollo Client 的单个全局实例。创建 Apollo Client 的多个实例将使在客户端上保持同步变得具有挑战性。这个困难是因为 Apollo Cache、Apollo Link 等,都会存储在不同的 Apollo Client 实例中。
在 Next 中,通常将 Apollo Client 的全局实例放置在页面上_app.js并使用Apollo Provider。在其他客户端页面上,您将使用调用单个全局实例的useQuery挂钩。
服务器端 (SSR) 函数 getStaticProps 或 getServerSideProps 无权访问 Apollo 的客户端实例、Next 的客户端实例或其他服务器端函数。因此,您必须在每个使用 getStaticPaths、getStaticProps 或 getServerSideProps 并需要访问 Apollo 客户端的页面上定义 Apollo 连接,否则服务器端调用将无法使用它。
由于钩子的第一条规则是它们只能在顶层(客户端)调用,因此不能在服务器端函数中使用它们。不,您不能在 Next SSR 或 SSG 函数中运行 useQuery。
您提供的示例使缓存保持同步,并且在定义客户端的方式上已经过时。这是一个更符合官方文档的简化示例。
graphqlClient.js
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
// Used server and client side - can't use react hooks
export const graphqlClient = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({
uri: 'YOUR_GQL_ENDPOINT',
}),
ssrMode: typeof window === 'undefined',
});
Run Code Online (Sandbox Code Playgroud)
_app.js - 所有客户端页面都使用的单个实例,因为它包装了整个应用程序
import graphqlClient from 'my/path/graphqlClient';
const App = ({ Component, pageProps }) => {
const client = graphqlClient();
return (
<ApolloProvider client={client}>
<Component {...pageProps} />
</ApolloProvider>
);
};
Run Code Online (Sandbox Code Playgroud)
每个需要访问 Apollo 的客户端页面都使用 useQuery 钩子
客户端
import { gql, useQuery } from '@apollo/client';
import graphqlClient from 'my/path/graphqlClient';
const About = () => {
const { data } = useQuery(YOUR_QUERY); // uses your single instance defined in _app.js
const { data } = await client.query({query: YOUR_QUERY});
return (
...
)
}
Run Code Online (Sandbox Code Playgroud)
每个使用 SSR 或 SSG 功能并需要访问 Apollo 的页面都必须实例化一个新的 Apollo 实例。
SSG
import graphqlClient from 'my/path/graphqlClient';
//does not have access to _app.js or client and must define new Apollo Client instance
export const getStaticProps = async () => {
const client = graphqlClient();//
const { data } = await client.query({query: YOUR_QUERY});
};
export const getStaticPaths = async () => {
const client = graphqlClient();
const { data } = await client.query({query: YOUR_QUERY});
};
Run Code Online (Sandbox Code Playgroud)
固态继电器
import graphqlClient from 'my/path/graphqlClient';
//does not have access to _app.js or client and must define new Apollo Client instance
export const getServerSideProps = async () => {
const client = graphqlClient();
const { data } = await client.query({query: YOUR_QUERY});
};
Run Code Online (Sandbox Code Playgroud)
最后,为了简化事情,您可以使用graphql-code-generator自动生成 Apollo 查询、变异等钩子(以及 TS 用户的类型)以及Next.js 的服务器端兼容查询和变异函数。
| 归档时间: |
|
| 查看次数: |
623 次 |
| 最近记录: |