我正在使用带有 Apollo 客户端的自定义 NextJS 服务器。我想在服务器端获取 GraphQL 数据,然后将其发送到客户端。我本来可以做到这一点,但客户端再次获取它。据我所知,Apollo 缓存仅在服务器上可用,然后需要发送到客户端并从那里恢复。
Apollo文档提到了 SSR,但我不想使用 Apollo 客户端完全渲染我的应用程序,我想使用 NextJS,我只想从 Apollo 客户端获取数据并将其手动注入到 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);
}
Run Code Online (Sandbox Code Playgroud)