Skip to content

Commit 1d0a1e3

Browse files
authored
Merge pull request #5 from episerver/user/base/do-not-try-to-subscribe-in-viewmode
Do not try to connect to CMS in View Mode
2 parents 1b35f97 + eeb2ffb commit 1d0a1e3

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/apolloClient.tsx

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
22
import { setContext } from '@apollo/client/link/context';
3+
import { getPreviewToken } from "@/helpers/onContentSaved";
34

45
let client: ApolloClient<any> | undefined = undefined;
56
const graphUrl = process.env.GRAPH_URL;
67
const cmsUrl = process.env.CMS_URL;
8+
const preview_token = getPreviewToken();
79

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);
3734
}
3835

36+
// In Public Mode
3937
if (client === undefined) {
4038
const singleGraphKey = process.env.GRAPH_SINGLE_KEY;
4139
const httpLink = createHttpLink({

src/helpers/onContentSaved.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ interface ContentSavedEventArgs {
1212
sectionId?: string;
1313
}
1414

15+
export function getPreviewToken() {
16+
if (typeof window !== "undefined" && window.location !== undefined) {
17+
const queryString = window?.location?.search;
18+
const urlParams = new URLSearchParams(queryString);
19+
return urlParams.get('preview_token');
20+
} else {
21+
return undefined;
22+
}
23+
}
24+
25+
function isInEditMode() {
26+
return !!getPreviewToken();
27+
}
28+
1529
function ensureEpiLoaded() {
1630
if (typeof window !== "undefined") {
1731
const epi = (window as any).epi;
@@ -24,6 +38,10 @@ function ensureEpiLoaded() {
2438
}
2539

2640
export function onContentSaved(callback: ((message: ContentSavedEventArgs) => void )) {
41+
if (!isInEditMode()) {
42+
return;
43+
}
44+
2745
const epi = ensureEpiLoaded();
2846
if (epi) {
2947
console.info("successfully connected to CMS.");

0 commit comments

Comments
 (0)