@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.mjs CHANGED
@@ -18111,10 +18111,10 @@ var svgs = {
18111
18111
  src: "https://cdn.swell.store/schema/685ef581b79e3e0012cf3222/fba884c7fe8122c472e130355fb52db7/collection-6.png"
18112
18112
  },
18113
18113
  "lifestyle-1": {
18114
- src: "https://cdn.swell.store/schema/685ef58bb79e3e0012cf3256/1da0da57ea80d90038fcf6b100a16fe6/lifestyle-1.png"
18114
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fc6/1777bcff40736610093833ef858b5936/lifestyle-1.png"
18115
18115
  },
18116
18116
  "lifestyle-2": {
18117
- src: "https://cdn.swell.store/schema/685ef58cb79e3e0012cf3259/f963448f1a6f4d6588fcd283b5c00c7c/lifestyle-2.png"
18117
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fcb/4d29197e3fa0b6d8255c1b7770f8dc12/lifestyle-2.png"
18118
18118
  },
18119
18119
  "product-apparel-1": {
18120
18120
  src: "https://cdn.swell.store/schema/685ef58eb79e3e0012cf3276/37c3c973a01287a580ab9db2e05dd36d/product-apparel-1.png"
@@ -18712,12 +18712,11 @@ var ThemeLoader = class _ThemeLoader {
18712
18712
  this.configPaths = [];
18713
18713
  }
18714
18714
  async init(themeConfigs) {
18715
- const { swellHeaders } = this.swell;
18716
18715
  if (themeConfigs) {
18717
18716
  this.setConfigs(themeConfigs);
18718
18717
  return;
18719
18718
  }
18720
- if (!swellHeaders["theme-id"]) {
18719
+ if (!this.getThemeId()) {
18721
18720
  return;
18722
18721
  }
18723
18722
  await this.fetchManifest();
@@ -18756,46 +18755,74 @@ var ThemeLoader = class _ThemeLoader {
18756
18755
  */
18757
18756
  setConfigs(themeConfigs) {
18758
18757
  this.configs = new Map(themeConfigs);
18758
+ this.manifest = /* @__PURE__ */ new Map();
18759
+ for (const { file_path, hash } of this.configs.values()) {
18760
+ this.manifest.set(file_path, hash);
18761
+ }
18759
18762
  this.configPaths = Array.from(this.configs.keys());
18760
- this.manifest = this.configPaths.reduce((manifest, path) => {
18761
- manifest[path] = this.configs.get(path)?.hash;
18762
- return manifest;
18763
- }, {});
18764
18763
  }
18765
18764
  /**
18766
18765
  * Preloads a theme version and configs. This is used to optimize initial theme load.
18767
18766
  */
18768
- async preloadTheme(version, configs) {
18767
+ async preloadTheme(payload) {
18768
+ const { version, configs } = payload;
18769
18769
  console.log(
18770
18770
  `ThemeLoader.preloadTheme${version?.hash ? ` - manifest: ${version.hash}` : ""}${configs?.length ? ` - configs: ${configs.length}` : ""}`
18771
18771
  );
18772
- await Promise2.all([
18773
- version && this.cacheManifest(version),
18774
- configs && Promise2.map(configs, (config) => this.cacheThemeConfig(config), {
18775
- concurrency: 10
18776
- })
18777
- ]);
18772
+ const promises = [];
18773
+ if (version) {
18774
+ promises.push(this.cacheManifest(version));
18775
+ }
18776
+ if (configs) {
18777
+ const themeId = this.getThemeId();
18778
+ promises.push(
18779
+ Promise2.map(
18780
+ configs,
18781
+ async (config) => {
18782
+ const promises2 = [
18783
+ this.cacheThemeConfig(config)
18784
+ ];
18785
+ if (themeId && config.file?.url) {
18786
+ promises2.push(
18787
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
18788
+ );
18789
+ }
18790
+ await Promise2.all(promises2);
18791
+ },
18792
+ { concurrency: 10 }
18793
+ )
18794
+ );
18795
+ }
18796
+ await Promise2.all(promises);
18778
18797
  }
18779
18798
  /**
18780
18799
  * Fetches a theme config by file path.
18781
18800
  */
18782
18801
  async fetchThemeConfig(filePath) {
18783
- const config = this.configs.get(filePath);
18784
- if (config !== void 0) {
18785
- return config;
18802
+ const themeConfig = this.configs.get(filePath);
18803
+ if (themeConfig !== void 0) {
18804
+ return themeConfig;
18786
18805
  }
18787
- const hash = this.manifest?.[filePath];
18788
- if (hash) {
18789
- const config2 = await this.getCache().get(
18790
- `config:${hash}`
18791
- );
18792
- if (config2) {
18793
- this.configs.set(filePath, config2);
18794
- return config2;
18806
+ const hash = this.manifest?.get(filePath);
18807
+ if (!hash) {
18808
+ return null;
18809
+ }
18810
+ const cache = this.getCache();
18811
+ let config = await cache.get(`config:${hash}`);
18812
+ if (config) {
18813
+ const themeId = this.getThemeId();
18814
+ if (themeId && config.file?.url) {
18815
+ const fileUrl = await cache.get(
18816
+ `file:${themeId}:${config.hash}`
18817
+ );
18818
+ if (fileUrl) {
18819
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18820
+ }
18795
18821
  }
18796
- return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18822
+ this.configs.set(filePath, config);
18823
+ return config;
18797
18824
  }
18798
- return null;
18825
+ return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18799
18826
  }
18800
18827
  async fetchThemeConfigsByPath(pathPrefix, pathSuffix) {
18801
18828
  const paths = this.configPaths.filter(
@@ -18804,9 +18831,7 @@ var ThemeLoader = class _ThemeLoader {
18804
18831
  const configs = await Promise2.map(
18805
18832
  paths,
18806
18833
  (path) => this.fetchThemeConfig(path),
18807
- {
18808
- concurrency: 10
18809
- }
18834
+ { concurrency: 10 }
18810
18835
  );
18811
18836
  return configs.filter((config) => config !== null);
18812
18837
  }
@@ -18839,17 +18864,26 @@ var ThemeLoader = class _ThemeLoader {
18839
18864
  }
18840
18865
  const configHashesUnresolved = [];
18841
18866
  const configsByHash = /* @__PURE__ */ new Map();
18867
+ const themeId = this.getThemeId();
18868
+ const cache = this.getCache();
18842
18869
  await Promise2.map(
18843
- Object.values(manifest),
18844
- (configHash) => {
18845
- return this.getCache().get(`config:${configHash}`).then((config) => {
18846
- if (config) {
18847
- configsByHash.set(config.hash, config);
18848
- this.configs.set(config.file_path, config);
18849
- } else {
18850
- configHashesUnresolved.push(configHash);
18870
+ manifest.values(),
18871
+ async (configHash) => {
18872
+ let config = await cache.get(`config:${configHash}`);
18873
+ if (!config) {
18874
+ configHashesUnresolved.push(configHash);
18875
+ return;
18876
+ }
18877
+ if (themeId && config.file?.url) {
18878
+ const fileUrl = await cache.get(
18879
+ `file:${themeId}:${configHash}`
18880
+ );
18881
+ if (fileUrl) {
18882
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18851
18883
  }
18852
- });
18884
+ }
18885
+ configsByHash.set(config.hash, config);
18886
+ this.configs.set(config.file_path, config);
18853
18887
  },
18854
18888
  { concurrency: 10 }
18855
18889
  );
@@ -18864,9 +18898,19 @@ var ThemeLoader = class _ThemeLoader {
18864
18898
  configsByHash.set(config.hash, config);
18865
18899
  this.configs.set(config.file_path, config);
18866
18900
  }
18867
- await Promise2.map(newConfigs, (config) => this.cacheThemeConfig(config), {
18868
- concurrency: 10
18869
- });
18901
+ await Promise2.map(
18902
+ newConfigs,
18903
+ async (config) => {
18904
+ const promises = [this.cacheThemeConfig(config)];
18905
+ if (themeId && config.file?.url) {
18906
+ promises.push(
18907
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
18908
+ );
18909
+ }
18910
+ await Promise2.all(promises);
18911
+ },
18912
+ { concurrency: 10 }
18913
+ );
18870
18914
  }
18871
18915
  return Array.from(configsByHash.values());
18872
18916
  }
@@ -18886,6 +18930,12 @@ var ThemeLoader = class _ThemeLoader {
18886
18930
  await this.getCache().set(`config:${config.hash}`, config);
18887
18931
  }
18888
18932
  }
18933
+ /**
18934
+ * Caches a CDN file url by config hash.
18935
+ */
18936
+ async cacheThemeFileUrl(themeId, configHash, fileUrl) {
18937
+ await this.getCache().set(`file:${themeId}:${configHash}`, fileUrl);
18938
+ }
18889
18939
  /**
18890
18940
  * Fetches the manifest (set of config hashes) for a theme version.
18891
18941
  */
@@ -18909,8 +18959,10 @@ var ThemeLoader = class _ThemeLoader {
18909
18959
  manifest = themeVersion.manifest;
18910
18960
  }
18911
18961
  }
18912
- this.manifest = manifest;
18913
- this.configPaths = Object.keys(manifest || {});
18962
+ if (manifest) {
18963
+ this.manifest = new Map(Object.entries(manifest));
18964
+ this.configPaths = [...this.manifest.keys()];
18965
+ }
18914
18966
  return this.manifest;
18915
18967
  }
18916
18968
  /**
@@ -18928,7 +18980,7 @@ var ThemeLoader = class _ThemeLoader {
18928
18980
  "/:themes:configs",
18929
18981
  {
18930
18982
  ...this.themeVersionQueryFilter(),
18931
- ...fetchAll ? {} : { hash: { $in: configHashes } },
18983
+ ...fetchAll ? void 0 : { hash: { $in: configHashes } },
18932
18984
  // TODO: paginate to support more than 1000 configs
18933
18985
  limit: 1e3,
18934
18986
  type: "theme",
@@ -18964,10 +19016,20 @@ var ThemeLoader = class _ThemeLoader {
18964
19016
  if (hash) {
18965
19017
  config.hash = hash;
18966
19018
  }
18967
- await this.cacheThemeConfig(config);
19019
+ const themeId = this.getThemeId();
19020
+ const promises = [this.cacheThemeConfig(config)];
19021
+ if (themeId && config.file?.url) {
19022
+ promises.push(
19023
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
19024
+ );
19025
+ }
19026
+ await Promise2.all(promises);
18968
19027
  }
18969
19028
  return config ?? null;
18970
19029
  }
19030
+ getThemeId() {
19031
+ return this.swell.swellHeaders["theme-id"];
19032
+ }
18971
19033
  /**
18972
19034
  * Generates a Swell API query filter for this theme version.
18973
19035
  */
@@ -19578,8 +19640,8 @@ var SwellTheme3 = class {
19578
19640
  /**
19579
19641
  * Preloads updated theme configs. Used to optimize initial theme load.
19580
19642
  */
19581
- async preloadThemeConfigs(version, configs) {
19582
- await this.themeLoader.preloadTheme(version, configs);
19643
+ async preloadThemeConfigs(payload) {
19644
+ await this.themeLoader.preloadTheme(payload);
19583
19645
  }
19584
19646
  getPageConfigPath(pageId, altTemplate) {
19585
19647
  if (this.shopifyCompatibility) {