@sitecore-content-sdk/nextjs 2.0.0-canary.14 → 2.0.0-canary.16

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.
Files changed (77) hide show
  1. package/dist/cjs/cache/page-params.js +30 -0
  2. package/dist/cjs/client/sitecore-nextjs-client.js +57 -94
  3. package/dist/cjs/components/BYOCWrapper.js +3 -12
  4. package/dist/cjs/components/DesignLibrary/DesignLibraryApp.js +31 -0
  5. package/dist/cjs/components/DesignLibrary/DesignLibraryClientEvents.js +139 -0
  6. package/dist/cjs/components/DesignLibrary/DesignLibraryServer.js +217 -0
  7. package/dist/cjs/components/DesignLibrary/index.js +5 -0
  8. package/dist/cjs/components/DesignLibrary/models.js +2 -0
  9. package/dist/cjs/components/FEaaSWrapper.js +3 -12
  10. package/dist/cjs/editing/editing-config-middleware.js +2 -11
  11. package/dist/cjs/editing/editing-render-middleware.js +3 -12
  12. package/dist/cjs/editing/feaas-render-middleware.js +2 -11
  13. package/dist/cjs/editing/utils.js +3 -12
  14. package/dist/cjs/index.js +6 -2
  15. package/dist/cjs/initialization/proxy/analytics-adapter.js +3 -12
  16. package/dist/cjs/initialization/proxy/personalize-adapter.js +3 -12
  17. package/dist/cjs/middleware/robots-middleware.js +14 -25
  18. package/dist/cjs/middleware/sitemap-middleware.js +17 -28
  19. package/dist/cjs/monitoring/healthcheck-middleware.js +2 -11
  20. package/dist/cjs/proxy/locale-proxy.js +2 -11
  21. package/dist/cjs/proxy/multisite-proxy.js +2 -11
  22. package/dist/cjs/proxy/personalize-proxy.js +34 -47
  23. package/dist/cjs/proxy/proxy.js +3 -12
  24. package/dist/cjs/proxy/redirects-proxy.js +58 -69
  25. package/dist/cjs/route-handler/editing-config-route-handler.js +4 -13
  26. package/dist/cjs/route-handler/editing-render-route-handler.js +17 -28
  27. package/dist/cjs/route-handler/robots-route-handler.js +5 -14
  28. package/dist/cjs/route-handler/sitemap-route-handler.js +5 -14
  29. package/dist/cjs/server-actions/update-server-component-action.js +54 -0
  30. package/dist/cjs/services/component-props-service.js +63 -80
  31. package/dist/esm/cache/page-params.js +25 -0
  32. package/dist/esm/client/sitecore-nextjs-client.js +57 -94
  33. package/dist/esm/components/BYOCWrapper.js +3 -12
  34. package/dist/esm/components/DesignLibrary/DesignLibraryApp.js +24 -0
  35. package/dist/esm/components/DesignLibrary/DesignLibraryClientEvents.js +100 -0
  36. package/dist/esm/components/DesignLibrary/DesignLibraryServer.js +174 -0
  37. package/dist/esm/components/DesignLibrary/index.js +1 -0
  38. package/dist/esm/components/DesignLibrary/models.js +1 -0
  39. package/dist/esm/components/FEaaSWrapper.js +3 -12
  40. package/dist/esm/editing/editing-config-middleware.js +2 -11
  41. package/dist/esm/editing/editing-render-middleware.js +3 -12
  42. package/dist/esm/editing/feaas-render-middleware.js +2 -11
  43. package/dist/esm/editing/utils.js +3 -12
  44. package/dist/esm/index.js +3 -1
  45. package/dist/esm/initialization/proxy/analytics-adapter.js +3 -12
  46. package/dist/esm/initialization/proxy/personalize-adapter.js +3 -12
  47. package/dist/esm/middleware/robots-middleware.js +14 -25
  48. package/dist/esm/middleware/sitemap-middleware.js +17 -28
  49. package/dist/esm/monitoring/healthcheck-middleware.js +2 -11
  50. package/dist/esm/proxy/locale-proxy.js +2 -11
  51. package/dist/esm/proxy/multisite-proxy.js +2 -11
  52. package/dist/esm/proxy/personalize-proxy.js +34 -47
  53. package/dist/esm/proxy/proxy.js +3 -12
  54. package/dist/esm/proxy/redirects-proxy.js +58 -69
  55. package/dist/esm/route-handler/editing-config-route-handler.js +4 -13
  56. package/dist/esm/route-handler/editing-render-route-handler.js +17 -28
  57. package/dist/esm/route-handler/robots-route-handler.js +5 -14
  58. package/dist/esm/route-handler/sitemap-route-handler.js +5 -14
  59. package/dist/esm/server-actions/update-server-component-action.js +50 -0
  60. package/dist/esm/services/component-props-service.js +63 -80
  61. package/package.json +7 -7
  62. package/types/cache/page-params.d.ts +30 -0
  63. package/types/cache/page-params.d.ts.map +1 -0
  64. package/types/components/DesignLibrary/DesignLibraryApp.d.ts +14 -0
  65. package/types/components/DesignLibrary/DesignLibraryApp.d.ts.map +1 -0
  66. package/types/components/DesignLibrary/DesignLibraryClientEvents.d.ts +21 -0
  67. package/types/components/DesignLibrary/DesignLibraryClientEvents.d.ts.map +1 -0
  68. package/types/components/DesignLibrary/DesignLibraryServer.d.ts +38 -0
  69. package/types/components/DesignLibrary/DesignLibraryServer.d.ts.map +1 -0
  70. package/types/components/DesignLibrary/index.d.ts +2 -0
  71. package/types/components/DesignLibrary/index.d.ts.map +1 -0
  72. package/types/components/DesignLibrary/models.d.ts +60 -0
  73. package/types/components/DesignLibrary/models.d.ts.map +1 -0
  74. package/types/index.d.ts +3 -1
  75. package/types/index.d.ts.map +1 -1
  76. package/types/server-actions/update-server-component-action.d.ts +58 -0
  77. package/types/server-actions/update-server-component-action.d.ts.map +1 -0
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCachedPageParams = void 0;
4
+ exports.setCachedPageParams = setCachedPageParams;
5
+ const react_1 = require("react");
6
+ /**
7
+ * Internal cache implementation for storing page params.
8
+ *
9
+ * This cache is used to store the locale and site information for the current page.
10
+ *
11
+ * It helps avoid the usage of functions that disable SSG such as `headers()`.
12
+ * @internal
13
+ */
14
+ const cacheImpl = (0, react_1.cache)(() => ({ locale: '', site: '' }));
15
+ /**
16
+ * Gets the cached page params, including locale and site.
17
+ * @returns An object containing the locale and site information for the current page.
18
+ * @public
19
+ */
20
+ const getCachedPageParams = () => cacheImpl();
21
+ exports.getCachedPageParams = getCachedPageParams;
22
+ /**
23
+ * Sets the cached page params, including locale and site.
24
+ * @param {CachedPageParams} pageParams An object containing the locale and site information to be set for the current page cache.
25
+ * @public
26
+ */
27
+ function setCachedPageParams(pageParams) {
28
+ cacheImpl().locale = pageParams.locale;
29
+ cacheImpl().site = pageParams.site;
30
+ }
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.SitecoreNextjsClient = void 0;
13
4
  const client_1 = require("@sitecore-content-sdk/content/client");
