@swell/apps-sdk 1.0.138 → 1.0.139

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
@@ -18130,10 +18130,10 @@ ${injects.join("\n")}<\/script>`;
18130
18130
  src: "https://cdn.swell.store/schema/685ef581b79e3e0012cf3222/fba884c7fe8122c472e130355fb52db7/collection-6.png"
18131
18131
  },
18132
18132
  "lifestyle-1": {
18133
- src: "https://cdn.swell.store/schema/685ef58bb79e3e0012cf3256/1da0da57ea80d90038fcf6b100a16fe6/lifestyle-1.png"
18133
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fc6/1777bcff40736610093833ef858b5936/lifestyle-1.png"
18134
18134
  },
18135
18135
  "lifestyle-2": {
18136
- src: "https://cdn.swell.store/schema/685ef58cb79e3e0012cf3259/f963448f1a6f4d6588fcd283b5c00c7c/lifestyle-2.png"
18136
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fcb/4d29197e3fa0b6d8255c1b7770f8dc12/lifestyle-2.png"
18137
18137
  },
18138
18138
  "product-apparel-1": {
18139
18139
  src: "https://cdn.swell.store/schema/685ef58eb79e3e0012cf3276/37c3c973a01287a580ab9db2e05dd36d/product-apparel-1.png"
@@ -18731,12 +18731,11 @@ ${injects.join("\n")}<\/script>`;
18731
18731
  this.configPaths = [];
18732
18732
  }
