@weaverse/hydrogen 5.16.2 → 5.16.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.mjs CHANGED
@@ -251,6 +251,67 @@ function hasWeaverseStudio(window2) {
251
251
  return "weaverseStudio" in window2 && typeof window2.weaverseStudio === "object";
252
252
  }
253
253
 
254
+ // src/utils/studio-script-src.ts
255
+ var DEFAULT_WEAVERSE_HOST = "https://studio.weaverse.io";
256
+ var TRUSTED_STUDIO_DOMAINS = ["weaverse.io", "weaverse.dev"];
257
+ function isLoopbackHostname(hostname) {
258
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname.endsWith(".localhost");
259
+ }
260
+ function isTrustedStudioHost(host, options) {
261
+ let parsed;
262
+ try {
263
+ parsed = new URL(host);
264
+ } catch {
265
+ return false;
266
+ }
267
+ let { hostname, protocol } = parsed;
268
+ if (options?.allowLoopback && isLoopbackHostname(hostname)) {
269
+ return protocol === "http:" || protocol === "https:";
270
+ }
271
+ if (protocol !== "https:") {
272
+ return false;
273
+ }
274
+ return TRUSTED_STUDIO_DOMAINS.some(
275
+ (domain) => hostname === domain || hostname.endsWith(`.${domain}`)
276
+ );
277
+ }
278
+ function getStudioScriptSrc(mode) {
279
+ let {
280
+ isDesignMode,
281
+ isPreviewMode,
282
+ isRevisionPreview,
283
+ weaverseHost,
284
+ weaverseVersion
285
+ } = mode;
286
+ let base = `${weaverseHost}/static/studio/hydrogen`;
287
+ let version = `?v=${weaverseVersion}`;
288
+ if (isRevisionPreview || isPreviewMode) {
289
+ return `${base}/preview.js${version}`;
290
+ }
291
+ if (isDesignMode) {
292
+ return `${base}/index.js${version}`;
293
+ }
294
+ return null;
295
+ }
296
+ function resolveStudioScriptSrc(search, storefrontHostname = "") {
297
+ let params = new URLSearchParams(search);
298
+ let last = (key) => params.getAll(key).at(-1);
299
+ let host = last("weaverseHost");
300
+ if (host) {
301
+ let allowLoopback = isLoopbackHostname(storefrontHostname);
302
+ if (!isTrustedStudioHost(host, { allowLoopback })) {
303
+ return null;
304
+ }
305
+ }
306
+ return getStudioScriptSrc({
307
+ isDesignMode: last("isDesignMode") === "true",
308
+ isPreviewMode: last("isPreviewMode") === "true",
309
+ isRevisionPreview: params.has("__revisionId"),
310
+ weaverseHost: host || DEFAULT_WEAVERSE_HOST,
311
+ weaverseVersion: last("weaverseVersion") || ""
312
+ });
313
+ }
314
+
254
315
  // src/utils/index.ts
255
316
  var warnedTypes = /* @__PURE__ */ new Set();