@@ -44,23 +35,17 @@ class SitecoreNextjsClient extends client_1.SitecoreClient {
44
35
  const basePath = super.parsePath(path);
45
36
  return (0, site_1.normalizeSiteRewrite)((0, personalize_1.normalizePersonalizedRewrite)(basePath));
46
37
  }
47
- getPage(path, pageOptions, options) {
48
- const _super = Object.create(null, {
49
- parsePath: { get: () => super.parsePath },
50
- getPage: { get: () => super.getPage }
51
- });
52
- return __awaiter(this, void 0, void 0, function* () {
53
- const resolvedPath = this.parsePath(path);
54
- // Get variant(s) for personalization (from path), must ensure path is of type string
55
- const personalizeData = pageOptions.personalize || (0, personalize_1.getPersonalizedRewriteData)(_super.parsePath.call(this, path));
56
- const site = pageOptions.site || this.getSiteNameFromPath(path);
57
- const page = yield _super.getPage.call(this, resolvedPath, {
58
- locale: pageOptions.locale,
59
- site,
60
- personalize: personalizeData,
61
- }, options);
62
- return page;
63
- });
38
+ async getPage(path, pageOptions, options) {
39
+ const resolvedPath = this.parsePath(path);
40
+ // Get variant(s) for personalization (from path), must ensure path is of type string
41
+ const personalizeData = pageOptions.personalize || (0, personalize_1.getPersonalizedRewriteData)(super.parsePath(path));
42
+ const site = pageOptions.site || this.getSiteNameFromPath(path);
43
+ const page = await super.getPage(resolvedPath, {
44
+ locale: pageOptions.locale,
45
+ site,
46
+ personalize: personalizeData,
47
+ }, options);
48
+ return page;
64
49
  }
65
50
  /**
66
51
  * Get design library page details for Design Library mode of your app
@@ -68,26 +53,16 @@ class SitecoreNextjsClient extends client_1.SitecoreClient {
68
53
  * @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
69
54
  * @returns {Page} preview page for Design Library
70
55
  */
71
- getDesignLibraryData(designLibData, fetchOptions) {
72
- const _super = Object.create(null, {
73
- getDesignLibraryData: { get: () => super.getDesignLibraryData }
74
- });
75
- return __awaiter(this, void 0, void 0, function* () {
76
- return _super.getDesignLibraryData.call(this, designLibData, fetchOptions);
77
- });
56
+ async getDesignLibraryData(designLibData, fetchOptions) {
57
+ return super.getDesignLibraryData(designLibData, fetchOptions);
78
58
  }
79
59
  /**
80
60
  * Retrieves preview page and layout details
81
61
  * @param {PreviewData} previewData - The editing preview data for metadata mode.
82
62
  * @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests (like retries and fetch)
83
63
  */
84
- getPreview(previewData, fetchOptions) {
85
- const _super = Object.create(null, {
86
- getPreview: { get: () => super.getPreview }
87
- });
88
- return __awaiter(this, void 0, void 0, function* () {
89
- return _super.getPreview.call(this, previewData, fetchOptions);
90
- });
64
+ async getPreview(previewData, fetchOptions) {
65
+ return super.getPreview(previewData, fetchOptions);
91
66
  }
92
67
  /**
93
68
  * Generates static params for the Next.js App Router from Sitecore routes.
@@ -103,25 +78,20 @@ class SitecoreNextjsClient extends client_1.SitecoreClient {
103
78
  * @param {FetchOptions} [fetchOptions] - Additional fetch options.
104
79
  * @returns {Promise<StaticParams[]>} Array of `{ site, locale, path }` entries for `generateStaticParams`.
105
80
  */
106
- getAppRouterStaticParams(sites, languages, fetchOptions) {
107
- const _super = Object.create(null, {
108
- getPagePaths: { get: () => super.getPagePaths }
109
- });
110
- return __awaiter(this, void 0, void 0, function* () {
111
- const staticPaths = yield _super.getPagePaths.call(this, sites, languages, fetchOptions);
112
- const params = new Array();
113
- staticPaths.map((path) => {
114
- var _a;
115
- // remove _site_ segments
116
- const normalizedPath = (0, site_1.normalizeSiteRewrite)(path.params.path.join('/')).split('/');
117
- params.push({
118
- locale: (_a = path.locale) !== null && _a !== void 0 ? _a : this.initOptions.defaultLanguage,
119
- site: this.getSiteNameFromPath(path.params.path),
120
- path: normalizedPath,
121
- });
81
+ async getAppRouterStaticParams(sites, languages, fetchOptions) {
82
+ const staticPaths = await super.getPagePaths(sites, languages, fetchOptions);
83
+ const params = new Array();
84
+ staticPaths.map((path) => {
85
+ var _a;
86
+ // remove _site_ segments
87
+ const normalizedPath = (0, site_1.normalizeSiteRewrite)(path.params.path.join('/')).split('/');
88
+ params.push({
89
+ locale: (_a = path.locale) !== null && _a !== void 0 ? _a : this.initOptions.defaultLanguage,
90
+ site: this.getSiteNameFromPath(path.params.path),
91
+ path: normalizedPath,
122
92
  });
123
- return params;
124
93
  });
94
+ return params;
125
95
  }
126
96
  /**
127
97
  * Retrieves the static paths for pages based on the given languages.
@@ -130,21 +100,16 @@ class SitecoreNextjsClient extends client_1.SitecoreClient {
130
100
  * @param {FetchOptions} [fetchOptions] - Additional fetch options.
131
101
  * @returns {Promise<StaticPath[]>} A promise that resolves to an array of static paths.
132
102
  */
133
- getPagePaths(sites, languages, fetchOptions) {
134
- const _super = Object.create(null, {
135
- getPagePaths: { get: () => super.getPagePaths }
136
- });
137
- return __awaiter(this, void 0, void 0, function* () {
138
- var _a;
139
- const staticPaths = yield _super.getPagePaths.call(this, sites, languages, fetchOptions);
140
- if (!((_a = this.initOptions.multisite) === null || _a === void 0 ? void 0 : _a.enabled)) {
141
- // remove _site_ segments when multisite is disabled
142
- staticPaths.map((path) => {
143
- path.params.path = (0, site_1.normalizeSiteRewrite)(path.params.path.join('/')).split('/');
144
- });
145
- }
146
- return staticPaths;
147
- });
103
+ async getPagePaths(sites, languages, fetchOptions) {
104
+ var _a;
105
+ const staticPaths = await super.getPagePaths(sites, languages, fetchOptions);
106
+ if (!((_a = this.initOptions.multisite) === null || _a === void 0 ? void 0 : _a.enabled)) {
107
+ // remove _site_ segments when multisite is disabled
108
+ staticPaths.map((path) => {
109
+ path.params.path = (0, site_1.normalizeSiteRewrite)(path.params.path.join('/')).split('/');
110
+ });
111
+ }
112
+ return staticPaths;
148
113
  }
149
114
  /**
150
115
  * Parses components from nextjs component map and layoutData, executes getServerProps/getStaticProps methods
@@ -154,30 +119,28 @@ class SitecoreNextjsClient extends client_1.SitecoreClient {
154
119
  * @param {ComponentMap<NextjsContentSdkComponent>} components component map to get props for
155
120
  * @returns {ComponentPropsCollection} component props
156
121
  */
157
- getComponentData(layoutData, context, components) {
158
- return __awaiter(this, void 0, void 0, function* () {
159
- let componentProps = {};
160
- if (!layoutData.sitecore.route)
161
- return componentProps;
162
- // Retrieve component props using side-effects defined on components level
163
- componentProps = yield this.componentPropsService.fetchComponentProps({
164
- layoutData: layoutData,
165
- context,
166
- components,
167
- });
168
- const errors = Object.keys(componentProps)
169
- .map((id) => {
170
- const component = componentProps[id];
171
- return component.error
172
- ? `\nUnable to get component props for ${component.componentName} (${id}): ${component.error}`
173
- : '';
174
- })
175
- .join('');
176
- if (errors.length) {
177
- throw new Error(errors);
178
- }
122
+ async getComponentData(layoutData, context, components) {
123
+ let componentProps = {};
124
+ if (!layoutData.sitecore.route)
179
125
  return componentProps;
126
+ // Retrieve component props using side-effects defined on components level
127
+ componentProps = await this.componentPropsService.fetchComponentProps({
128
+ layoutData: layoutData,
129
+ context,
130
+ components,
180
131
  });
132
+ const errors = Object.keys(componentProps)
133
+ .map((id) => {
134
+ const component = componentProps[id];
135
+ return component.error
136
+ ? `\nUnable to get component props for ${component.componentName} (${id}): ${component.error}`
137
+ : '';
138
+ })
139
+ .join('');
140
+ if (errors.length) {
141
+ throw new Error(errors);
142
+ }
143
+ return componentProps;
181
144
  }
182
145
  getComponentPropsService() {
183
146
  return new component_props_service_1.ComponentPropsService();
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.getComponentServerProps = void 0;
13
4
  const react_1 = require("@sitecore-content-sdk/react");
@@ -17,10 +8,10 @@ const react_1 = require("@sitecore-content-sdk/react");
17
8
  * @returns {GetStaticPropsContext | GetStaticPropsContext} context with type depending on SSR or SSG mode
18
9
  * @internal
19
10
  */
20
- const getComponentServerProps = (rendering) => __awaiter(void 0, void 0, void 0, function* () {
11
+ const getComponentServerProps = async (rendering) => {
21
12
  const params = rendering.params || {};
22
- const result = yield (0, react_1.fetchBYOCComponentServerProps)(params);
13
+ const result = await (0, react_1.fetchBYOCComponentServerProps)(params);
23
14
  return result;
24
- });
15
+ };
25
16
  exports.getComponentServerProps = getComponentServerProps;
26
17
  exports.default = react_1.BYOCWrapper;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DesignLibraryApp = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const layout_1 = require("@sitecore-content-sdk/content/layout");
9
+ const DesignLibraryServer_1 = require("./DesignLibraryServer");
10
+ const react_2 = require("@sitecore-content-sdk/react");
11
+ /**
12
+ * Design Library component intended to be used by the NextJs app router application
13
+ * This component serves as a router between client and server component rendering modes for the Design Library.
14
+ * It inspects the component type from the component map and
15
+ * delegates to the appropriate rendering implementation:
16
+ * - Client components are rendered using the `DesignLibrary` component
17
+ * - Server components are rendered using the `DesignLibraryServer` component
18
+ * @param {DesingLibraryAppProps} props - The properties for the Design Library App.
19
+ * @public
20
+ */
21
+ const DesignLibraryApp = ({ page, componentMap, loadServerImportMap, }) => {
22
+ var _a;
23
+ const { route } = page.layout.sitecore;
24
+ if (!route)
25
+ return null;
26
+ const rendering = (_a = route === null || route === void 0 ? void 0 : route.placeholders[layout_1.EDITING_COMPONENT_PLACEHOLDER]) === null || _a === void 0 ? void 0 : _a[0];
27
+ const component = componentMap.get((rendering === null || rendering === void 0 ? void 0 : rendering.componentName) || '');
28
+ const isClient = component && component.componentType === 'client';
29
+ return (react_1.default.createElement(react_1.default.Fragment, null, isClient ? (react_1.default.createElement(react_2.DesignLibrary, null)) : (react_1.default.createElement(DesignLibraryServer_1.DesignLibraryServer, { page: page, componentMap: componentMap, loadServerImportMap: loadServerImportMap, rendering: route }))));
30
+ };
31
+ exports.DesignLibraryApp = DesignLibraryApp;
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ 'use client';
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.DesignLibraryVariantGenerationEvents = exports.DesignLibraryPreviewEvents = exports.__mockDependencies = void 0;
38
+ const react_1 = __importStar(require("react"));
39
+ const dlHelpers = __importStar(require("@sitecore-content-sdk/content/editing"));
40
+ const codegen = __importStar(require("@sitecore-content-sdk/content/codegen"));
41
+ const update_server_component_action_1 = require("../../server-actions/update-server-component-action");
42
+ const react_2 = require("@sitecore-content-sdk/react");
43
+ let { getDesignLibraryComponentPropsEvent, addServerComponentPreviewHandler, getDesignLibraryImportMapEvent, addStyleElement, sendErrorEvent, } = codegen;
44
+ let { getDesignLibraryStatusEvent, addComponentUpdateHandler, postToDesignLibrary } = dlHelpers;
45
+ let _updateComponentAction = update_server_component_action_1.updateComponentAction;
46
+ let _previewComponentAction = update_server_component_action_1.previewComponentAction;
47
+ const __mockDependencies = (mocks) => {
48
+ postToDesignLibrary = mocks.postToDesignLibrary;
49
+ addComponentUpdateHandler = mocks.addComponentUpdateHandler;
50
+ _updateComponentAction = mocks.updateComponentAction;
51
+ _previewComponentAction = mocks.previewComponentAction;
52
+ addServerComponentPreviewHandler = mocks.addServerComponentPreviewHandler;
53
+ getDesignLibraryImportMapEvent = mocks.getDesignLibraryImportMapEvent;
54
+ getDesignLibraryComponentPropsEvent = mocks.getDesignLibraryComponentPropsEvent;
55
+ addStyleElement = mocks.addStyleElement;
56
+ sendErrorEvent = mocks.sendErrorEvent;
57
+ };
58
+ exports.__mockDependencies = __mockDependencies;
59
+ /**
60
+ * Design Library component for rendering server components in app router application.
61
+ * DesignLibraryPreviewEvents component serves as a communication bridge between DesignLibraryServer and the Design Studio on the client side.
62
+ * It posts messages to Design Library Studio and sets up handlers to receive updates and previews which are then passed to the server component via server function updateServerComponentAction.
63
+ * @param {DesignLibraryPreviewEventsProps} props The props. {@link DesignLibraryPreviewEventsProps}
64
+ * @returns {JSX.Element} empty JSX element.
65
+ */
66
+ const DesignLibraryPreviewEvents = ({ designLibraryStatus, component, }) => {
67
+ (0, react_1.useEffect)(() => {
68
+ if (!(component === null || component === void 0 ? void 0 : component.uid))
69
+ return;
70
+ postToDesignLibrary(getDesignLibraryStatusEvent(designLibraryStatus, component.uid, true));
71
+ const unsubUpdate = addComponentUpdateHandler(component, (rendering) => {
72
+ _updateComponentAction({ uid: rendering.uid, rendering });
73
+ });
74
+ return () => {
75
+ unsubUpdate && unsubUpdate();
76
+ };
77
+ }, [component, designLibraryStatus]);
78
+ return react_1.default.createElement(react_1.default.Fragment, null);
79
+ };
80
+ exports.DesignLibraryPreviewEvents = DesignLibraryPreviewEvents;
81
+ /**
82
+ * Design Library component for rendering server components in app router application.
83
+ * DesignLibraryVariantGenerationEvents component serves as a communication bridge between DesignLibraryServer and the Design Studio on the client side in variant generation mode.
84
+ * It posts messages to Design Library Studio and sets up handlers to receive updates and previews which are then passed to the server component via server function updateServerComponentAction.
85
+ * If the import map is not present then the import map error will be sent to Design Studio.
86
+ * @param {DesignLibraryVariantGenerationEventsProps} props The props. {@link DesignLibraryVariantGenerationEventsProps}
87
+ * @returns {JSX.Element} empty JSX element.
88
+ */
89
+ const DesignLibraryVariantGenerationEvents = ({ designLibraryStatus, component, importMap, componentInitError, generatedComponentData, }) => {
90
+ var _a;
91
+ const { api } = (0, react_2.useSitecore)();
92
+ const edgeUrl = (_a = api === null || api === void 0 ? void 0 : api.edge) === null || _a === void 0 ? void 0 : _a.edgeUrl;
93
+ (0, react_1.useEffect)(() => {
94
+ var _a;
95
+ if (!(component === null || component === void 0 ? void 0 : component.uid))
96
+ return;
97
+ postToDesignLibrary(getDesignLibraryStatusEvent(designLibraryStatus, component.uid, true));
98
+ const unsubUpdate = addComponentUpdateHandler(component, (rendering) => {
99
+ _updateComponentAction({
100
+ uid: rendering.uid,
101
+ rendering,
102
+ generatedComponentData,
103
+ });
104
+ });
105
+ const unsubPreview = addServerComponentPreviewHandler((eventArgs) => {
106
+ _previewComponentAction({
107
+ uid: component.uid,
108
+ args: eventArgs,
109
+ }, edgeUrl);
110
+ });
111
+ if (componentInitError) {
112
+ // an error occurred during initialization of the component on the server side
113
+ sendErrorEvent(component.uid, componentInitError.message, componentInitError.type);
114
+ }
115
+ else {
116
+ const importMapEvent = getDesignLibraryImportMapEvent(component.uid, importMap);
117
+ postToDesignLibrary(importMapEvent);
118
+ const propsEvent = getDesignLibraryComponentPropsEvent(component.uid, component.fields, component.params);
119
+ postToDesignLibrary(propsEvent);
120
+ if ((_a = generatedComponentData === null || generatedComponentData === void 0 ? void 0 : generatedComponentData.styles) === null || _a === void 0 ? void 0 : _a.content) {
121
+ // the generated component has custom styles, so add the css in style element and add it to document head
122
+ addStyleElement(generatedComponentData.styles.content);
123
+ }
124
+ }
125
+ return () => {
126
+ unsubUpdate && unsubUpdate();
127
+ unsubPreview && unsubPreview();
128
+ };
129
+ }, [
130
+ component,
131
+ designLibraryStatus,
132
+ importMap,
133
+ componentInitError,
134
+ generatedComponentData,
135
+ edgeUrl,
136
+ ]);
137
+ return react_1.default.createElement(react_1.default.Fragment, null);
138
+ };
139
+ exports.DesignLibraryVariantGenerationEvents = DesignLibraryVariantGenerationEvents;
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ 'use server';
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ var __importDefault = (this && this.__importDefault) || function (mod) {
37
+ return (mod && mod.__esModule) ? mod : { "default": mod };
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.DesignLibraryServerPreview = exports.DesignLibraryServerVariantGeneration = exports.DesignLibraryServer = exports.__mockDependencies = void 0;
41
+ const react_1 = __importDefault(require("react"));
42
+ const layout_1 = require("@sitecore-content-sdk/content/layout");
43
+ const DesignLibraryClientEvents_1 = require("./DesignLibraryClientEvents");
44
+ const globalCache = __importStar(require("@sitecore-content-sdk/core/tools"));
45
+ const editing_1 = require("@sitecore-content-sdk/content/editing");
46
+ const codegen = __importStar(require("@sitecore-content-sdk/content/codegen"));
47
+ const react_2 = require("@sitecore-content-sdk/react");
48
+ const react_3 = require("@sitecore-content-sdk/react");
49
+ let { getCacheAndClean, hasCache } = globalCache;
50
+ let { createComponentInstance, getImportMapInfo } = codegen;
51
+ let updateComponent = editing_1.updateComponent;
52
+ const __mockDependencies = async (mocks) => {
53
+ getCacheAndClean = mocks.getCacheAndClean;
54
+ hasCache = mocks.hasCache;
55
+ createComponentInstance = mocks.createComponentInstance;
56
+ if (mocks.updateComponent) {
57
+ updateComponent = mocks.updateComponent;
58
+ }
59
+ if (mocks.getImportMapInfo) {
60
+ getImportMapInfo = mocks.getImportMapInfo;
61
+ }
62
+ };
63
+ exports.__mockDependencies = __mockDependencies;
64
+ /**
65
+ * Design Library component for rendering server components in app router application.
66
+ *
67
+ * Renders the **real** Sitecore component for `library` / `library-metadata` modes and,
68
+ * when generation is enabled (`page.mode.designLibrary.isVariantGeneration === true`),
69
+ * wires the **variant generation** handshake so the parent (Design Library) can send
70
+ * generated code to preview and iterate on.
71
+ * Also renders the DesignLibraryClientEvents component which serves as a communication bridge between DesignLibraryServer and the Design Studio on the client side.
72
+ * @param {DesignLibraryServerProps} props The props. {@link DesignLibraryServerProps}
73
+ * @returns {JSX.Element} The preview surface, or `null` when not in Design Library mode.
74
+ */
75
+ const DesignLibraryServer = async ({ page, componentMap, rendering, loadServerImportMap, }) => {
76
+ var _a;
77
+ if (!page.mode.isDesignLibrary) {
78
+ return null;
79
+ }
80
+ const isVariantGeneration = (_a = page.mode.designLibrary) === null || _a === void 0 ? void 0 : _a.isVariantGeneration;
81
+ if (isVariantGeneration) {
82
+ return (react_1.default.createElement(exports.DesignLibraryServerVariantGeneration, { page: page, rendering: rendering, loadServerImportMap: loadServerImportMap, componentMap: componentMap }));
83
+ }
84
+ return (react_1.default.createElement(exports.DesignLibraryServerPreview, { page: page, rendering: rendering, componentMap: componentMap }));
85
+ };
86
+ exports.DesignLibraryServer = DesignLibraryServer;
87
+ /**
88
+ * Design Library component for rendering server components in app router application in variant generation mode.
89
+ *
90
+ * Renders the **real** Sitecore component for `library` / `library-metadata` modes on first render and,
91
+ * wires the **variant generation** handshake so the parent (Design Library) can send
92
+ * generated code to preview and iterate on.
93
+ * Also renders the DesignLibraryVariantGenerationEvents component which serves as a communication bridge between DesignLibraryServer and the Design Studio on the client side.
94
+ * @param {DesignLibraryServerVariantGenerationProps} props The props. {@link DesignLibraryServerVariantGenerationProps}
95
+ * @returns {JSX.Element} The preview surface, or `null` when not in Design Library mode.
96
+ */
97
+ const DesignLibraryServerVariantGeneration = async ({ page, rendering, loadServerImportMap, componentMap, }) => {
98
+ var _a, _b;
99
+ let designLibraryStatus = editing_1.DesignLibraryStatus.READY;
100
+ let importMap;
101
+ let importMapInfo;
102
+ let Component;
103
+ let componentInitError;
104
+ let generatedComponentData;
105
+ const getComponentInitError = (type, error) => ({
106
+ type,
107
+ message: error instanceof Error ? error.message : String(error),
108
+ });
109
+ // load importmap and importmap payload to pass to FE
110
+ // if not provided, or errors during load set error to pass to FE
111
+ if (!loadServerImportMap) {
112
+ componentInitError = getComponentInitError(codegen.DesignLibraryPreviewError.ImportMapMissing, 'No loadImportMap provided');
113
+ }
114
+ else {
115
+ try {
116
+ const mod = await loadServerImportMap();
117
+ importMap = mod.default;
118
+ importMapInfo = getImportMapInfo(importMap);
119
+ }
120
+ catch (e) {
121
+ componentInitError = getComponentInitError(codegen.DesignLibraryPreviewError.ImportMapLoad, `Error loading import map: ${e}`);
122
+ }
123
+ }
124
+ let componentToUpdate = (_b = (_a = rendering === null || rendering === void 0 ? void 0 : rendering.placeholders) === null || _a === void 0 ? void 0 : _a[layout_1.EDITING_COMPONENT_PLACEHOLDER]) === null || _b === void 0 ? void 0 : _b[0];
125
+ if (!componentToUpdate)
126
+ return react_1.default.createElement(react_3.ErrorComponent, { message: "Rendering data is missing" });
127
+ if (!componentToUpdate.uid)
128
+ return react_1.default.createElement(react_3.ErrorComponent, { message: "Rendering UID is missing in the rendering data" });
129
+ const uid = componentToUpdate.uid;
130
+ const componentUpdateKey = `${editing_1.COMPONENT_UPDATE_CACHE_KEY_PREFIX}${uid}`;
131
+ const componentPreviewKey = `${editing_1.COMPONENT_PREVIEW_CACHE_KEY_PREFIX}${uid}`;
132
+ // check if we have an update for this component in the global cache
133
+ if (hasCache(componentUpdateKey)) {
134
+ // we have fields/params update, get it and clean the cache
135
+ designLibraryStatus = editing_1.DesignLibraryStatus.RENDERED;
136
+ const updateData = getCacheAndClean(componentUpdateKey);
137
+ // apply the updates to the component rendering
138
+ if (updateData === null || updateData === void 0 ? void 0 : updateData.rendering) {
139
+ updateComponent(componentToUpdate, updateData.rendering.fields, updateData.rendering.params);
140
+ }
141
+ // generate the component instance if we are dealing with an AI-generated component
142
+ if ((updateData === null || updateData === void 0 ? void 0 : updateData.generatedComponentData) && !componentInitError && importMap) {
143
+ generatedComponentData = updateData.generatedComponentData;
144
+ try {
145
+ // use provided code and import map to create the component instance
146
+ Component = createComponentInstance(importMap, updateData.generatedComponentData);
147
+ }
148
+ catch (error) {
149
+ // error during component initialization - send error to client
150
+ componentInitError = getComponentInitError(codegen.DesignLibraryPreviewError.RenderInit, error);
151
+ }
152
+ }
153
+ }
154
+ else if (hasCache(componentPreviewKey) && !componentInitError && importMap) {
155
+ // we have a preview update, get it and clean the cache
156
+ designLibraryStatus = editing_1.DesignLibraryStatus.RENDERED;
157
+ const previewData = getCacheAndClean(componentPreviewKey);
158
+ if (previewData === null || previewData === void 0 ? void 0 : previewData.error) {
159
+ componentInitError = getComponentInitError(codegen.DesignLibraryPreviewError.GeneratedComponentFetch, previewData.error);
160
+ }
161
+ if (previewData === null || previewData === void 0 ? void 0 : previewData.generatedComponentData) {
162
+ generatedComponentData = previewData.generatedComponentData;
163
+ try {
164
+ // use provided code and import map to create the component instance
165
+ Component = createComponentInstance(importMap, previewData.generatedComponentData);
166
+ }
167
+ catch (error) {
168
+ // error during component initialization - send error to client
169
+ componentInitError = getComponentInitError(codegen.DesignLibraryPreviewError.RenderInit, error);
170
+ }
171
+ }
172
+ }
173
+ return (react_1.default.createElement(react_1.default.Fragment, null,
174
+ Component ? (react_1.default.createElement(react_3.DesignLibraryErrorBoundary, { uid: componentToUpdate.uid },
175
+ react_1.default.createElement(react_2.PlaceholderMetadata, { rendering: componentToUpdate },
176
+ react_1.default.createElement(Component, { fields: componentToUpdate.fields, params: componentToUpdate.params, key: Date.now() })))) : (react_1.default.createElement(react_2.AppPlaceholder, { name: layout_1.EDITING_COMPONENT_PLACEHOLDER, page: page, rendering: rendering, componentMap: componentMap, key: Date.now() })),
177
+ react_1.default.createElement(DesignLibraryClientEvents_1.DesignLibraryVariantGenerationEvents, { designLibraryStatus: designLibraryStatus, importMap: importMapInfo,
178
+ // pass a new object since we have mutated the original which leads to old reference passed to the client
179
+ component: Object.assign({}, componentToUpdate), componentInitError: componentInitError, generatedComponentData: generatedComponentData })));
180
+ };
181
+ exports.DesignLibraryServerVariantGeneration = DesignLibraryServerVariantGeneration;
182
+ /**
183
+ * Design Library component for rendering server components in app router application when variant generation is not enabled.
184
+ *
185
+ * Renders the **real** Sitecore component for `library` / `library-metadata` modes and,
186
+ * wires the **component update** handshake so the parent (Design Library) can send
187
+ * updated component props.
188
+ * Also renders the DesignLibraryPreviewEvents component which serves as a communication bridge between DesignLibraryServer and the Design Studio on the client side.
189
+ * @param {DesignLibraryServerPreviewProps} props The props. {@link DesignLibraryServerPreviewProps}
190
+ * @returns {JSX.Element} The preview surface, or `null` when not in Design Library mode.
191
+ */
192
+ const DesignLibraryServerPreview = async ({ page, rendering, componentMap, }) => {
193
+ var _a, _b;
194
+ let designLibraryStatus = editing_1.DesignLibraryStatus.READY;
195
+ let componentToUpdate = (_b = (_a = rendering === null || rendering === void 0 ? void 0 : rendering.placeholders) === null || _a === void 0 ? void 0 : _a[layout_1.EDITING_COMPONENT_PLACEHOLDER]) === null || _b === void 0 ? void 0 : _b[0];
196
+ if (!componentToUpdate)
197
+ return react_1.default.createElement(react_3.ErrorComponent, { message: "Rendering data is missing" });
198
+ if (!componentToUpdate.uid)
199
+ return react_1.default.createElement(react_3.ErrorComponent, { message: "Rendering UID is missing in the rendering data" });
200
+ const componentUpdateKey = `${editing_1.COMPONENT_UPDATE_CACHE_KEY_PREFIX}${componentToUpdate.uid}`;
201
+ // check if we have an update for this component in the global cache
202
+ if (hasCache(componentUpdateKey)) {
203
+ // we have an update, get it and clean the cache
204
+ designLibraryStatus = editing_1.DesignLibraryStatus.RENDERED;
205
+ const updateData = getCacheAndClean(componentUpdateKey);
206
+ // apply the updates to the component rendering
207
+ if (updateData === null || updateData === void 0 ? void 0 : updateData.rendering) {
208
+ updateComponent(componentToUpdate, updateData.rendering.fields, updateData.rendering.params);
209
+ }
210
+ }
211
+ return (react_1.default.createElement(react_1.default.Fragment, null,
212
+ react_1.default.createElement(react_2.AppPlaceholder, { name: layout_1.EDITING_COMPONENT_PLACEHOLDER, page: page, rendering: rendering, componentMap: componentMap, key: Date.now() }),
213
+ react_1.default.createElement(DesignLibraryClientEvents_1.DesignLibraryPreviewEvents, { designLibraryStatus: designLibraryStatus,
214
+ // pass a new object since we have mutated the original which leads to old reference passed to the client
215
+ component: Object.assign({}, componentToUpdate) })));
216
+ };
217
+ exports.DesignLibraryServerPreview = DesignLibraryServerPreview;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DesignLibraryApp = void 0;
4
+ var DesignLibraryApp_1 = require("./DesignLibraryApp");
5
+ Object.defineProperty(exports, "DesignLibraryApp", { enumerable: true, get: function () { return DesignLibraryApp_1.DesignLibraryApp; } });