18733
18733
  async init(themeConfigs) {
18734
- const { swellHeaders } = this.swell;
18735
18734
  if (themeConfigs) {
18736
18735
  this.setConfigs(themeConfigs);
18737
18736
  return;
18738
18737
  }
18739
- if (!swellHeaders["theme-id"]) {
18738
+ if (!this.getThemeId()) {
18740
18739
  return;
18741
18740
  }
18742
18741
  await this.fetchManifest();
@@ -18775,46 +18774,74 @@ ${injects.join("\n")}<\/script>`;
18775
18774
  */
18776
18775
  setConfigs(themeConfigs) {
18777
18776
  this.configs = new Map(themeConfigs);
18777
+ this.manifest = /* @__PURE__ */ new Map();
18778
+ for (const { file_path, hash } of this.configs.values()) {
18779
+ this.manifest.set(file_path, hash);
18780
+ }
18778
18781
  this.configPaths = Array.from(this.configs.keys());
18779
- this.manifest = this.configPaths.reduce((manifest, path) => {
18780
- manifest[path] = this.configs.get(path)?.hash;
18781
- return manifest;
18782
- }, {});
18783
18782
  }
18784
18783
  /**
18785
18784
  * Preloads a theme version and configs. This is used to optimize initial theme load.
18786
18785
  */
18787
- async preloadTheme(version, configs) {
18786
+ async preloadTheme(payload) {
18787
+ const { version, configs } = payload;
18788
18788
  console.log(
18789
18789
  `ThemeLoader.preloadTheme${version?.hash ? ` - manifest: ${version.hash}` : ""}${configs?.length ? ` - configs: ${configs.length}` : ""}`
18790
18790
  );
18791
- await Promise2.all([
18792
- version && this.cacheManifest(version),
18793
- configs && Promise2.map(configs, (config) => this.cacheThemeConfig(config), {
18794
- concurrency: 10
18795
- })
18796
- ]);
18791
+ const promises = [];
18792
+ if (version) {
18793
+ promises.push(this.cacheManifest(version));
18794
+ }
18795
+ if (configs) {
18796
+ const themeId = this.getThemeId();
18797
+ promises.push(
18798
+ Promise2.map(
18799
+ configs,
18800
+ async (config) => {
18801
+ const promises2 = [
18802
+ this.cacheThemeConfig(config)
18803
+ ];
18804
+ if (themeId && config.file?.url) {
18805
+ promises2.push(
18806
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
18807
+ );
18808
+ }
18809
+ await Promise2.all(promises2);
18810
+ },
18811
+ { concurrency: 10 }
18812
+ )
18813
+ );
18814
+ }
18815
+ await Promise2.all(promises);
18797
18816
  }
18798
18817
  /**
18799
18818
  * Fetches a theme config by file path.
18800
18819
  */
18801
18820
  async fetchThemeConfig(filePath) {
18802
- const config = this.configs.get(filePath);
18803
- if (config !== void 0) {
18804
- return config;
18821
+ const themeConfig = this.configs.get(filePath);
18822
+ if (themeConfig !== void 0) {
18823
+ return themeConfig;
18805
18824
  }
18806
- const hash = this.manifest?.[filePath];
18807
- if (hash) {
18808
- const config2 = await this.getCache().get(
18809
- `config:${hash}`
18810
- );
18811
- if (config2) {
18812
- this.configs.set(filePath, config2);
18813
- return config2;
18825
+ const hash = this.manifest?.get(filePath);
18826
+ if (!hash) {
18827
+ return null;
18828
+ }
18829
+ const cache = this.getCache();
18830
+ let config = await cache.get(`config:${hash}`);
18831
+ if (config) {
18832
+ const themeId = this.getThemeId();
18833
+ if (themeId && config.file?.url) {
18834
+ const fileUrl = await cache.get(
18835
+ `file:${themeId}:${config.hash}`
18836
+ );
18837
+ if (fileUrl) {
18838
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18839
+ }
18814
18840
  }
18815
- return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18841
+ this.configs.set(filePath, config);
18842
+ return config;
18816
18843
  }
18817
- return null;
18844
+ return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18818
18845
  }
18819
18846
  async fetchThemeConfigsByPath(pathPrefix, pathSuffix) {
18820
18847
  const paths = this.configPaths.filter(
@@ -18823,9 +18850,7 @@ ${injects.join("\n")}<\/script>`;
18823
18850
  const configs = await Promise2.map(
18824
18851
  paths,
18825
18852
  (path) => this.fetchThemeConfig(path),
18826
- {
18827
- concurrency: 10
18828
- }
18853
+ { concurrency: 10 }
18829
18854
  );
18830
18855
  return configs.filter((config) => config !== null);
18831
18856
  }
@@ -18858,17 +18883,26 @@ ${injects.join("\n")}<\/script>`;
18858
18883
  }
18859
18884
  const configHashesUnresolved = [];
18860
18885
  const configsByHash = /* @__PURE__ */ new Map();
18886
+ const themeId = this.getThemeId();
18887
+ const cache = this.getCache();
18861
18888
  await Promise2.map(
18862
- Object.values(manifest),
18863
- (configHash) => {
18864
- return this.getCache().get(`config:${configHash}`).then((config) => {
18865
- if (config) {
18866
- configsByHash.set(config.hash, config);
18867
- this.configs.set(config.file_path, config);
18868
- } else {
18869
- configHashesUnresolved.push(configHash);
18889
+ manifest.values(),
18890
+ async (configHash) => {
18891
+ let config = await cache.get(`config:${configHash}`);
18892
+ if (!config) {
18893
+ configHashesUnresolved.push(configHash);
18894
+ return;
18895
+ }
18896
+ if (themeId && config.file?.url) {
18897
+ const fileUrl = await cache.get(
18898
+ `file:${themeId}:${configHash}`
18899
+ );
18900
+ if (fileUrl) {
18901
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18870
18902
  }
18871
- });
18903
+ }
18904
+ configsByHash.set(config.hash, config);
18905
+ this.configs.set(config.file_path, config);
18872
18906
  },
18873
18907
  { concurrency: 10 }
18874
18908
  );
@@ -18883,9 +18917,19 @@ ${injects.join("\n")}<\/script>`;
18883
18917
  configsByHash.set(config.hash, config);
18884
18918
  this.configs.set(config.file_path, config);
18885
18919
  }
