create-sitecore-jss 22.2.0-canary.79 → 22.2.0-canary.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
1
  import { Component, OnInit } from '@angular/core';
2
+ import { take } from 'rxjs/operators';
2
3
  import { CloudSDK } from '@sitecore-cloudsdk/core/browser';
3
4
  import '@sitecore-cloudsdk/events/browser';
5
+ import { isServer, LayoutServicePageState } from '@sitecore-jss/sitecore-jss-angular';
4
6
  import { environment } from '../../../environments/environment';
5
- import { isServer } from '@sitecore-jss/sitecore-jss-angular';
7
+ import { JssContextService } from '../../jss-context.service';
8
+ import { JssState } from '../../JssState';
6
9
 
7
10
  /**
8
11
  * Component to init CloudSDK logic - to allow events throughout the site
@@ -12,21 +15,34 @@ import { isServer } from '@sitecore-jss/sitecore-jss-angular';
12
15
  template: '',
13
16
  })
14
17
  export class CloudSdkInitComponent implements OnInit {
15
- constructor() {}
18
+ constructor(private jssContext: JssContextService) {}
16
19
 
17
20
  ngOnInit(): void {
18
21
  if (!isServer() && environment.production) {
19
- CloudSDK({
20
- siteName: environment.sitecoreSiteName,
21
- sitecoreEdgeUrl: environment.sitecoreEdgeUrl,
22
- sitecoreEdgeContextId: environment.sitecoreEdgeContextId,
23
- // Replace with the top level cookie domain of the website that is being integrated e.g ".example.com" and not "www.example.com"
24
- cookieDomain: window.location.hostname.replace(/^www\./, ''),
25
- // Cookie may be created in personalize middleware (server), but if not we should create it here
26
- enableBrowserCookie: true,
27
- })
28
- .addEvents()
29
- .initialize();
22
+ // to ensure that CloudSDK initialization logic runs only once in the browser, take only the first emitted value of state
23
+ this.jssContext.state.pipe(take(1)).subscribe((newState: JssState) => {
24
+ const {
25
+ route,
26
+ context: { pageState },
27
+ } = newState.sitecore;
28
+
29
+ // Do not initialize CloudSDK in editing or preview mode or if missing route data
30
+ if (pageState !== LayoutServicePageState.Normal || !route?.itemId) {
31
+ return;
32
+ }
33
+
34
+ CloudSDK({
35
+ siteName: environment.sitecoreSiteName,
36
+ sitecoreEdgeUrl: environment.sitecoreEdgeUrl,
37
+ sitecoreEdgeContextId: environment.sitecoreEdgeContextId,
38
+ // Replace with the top level cookie domain of the website that is being integrated e.g ".example.com" and not "www.example.com"
39
+ cookieDomain: window.location.hostname.replace(/^www\./, ''),
40
+ // Cookie may be created in personalize middleware (server), but if not we should create it here
41
+ enableBrowserCookie: true,
42
+ })
43
+ .addEvents()
44
+ .initialize();
45
+ });
30
46
  }
31
47
  }
32
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sitecore-jss",
3
- "version": "22.2.0-canary.79",
3
+ "version": "22.2.0-canary.80",
4
4
  "description": "Sitecore JSS initializer",
5
5
  "bin": "./dist/index.js",
6
6
  "scripts": {
@@ -63,5 +63,5 @@
63
63
  "ts-node": "^10.9.1",
64
64
  "typescript": "~4.9.5"
65
65
  },
66
- "gitHead": "c3625aea93712fdec9a17d5938111c540e2ad401"
66
+ "gitHead": "0ecca7f10712e929cfe4375ccfbd479020cd30fc"
67
67
  }