-
Notifications
You must be signed in to change notification settings - Fork 589
/
ssr.js
56 lines (52 loc) · 1.81 KB
/
ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import "cross-fetch/polyfill";
import "core-js/stable";
import "regenerator-runtime/runtime";
import React from "react";
import { ApolloProvider } from "react-apollo";
import { StaticRouter } from "react-router-dom";
import ReactDOMServer from "react-dom/server";
import Helmet from "react-helmet";
import { getDataFromTree } from "@apollo/react-ssr";
import ApolloClient from "apollo-client";
import { ApolloLink } from "apollo-link";
import { InMemoryCache } from "apollo-cache-inmemory";
import { createHttpLink } from "apollo-link-http";
import { createOmitTypenameLink } from "@webiny/app/graphql";
import injectContent from "./injectContent";
import App from "../src/App";
const createClient = () => {
return new ApolloClient({
ssrMode: true,
link: ApolloLink.from([
createOmitTypenameLink(),
createHttpLink({
uri: process.env.REACT_APP_GRAPHQL_API_URL
})
]),
cache: new InMemoryCache({
addTypename: true,
dataIdFromObject: obj => obj.id || null
})
});
};
export const renderer = async url => {
const apolloClient = createClient();
const context = {};
const app = (
<ApolloProvider client={apolloClient}>
<StaticRouter
basename={process.env.PUBLIC_URL === "/" ? "" : process.env.PUBLIC_URL}
location={url}
context={context}
>
<App />
</StaticRouter>
</ApolloProvider>
);
// Executes all graphql queries for the current state of application
await getDataFromTree(app);
const content = ReactDOMServer.renderToStaticMarkup(app);
const state = apolloClient.extract();
const helmet = Helmet.renderStatic();
return { html: injectContent(content, helmet, state), state };
};