18886
- await Promise2.map(newConfigs, (config) => this.cacheThemeConfig(config), {
18887
- concurrency: 10
18888
- });
18920
+ await Promise2.map(
18921
+ newConfigs,
18922
+ async (config) => {
18923
+ const promises = [this.cacheThemeConfig(config)];
18924
+ if (themeId && config.file?.url) {
18925
+ promises.push(
18926
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
18927
+ );
18928
+ }
18929
+ await Promise2.all(promises);
18930
+ },
18931
+ { concurrency: 10 }
18932
+ );
18889
18933
  }
18890
18934
  return Array.from(configsByHash.values());
18891
18935
  }
@@ -18905,6 +18949,12 @@ ${injects.join("\n")}<\/script>`;
18905
18949
  await this.getCache().set(`config:${config.hash}`, config);
18906
18950
  }
18907
18951
  }
18952
+ /**
18953
+ * Caches a CDN file url by config hash.
18954
+ */
18955
+ async cacheThemeFileUrl(themeId, configHash, fileUrl) {
18956
+ await this.getCache().set(`file:${themeId}:${configHash}`, fileUrl);
18957
+ }
18908
18958
  /**
18909
18959
  * Fetches the manifest (set of config hashes) for a theme version.
18910
18960
  */
@@ -18928,8 +18978,10 @@ ${injects.join("\n")}<\/script>`;
18928
18978
  manifest = themeVersion.manifest;
18929
18979
  }
18930
18980
  }
18931
- this.manifest = manifest;
18932
- this.configPaths = Object.keys(manifest || {});
18981
+ if (manifest) {
18982
+ this.manifest = new Map(Object.entries(manifest));
18983
+ this.configPaths = [...this.manifest.keys()];
18984
+ }
18933
18985
  return this.manifest;
18934
18986
  }
18935
18987
  /**
@@ -18947,7 +18999,7 @@ ${injects.join("\n")}<\/script>`;
18947
18999
  "/:themes:configs",
18948
19000
  {
18949
19001
  ...this.themeVersionQueryFilter(),
18950
- ...fetchAll ? {} : { hash: { $in: configHashes } },
19002
+ ...fetchAll ? void 0 : { hash: { $in: configHashes } },
18951
19003
  // TODO: paginate to support more than 1000 configs
18952
19004
  limit: 1e3,
18953
19005
  type: "theme",
@@ -18983,10 +19035,20 @@ ${injects.join("\n")}<\/script>`;
18983
19035
  if (hash) {
18984
19036
  config.hash = hash;
18985
19037
  }
18986
- await this.cacheThemeConfig(config);
19038
+ const themeId = this.getThemeId();
19039
+ const promises = [this.cacheThemeConfig(config)];
19040
+ if (themeId && config.file?.url) {
19041
+ promises.push(
19042
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
19043
+ );
19044
+ }
19045
+ await Promise2.all(promises);
18987
19046
  }
18988
19047
  return config ?? null;
18989
19048
  }
19049
+ getThemeId() {
19050
+ return this.swell.swellHeaders["theme-id"];
19051
+ }
18990
19052
  /**
18991
19053
  * Generates a Swell API query filter for this theme version.
18992
19054
  */
@@ -19597,8 +19659,8 @@ ${injects.join("\n")}<\/script>`;
19597
19659
  /**
19598
19660
  * Preloads updated theme configs. Used to optimize initial theme load.
19599
19661
  */
19600
- async preloadThemeConfigs(version, configs) {
19601
- await this.themeLoader.preloadTheme(version, configs);
19662
+ async preloadThemeConfigs(payload) {
19663
+ await this.themeLoader.preloadTheme(payload);
19602
19664
  }
19603
19665
  getPageConfigPath(pageId, altTemplate) {
19604
19666
  if (this.shopifyCompatibility) {