|
1 | 1 | import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
|
2 | 2 | import { setContext } from '@apollo/client/link/context';
|
| 3 | +import { getPreviewToken } from "@/helpers/onContentSaved"; |
3 | 4 |
|
4 | 5 | let client: ApolloClient<any> | undefined = undefined;
|
5 | 6 | const graphUrl = process.env.GRAPH_URL;
|
6 | 7 | const cmsUrl = process.env.CMS_URL;
|
| 8 | +const preview_token = getPreviewToken(); |
7 | 9 |
|
8 |
| -if (typeof window !== "undefined" && window.location !== undefined) { |
9 |
| - const queryString = window?.location?.search; |
10 |
| - const urlParams = new URLSearchParams(queryString); |
11 |
| - const preview_token = urlParams.get('preview_token') ?? undefined; |
12 |
| - |
13 |
| - if (preview_token) { |
14 |
| - const httpLink = createHttpLink({ |
15 |
| - uri: `https://${graphUrl}/content/v2`, |
16 |
| - }); |
17 |
| - |
18 |
| - const authLink = setContext((_, { headers }) => { |
19 |
| - return { |
20 |
| - headers: { |
21 |
| - ...headers, |
22 |
| - authorization: `Bearer ${preview_token}` |
23 |
| - } |
24 |
| - }; |
25 |
| - }); |
26 |
| - |
27 |
| - client = new ApolloClient({ |
28 |
| - link: authLink.concat(httpLink), |
29 |
| - cache: new InMemoryCache() |
30 |
| - }); |
31 |
| - |
32 |
| - const communicationScript = document.createElement('script'); |
33 |
| - communicationScript.src = `https://${cmsUrl}/Util/javascript/communicationInjector.js`; |
34 |
| - communicationScript.setAttribute('data-nscript', 'afterInteractive') |
35 |
| - document.body.appendChild(communicationScript); |
36 |
| - } |
| 10 | +// In Preview Mode |
| 11 | +if (preview_token) { |
| 12 | + const httpLink = createHttpLink({ |
| 13 | + uri: `https://${graphUrl}/content/v2`, |
| 14 | + }); |
| 15 | + |
| 16 | + const authLink = setContext((_, { headers }) => { |
| 17 | + return { |
| 18 | + headers: { |
| 19 | + ...headers, |
| 20 | + authorization: `Bearer ${preview_token}` |
| 21 | + } |
| 22 | + }; |
| 23 | + }); |
| 24 | + |
| 25 | + client = new ApolloClient({ |
| 26 | + link: authLink.concat(httpLink), |
| 27 | + cache: new InMemoryCache() |
| 28 | + }); |
| 29 | + |
| 30 | + const communicationScript = document.createElement('script'); |
| 31 | + communicationScript.src = `https://${cmsUrl}/Util/javascript/communicationInjector.js`; |
| 32 | + communicationScript.setAttribute('data-nscript', 'afterInteractive') |
| 33 | + document.body.appendChild(communicationScript); |
37 | 34 | }
|
38 | 35 |
|
| 36 | +// In Public Mode |
39 | 37 | if (client === undefined) {
|
40 | 38 | const singleGraphKey = process.env.GRAPH_SINGLE_KEY;
|
41 | 39 | const httpLink = createHttpLink({
|
|
0 commit comments