- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有 Apollo 客户端的自定义 NextJS 服务器。我想在服务器端获取 GraphQL 数据,然后将其发送到客户端。我本来可以做到这一点,但客户端再次获取它。据我所知,Apollo 缓存仅在服务器上可用,然后需要发送到客户端并从那里恢复。
Apollo docs提到SSR,但我不想使用Apollo客户端完全渲染我的应用程序,我想使用NextJS,我只想从Apollo客户端获取数据并将其手动注入(inject)HTML以在客户端上恢复它。我查看了一些使用 Apollo 的 NextJS 示例,但没有一个示例展示了如何准确地做到这一点。
这是我的自定义请求处理程序:
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const customHandler = async (req, res) => {
const rendered = await app.renderToHTML(req, res, req.path, req.query);
// somehow get the data from the apollo cache and inject it in the rendered html
res.send(rendered);
}
最佳答案
当您在服务器中创建ApolloClient
时,您可以传递initialState
来水合缓存。
const createApolloClient = ({ initialState, headers }) =>
new ApolloClient({
uri: GRAPHQL_URL,
cache: new InMemoryCache().restore(initialState || {}) // hydrate cache
});
export default withApollo(PageComponent, { ssr = true } = {}) => {
const WithApollo = ({ apolloClient, apolloState, ...pageProps }) => {
const client = apolloClient || createApolloClient({ initialState: apolloState, headers: {} });
... rest of your code.
});
});
我专门为此创建了一个名为 nextjs-with-apollo
的包。看看https://github.com/adikari/nextjs-with-apollo 。安装该软件包后,创建一个 HOC。
// hocs/withApollo.js
import withApollo from 'nextjs-with-apollo';
import ApolloClient from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
const GRAPHQL_URL = 'https://your-graphql-url';
const createApolloClient = ({ initialState, headers }) =>
new ApolloClient({
uri: GRAPHQL_URL,
cache: new InMemoryCache().restore(initialState || {}) // hydrate cache
});
export default withApollo(createApolloClient);
然后您就可以在下一个
页面中使用该临时内容。
import React from 'react';
import { useQuery } from '@apollo/react-hooks';
import withApollo from 'hocs/withApollo';
const QUERY = gql`
query Profile {
profile {
name
displayname
}
}
`;
const ProfilePage = () => {
const { loading, error, data } = useQuery(PROFILE_QUERY);
if (loading) {
return <p>loading..</p>;
}
if (error) {
return JSON.stringify(error);
}
return (
<>
<p>user name: {data.profile.displayname}</p>
<p>name: {data.profile.name}</p>
</>
);
};
export default withApollo(ProfilePage);
关于javascript - 如何从 NextJS 服务器水合 Apollo 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867857/
我正在使用 suds 库从网络服务中获取产品列表。 这是一个示例代码: from suds.client import Client url = 'WSDLURL' client = Client(u
如何在我的 spriteKit 应用程序中获取少量液体? 我想要那种液体放在一个容器里,它会被密封在里面,但我希望它能够四处移动。 有没有更好的方法来实现这个?也许没有 SpriteKit 的选项?
我正在尝试使用 Soap API 将一些文件附加到 Jira。我有 python 2.6 并且 SOAPpy 不再工作,所以,我正在使用 suds。除了附件之外一切都很好...我不知道如何重写这段代码
请建议库在 python 中使用 soap。 现在,我正在尝试使用“suds”,我无法理解如何从服务器回复中获取 http header 代码示例: from suds.client import C
我正在使用 OpenGL 开发 3d 游戏,并希望将其带入幻想的方向。具体来说,我正在考虑拥有具有火、水、冰和闪电效果的魔法。我的问题是我不知道如何创建这些效果。有没有关于如何学习这样的东西的资源?
这是我多年来依赖这个网站后的第一个问题! 无论如何,我想完成类似这种效果的事情: http://www.flashmonkey.co.uk/html5/wave-physics/ 但在圆形路径上,而不
我正在尝试解决 SPOJ 中的以下问题: On a rectangular mesh comprising nm fields, nm cuboids were put, one cuboid on
我在 Django(1.3、python 2.7)中使用 Suds 时遇到一些问题。 当我在脚本中使用 suds 检索数据时,它可以工作;但是如果我将**完全相同的**代码放入 django View
我是 的新手WATIR 或 Selenium,但我试图在我的 WATIR 浏览器中添加一个 cookie,如下所示: browser = Watir::Browser.new :firefox
我想知道是否有可能在 Google map 或 Bing Mag 2D/3D map 上恢复地形类型(山脉、森林、水域、平原等...) 。为了根据玩家在现实世界中的位置生成 map !我认为可用 AP
我是一名优秀的程序员,十分优秀!