256
317
  function logOnce(type, message) {
@@ -314,8 +375,9 @@ function getWeaverseConfigs(request, env) {
314
375
  let url = new URL(request.url);
315
376
  let isRevisionPreview = url.searchParams.has("__revisionId");
316
377
  let configuredWeaverseHost = WEAVERSE_HOST || envFromProcess.WEAVERSE_HOST;
317
- let resolvedWeaverseHost = weaverseHost || configuredWeaverseHost || "https://studio.weaverse.io";
318
- let resolvedWeaverseApiBase = weaverseHost ? weaverseHost : WEAVERSE_PUBLIC_API_BASE || envFromProcess.WEAVERSE_PUBLIC_API_BASE || (configuredWeaverseHost && configuredWeaverseHost !== "https://studio.weaverse.io" ? configuredWeaverseHost : "https://api.weaverse.io");
378
+ let trustedUrlHost = typeof weaverseHost === "string" && isTrustedStudioHost(weaverseHost) ? weaverseHost : "";
379
+ let resolvedWeaverseHost = trustedUrlHost || configuredWeaverseHost || "https://studio.weaverse.io";
380
+ let resolvedWeaverseApiBase = trustedUrlHost ? trustedUrlHost : WEAVERSE_PUBLIC_API_BASE || envFromProcess.WEAVERSE_PUBLIC_API_BASE || (configuredWeaverseHost && configuredWeaverseHost !== "https://studio.weaverse.io" ? configuredWeaverseHost : "https://api.weaverse.io");
319
381
  return {
320
382
  projectId: weaverseProjectId || WEAVERSE_PROJECT_ID || envFromProcess.WEAVERSE_PROJECT_ID || "",
321
383
  weaverseHost: resolvedWeaverseHost,
@@ -720,6 +782,16 @@ function waitForStudio() {
720
782
  check();
721
783
  });
722
784
  }
785
+ function useStudioConnect() {
786
+ let { search } = useLocation();
787
+ useEffect(() => {
788
+ let hostname = typeof window === "undefined" ? "" : window.location.hostname;
789
+ let src = resolveStudioScriptSrc(search, hostname);
790
+ if (src) {
791
+ loadScript(src).catch(console.error);
792
+ }
793
+ }, [search]);
794
+ }
723
795
  function useStudio(weaverse) {
724
796
  let { revalidate } = useRevalidator();
725
797
  let navigation = useNavigation();
@@ -735,22 +807,31 @@ function useStudio(weaverse) {
735
807
  isRevisionPreview
736
808
  } = weaverse;
737
809
  useEffect(() => {
738
- if (navigation.state === "idle") {
739
- if (isRevisionPreview || isPreviewMode) {
740
- let previewSrc = `${weaverseHost}/static/studio/hydrogen/preview.js?v=${weaverseVersion}`;
741
- loadScript(previewSrc).catch(console.error);
742
- } else if (isDesignMode) {
743
- weaverse.internal = {
744
- ...weaverse.internal,
745
- navigate,
746
- revalidate,
747
- merchantOverrides,
748
- themeSettingsStore,
749
- themeTextStore
750
- };
751
- let studioSrc = `${weaverseHost}/static/studio/hydrogen/index.js?v=${weaverseVersion}`;
752
- loadScript(studioSrc).then(waitForStudio).then((studio) => studio?.init(weaverse)).catch(console.error);
753
- }
810
+ if (navigation.state !== "idle") {
811
+ return;
812
+ }
813
+ let src = getStudioScriptSrc({
814
+ isDesignMode,
815
+ isPreviewMode,
816
+ isRevisionPreview,
817
+ weaverseHost,
818
+ weaverseVersion
819
+ });
820
+ if (!src) {
821
+ return;
822
+ }
823
+ if (isRevisionPreview || isPreviewMode) {
824
+ loadScript(src).catch(console.error);
825
+ } else {
826
+ weaverse.internal = {
827
+ ...weaverse.internal,
828
+ navigate,
829
+ revalidate,
830
+ merchantOverrides,
831
+ themeSettingsStore,
832
+ themeTextStore
833
+ };
834
+ loadScript(src).then(waitForStudio).then((studio) => studio?.init(weaverse)).catch(console.error);
754
835
  }
755
836
  }, [pathname, search, navigation.state]);
756
837
  usePixel(weaverse);
@@ -959,6 +1040,10 @@ var StudioBridge = memo(({ context }) => {
959
1040
  useStudio(context);
960
1041
  return null;
961
1042
  });
1043
+ var StudioConnect = memo(() => {
1044
+ useStudioConnect();
1045
+ return null;
1046
+ });
962
1047
  var registeredComponentLists = /* @__PURE__ */ new WeakSet();
963
1048
  function RenderRoot(props) {
964
1049
  const { data, components, dataContext } = props;
@@ -1046,14 +1131,17 @@ function withWeaverse(Component, options) {
1046
1131
  if (!themeTextStoreRef.current) {
1047
1132
  themeTextStoreRef.current = new ThemeTextStore();
1048
1133
  }
1049
- return /* @__PURE__ */ jsx(ThemeSettingsStoreContext.Provider, { value: themeSettingsStore, children: /* @__PURE__ */ jsx(
1134
+ return /* @__PURE__ */ jsx(ThemeSettingsStoreContext.Provider, { value: themeSettingsStore, children: /* @__PURE__ */ jsxs(
1050
1135
  ThemeTextProvider,
1051
1136
  {
1052
1137
  merchantOverrides: themeTextData.merchantOverrides,
1053
1138
  staticContent: themeTextData.staticContent,
1054
1139
  t: options?.t,
1055
1140
  themeTextStore: themeTextStoreRef.current,
1056
- children: /* @__PURE__ */ jsx(Component, { ...props })
1141
+ children: [
1142
+ /* @__PURE__ */ jsx(StudioConnect, {}),
1143
+ /* @__PURE__ */ jsx(Component, { ...props })
1144
+ ]
1057
1145
  }
1058
1146
  ) });
1059
1147
  };