NextJs-Apollo-第一个渲染不是SSR

时间:2020-03-13 22:33:53

标签: next.js apollo server-side-rendering

我正在将NextJs(9.3.0)用于SSR和graphQL(Apollo)。

我的应用程序运行良好,但是当我检查google看到的内容时,Apollo的数据不可用

enter image description here

如果我随机进行curl https://myWebsite.com,则有时我的内容(例如google)中没有来自Apollo的数据,有时没有来自Apollo的数据。

出于SEO的目的,我需要始终使用刷新的数据进行第一个渲染(购买刷新后)并购买我的后端(Apollo)

这是我的文件:apolloClient.tsx

import { ApolloClient } from "apollo-client";
import { AUTH_TOKEN } from "./config";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import Cookies from "js-cookie";
import fetch from "isomorphic-unfetch";
import nextCookies from "next-cookies";
import { uriBackend } from "./config";

let token = null;

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  // on server
  if (ctx && ctx.req && ctx.req.headers["cookie"]) {
    token = nextCookies(ctx)[AUTH_TOKEN];

    // on client
  } else {
    // console.log("with data get client cookie");
    token = Cookies.get(AUTH_TOKEN);
  }
  const headers = token ? { Authorization: `Bearer ${token}` } : {};

  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      uri: uriBackend, // Server URL (must be absolute)
      fetch,
      headers
    }),
    cache: new InMemoryCache().restore(initialState)
  });
}

0 个答案:

没有答案