@hubspot/cms-dev-server 0.4.2 → 0.4.3

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.
package/dist/index.js CHANGED
@@ -28,7 +28,7 @@ function resolveIslandPlugin() {
28
28
  viteCommand = config.command;
29
29
  },
30
30
  load(id) {
31
- if (id.endsWith("?island")) {
31
+ if (id.endsWith("?island") || id.endsWith("?client")) {
32
32
  const [idWithNoParam] = id.split("?");
33
33
  const componentExtension = path.extname(idWithNoParam);
34
34
  const componentFilenameWithNoExtension = path.basename(
@@ -71,9 +71,9 @@ var collectCssInDev = (modulePathToStartWith, viteServer, cssModulesImported = /
71
71
  ).map((cssModule) => createPreloadLink(cssModule.url)).join("\n")}
72
72
  `;
73
73
  };
74
- var writeIslandDataToJsGlobal = (tweakedIslandsArray, indent = 0) => {
74
+ var serializeIslandDataToJSON = (tweakedIslandsArray, indent = 0) => {
75
75
  const indentString = new Array(indent).fill(" ").join("");
76
- return `window.__islands = ${JSON.stringify(
76
+ return `${JSON.stringify(
77
77
  tweakedIslandsArray.map(
78
78
  ({
79
79
  id,
@@ -83,24 +83,50 @@ var writeIslandDataToJsGlobal = (tweakedIslandsArray, indent = 0) => {
83
83
  hydrateOn,
84
84
  clientOnly,
85
85
  priority,
86
- url
86
+ url,
87
+ sharedStateID
87
88
  }) => {
88
- return {
89
+ const output = {
89
90
  id,
90
91
  moduleName,
91
92
  moduleId,
92
- props,
93
93
  hydrateOn,
94
94
  clientOnly,
95
95
  priority,
96
+ props,
96
97
  url
97
98
  };
99
+ if (sharedStateID) {
100
+ output.sharedStateID = sharedStateID;
101
+ }
102
+ return output;
98
103
  }
99
104
  ),
100
105
  null,
101
106
  2
102
107
  ).replace(/\n/gm, `
103
- ${indentString}`)};`;
108
+ ${indentString}`)}`;
109
+ };
110
+ var writeSharedStateToJsGlobal = (islands, perServerRequestData, indent = 0) => {
111
+ const indentString = new Array(indent).fill(" ").join("");
112
+ const sharedStateIDs = islands.map((island) => island.sharedStateID).filter((x) => !!x);
113
+ if (sharedStateIDs.length > 0) {
114
+ const stateByID = {};
115
+ sharedStateIDs.forEach(
116
+ (id) => stateByID[id] = perServerRequestData.get(id)
117
+ );
118
+ const stringifiedState = JSON.stringify(stateByID, null, 2);
119
+ return `
120
+
121
+ var newState = ${stringifiedState};
122
+ if(window.__hsSSInit) {
123
+ window.__hsSSInit = Object.assign(window.__hsSSInit, newState);
124
+ } else {
125
+ window.__hsSSInit = newState;
126
+ }`.replace(/\n/gm, `
127
+ ${indentString}`);
128
+ }
129
+ return "";
104
130
  };
105
131
  function getIslandUrls(roots, viteDevServer) {
106
132
  return roots.map((island) => {
@@ -117,8 +143,12 @@ function getIslandUrls(roots, viteDevServer) {
117
143
  return tweakedIsland;
118
144
  });
119
145
  }
120
- var createIslandsGlobalJsForClientInDev = (roots, viteDevServer, indent) => {
121
- return writeIslandDataToJsGlobal(getIslandUrls(roots, viteDevServer), indent);
146
+ var createIslandsGlobalJsForClientInDev = (roots, perServerRequestData, viteDevServer, indent) => {
147
+ const islands = getIslandUrls(roots, viteDevServer);
148
+ return `window.__islands = ${serializeIslandDataToJSON(
149
+ islands,
150
+ indent
151
+ )}${writeSharedStateToJsGlobal(islands, perServerRequestData, indent)}`;
122
152
  };
123
153
  var viteRoot;
124
154
  var viteCommand;
@@ -131,7 +161,7 @@ function resolveIslandPlugin() {
131
161
  viteCommand = config.command;
132
162
  },
133
163
  load(id) {
134
- if (id.endsWith("?island")) {
164
+ if (id.endsWith("?island") || id.endsWith("?client")) {
135
165
  const [idWithNoParam] = id.split("?");
136
166
  const componentExtension = path.extname(idWithNoParam);
137
167
  const componentFilenameWithNoExtension = path.basename(
@@ -386,5 +416,6 @@ export {
386
416
  renderOnServer,
387
417
  renderStreamToPromise,
388
418
  resolveIslandPlugin,
419
+ serializeIslandDataToJSON,
389
420
  setIn
390
421
  };
package/dist/run.js CHANGED
@@ -69,9 +69,9 @@ import { join as joinPosix } from "path/posix";
69
69
  import { basename } from "path";
70
70
  import { cloneElement } from "react";
71
71
  import { renderToStaticMarkup } from "react-dom/server";
72
- var writeIslandDataToJsGlobal = (tweakedIslandsArray, indent = 0) => {
72
+ var serializeIslandDataToJSON = (tweakedIslandsArray, indent = 0) => {
73
73
  const indentString = new Array(indent).fill(" ").join("");
74
- return `window.__islands = ${JSON.stringify(
74
+ return `${JSON.stringify(
75
75
  tweakedIslandsArray.map(
76
76
  ({
77
77
  id,
@@ -81,24 +81,50 @@ var writeIslandDataToJsGlobal = (tweakedIslandsArray, indent = 0) => {
81
81
  hydrateOn,
82
82
  clientOnly,
83
83
  priority,
84
- url: url3
84
+ url: url3,
85
+ sharedStateID
85
86
  }) => {
86
- return {
87
+ const output = {
87
88
  id,
88
89
  moduleName,
89
90
  moduleId,
90
- props,
91
91
  hydrateOn,
92
92
  clientOnly,
93
93
  priority,
94
+ props,
94
95
  url: url3
95
96
  };
97
+ if (sharedStateID) {
98
+ output.sharedStateID = sharedStateID;
99
+ }
100
+ return output;
96
101
  }
97
102
  ),
98
103
  null,
99
104
  2
100
105
  ).replace(/\n/gm, `
101
- ${indentString}`)};`;
106
+ ${indentString}`)}`;
107
+ };
108
+ var writeSharedStateToJsGlobal = (islands, perServerRequestData, indent = 0) => {
109
+ const indentString = new Array(indent).fill(" ").join("");
110
+ const sharedStateIDs = islands.map((island) => island.sharedStateID).filter((x) => !!x);
111
+ if (sharedStateIDs.length > 0) {
112
+ const stateByID = {};
113
+ sharedStateIDs.forEach(
114
+ (id) => stateByID[id] = perServerRequestData.get(id)
115
+ );
116
+ const stringifiedState = JSON.stringify(stateByID, null, 2);
117
+ return `
118
+
119
+ var newState = ${stringifiedState};
120
+ if(window.__hsSSInit) {
121
+ window.__hsSSInit = Object.assign(window.__hsSSInit, newState);
122
+ } else {
123
+ window.__hsSSInit = newState;
124
+ }`.replace(/\n/gm, `
125
+ ${indentString}`);
126
+ }
127
+ return "";
102
128
  };
103
129
  function getIslandUrls(roots, viteDevServer) {
104
130
  return roots.map((island) => {
@@ -115,8 +141,12 @@ function getIslandUrls(roots, viteDevServer) {
115
141
  return tweakedIsland;
116
142
  });
117
143
  }
118
- var createIslandsGlobalJsForClientInDev = (roots, viteDevServer, indent) => {
119
- return writeIslandDataToJsGlobal(getIslandUrls(roots, viteDevServer), indent);
144
+ var createIslandsGlobalJsForClientInDev = (roots, perServerRequestData, viteDevServer, indent) => {
145
+ const islands = getIslandUrls(roots, viteDevServer);
146
+ return `window.__islands = ${serializeIslandDataToJSON(
147
+ islands,
148
+ indent
149
+ )}${writeSharedStateToJsGlobal(islands, perServerRequestData, indent)}`;
120
150
  };
121
151
  var viteRoot;
122
152
  var viteCommand;
@@ -129,7 +159,7 @@ function resolveIslandPlugin() {
129
159
  viteCommand = config.command;
130
160
  },
131
161
  load(id) {
132
- if (id.endsWith("?island")) {
162
+ if (id.endsWith("?island") || id.endsWith("?client")) {
133
163
  const [idWithNoParam] = id.split("?");
134
164
  const componentExtension = path.extname(idWithNoParam);
135
165
  const componentFilenameWithNoExtension = path.basename(
@@ -529,7 +559,12 @@ var createHtmlShellDependingOnRenderContext = async (baseTemplatePath, url3, tit
529
559
  htmlTemplate = htmlTemplate.replace(
530
560
  "<!--islandsglobal-->",
531
561
  `<script>
532
- ${createIslandsGlobalJsForClientInDev(renderContext.islands, viteServer, 4)}
562
+ ${createIslandsGlobalJsForClientInDev(
563
+ renderContext.islands,
564
+ renderContext.perServerRequestData,
565
+ viteServer,
566
+ 4
567
+ )}
533
568
  </script>
534
569
  `
535
570
  );
@@ -545,7 +580,7 @@ var createHtmlShellDependingOnRenderContext = async (baseTemplatePath, url3, tit
545
580
  }
546
581
  return viteServer.transformIndexHtml(url3, htmlTemplate);
547
582
  };
548
- var getIslandRuntimeAndGlobalToInject = async (sortedIslandRoots, viteServer) => {
583
+ var getIslandRuntimeAndGlobalToInject = async (sortedIslandRoots, perServerRequestData, viteServer) => {
549
584
  const internalSsrPath = packagePath(import.meta.url, "internalSsr");
550
585
  const { createIslandsGlobalJsForClientInDev: createIslandsGlobalJsForClientInDev2 } = await viteServer.ssrLoadModule(internalSsrPath);
551
586
  const islandRuntimeScript = await viteServer.transformIndexHtml(
@@ -555,6 +590,7 @@ var getIslandRuntimeAndGlobalToInject = async (sortedIslandRoots, viteServer) =>
555
590
  return `${islandRuntimeScript}
556
591
  <script>${createIslandsGlobalJsForClientInDev2(
557
592
  sortedIslandRoots,
593
+ perServerRequestData,
558
594
  viteServer,
559
595
  4
560
596
  )}</script>
@@ -1086,6 +1122,9 @@ ${error.error.message}`);
1086
1122
  import {
1087
1123
  buildStaticEnvConfig as buildStaticEnvConfig3
1088
1124
  } from "@hubspot/cms-components/config";
1125
+ import {
1126
+ mergeServerRequestData_internal
1127
+ } from "@hubspot/cms-components";
1089
1128
  var makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId) => {
1090
1129
  const url3 = new URL(urlToProxy);
1091
1130
  console.log(`Proxying request to ${url3.href}`, {
@@ -1207,6 +1246,7 @@ async function proxySpecificPage(response, portalId, hublet, contentId, webRoot2
1207
1246
  const numPlaceholders = localRenderPlaceholders.length;
1208
1247
  let numJsRenderFailures = 0;
1209
1248
  let islands = [];
1249
+ let mergedPerRequestState = /* @__PURE__ */ new Map();
1210
1250
  const renderPromisesByChunkIndex = new Array(
1211
1251
  numPlaceholders
1212
1252
  );
@@ -1237,6 +1277,10 @@ async function proxySpecificPage(response, portalId, hublet, contentId, webRoot2
1237
1277
  if (renderContext.islands.length > 0) {
1238
1278
  islands = islands.concat(renderContext.islands);
1239
1279
  }
1280
+ mergedPerRequestState = mergeServerRequestData_internal(
1281
+ mergedPerRequestState,
1282
+ renderContext.perServerRequestData
1283
+ );
1240
1284
  if (cssNeeded) {
1241
1285
  return `${cssNeeded}
1242
1286
  ${html}`;
@@ -1244,6 +1288,12 @@ ${html}`;
1244
1288
  return html;
1245
1289
  }).catch((e) => {
1246
1290
  numJsRenderFailures += 1;
1291
+ console.log(
1292
+ "Attempted to render",
1293
+ localRenderPlaceholder.path,
1294
+ "with props",
1295
+ localRenderPlaceholder.json
1296
+ );
1247
1297
  console.error(e);
1248
1298
  return renderLocalError_default(localRenderPlaceholder.path, e);
1249
1299
  });
@@ -1264,6 +1314,7 @@ ${html}`;
1264
1314
  const sortedIslandRoots = sortIslandRootsByPriority(islands);
1265
1315
  const islandRuntimeAndGlobalScriptHtml = await getIslandRuntimeAndGlobalToInject(
1266
1316
  sortedIslandRoots,
1317
+ mergedPerRequestState,
1267
1318
  viteServer
1268
1319
  );
1269
1320
  response.write(islandRuntimeAndGlobalScriptHtml);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubspot/cms-dev-server",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@hubspot/cli-lib": "^3.0.12",
27
- "@hubspot/cms-components": "^0.4.2",
27
+ "@hubspot/cms-components": "^0.4.3",
28
28
  "@storybook/addon-actions": "^7.0.0-beta.19",
29
29
  "@storybook/addon-essentials": "^7.0.0-beta.19",
30
30
  "@storybook/addon-interactions": "^7.0.0-beta.19",
@@ -77,8 +77,8 @@
77
77
  "types"
78
78
  ],
79
79
  "nodemonConfig": {
80
- "delay": "2000ms",
81
80
  "ext": "js,json,ts,tsx,jsx,html",
81
+ "delay": "2000ms",
82
82
  "ignore": [
83
83
  "testApp/*",
84
84
  "./dist/*"
@@ -95,5 +95,5 @@
95
95
  "publishConfig": {
96
96
  "access": "public"
97
97
  },
98
- "gitHead": "977f3d499c0813345ef95393b6e1c53021429ea6"
98
+ "gitHead": "7d2696951f3aeeae3085382ebe1a66ba94ecc748"
99
99
  }