@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.cjs CHANGED
@@ -18236,10 +18236,10 @@ var svgs = {
18236
18236
  src: "https://cdn.swell.store/schema/685ef581b79e3e0012cf3222/fba884c7fe8122c472e130355fb52db7/collection-6.png"
18237
18237
  },
18238
18238
  "lifestyle-1": {
18239
- src: "https://cdn.swell.store/schema/685ef58bb79e3e0012cf3256/1da0da57ea80d90038fcf6b100a16fe6/lifestyle-1.png"
18239
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fc6/1777bcff40736610093833ef858b5936/lifestyle-1.png"
18240
18240
  },
18241
18241
  "lifestyle-2": {
18242
- src: "https://cdn.swell.store/schema/685ef58cb79e3e0012cf3259/f963448f1a6f4d6588fcd283b5c00c7c/lifestyle-2.png"
18242
+ src: "https://cdn.swell.store/schema/6870eddc1657130012643fcb/4d29197e3fa0b6d8255c1b7770f8dc12/lifestyle-2.png"
18243
18243
  },
18244
18244
  "product-apparel-1": {
18245
18245
  src: "https://cdn.swell.store/schema/685ef58eb79e3e0012cf3276/37c3c973a01287a580ab9db2e05dd36d/product-apparel-1.png"
@@ -18837,12 +18837,11 @@ var ThemeLoader = class _ThemeLoader {
18837
18837
  this.configPaths = [];
18838
18838
  }
18839
18839
  async init(themeConfigs) {
18840
- const { swellHeaders } = this.swell;
18841
18840
  if (themeConfigs) {
18842
18841
  this.setConfigs(themeConfigs);
18843
18842
  return;
18844
18843
  }
18845
- if (!swellHeaders["theme-id"]) {
18844
+ if (!this.getThemeId()) {
18846
18845
  return;
18847
18846
  }
18848
18847
  await this.fetchManifest();
@@ -18881,46 +18880,74 @@ var ThemeLoader = class _ThemeLoader {
18881
18880
  */
18882
18881
  setConfigs(themeConfigs) {
18883
18882
  this.configs = new Map(themeConfigs);
18883
+ this.manifest = /* @__PURE__ */ new Map();
18884
+ for (const { file_path, hash } of this.configs.values()) {
18885
+ this.manifest.set(file_path, hash);
18886
+ }
18884
18887
  this.configPaths = Array.from(this.configs.keys());
18885
- this.manifest = this.configPaths.reduce((manifest, path) => {
18886
- manifest[path] = this.configs.get(path)?.hash;
18887
- return manifest;
18888
- }, {});
18889
18888
  }
18890
18889
  /**
18891
18890
  * Preloads a theme version and configs. This is used to optimize initial theme load.
18892
18891
  */
18893
- async preloadTheme(version, configs) {
18892
+ async preloadTheme(payload) {
18893
+ const { version, configs } = payload;
18894
18894
  console.log(
18895
18895
  `ThemeLoader.preloadTheme${version?.hash ? ` - manifest: ${version.hash}` : ""}${configs?.length ? ` - configs: ${configs.length}` : ""}`
18896
18896
  );
18897
- await Promise2.all([
18898
- version && this.cacheManifest(version),
18899
- configs && Promise2.map(configs, (config) => this.cacheThemeConfig(config), {
18900
- concurrency: 10
18901
- })
18902
- ]);
18897
+ const promises = [];
18898
+ if (version) {
18899
+ promises.push(this.cacheManifest(version));
18900
+ }
18901
+ if (configs) {
18902
+ const themeId = this.getThemeId();
18903
+ promises.push(
18904
+ Promise2.map(
18905
+ configs,
18906
+ async (config) => {
18907
+ const promises2 = [
18908
+ this.cacheThemeConfig(config)
18909
+ ];
18910
+ if (themeId && config.file?.url) {
18911
+ promises2.push(
18912
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
18913
+ );
18914
+ }
18915
+ await Promise2.all(promises2);
18916
+ },
18917
+ { concurrency: 10 }
18918
+ )
18919
+ );
18920
+ }
18921
+ await Promise2.all(promises);
18903
18922
  }
18904
18923
  /**
18905
18924
  * Fetches a theme config by file path.
18906
18925
  */
18907
18926
  async fetchThemeConfig(filePath) {
18908
- const config = this.configs.get(filePath);
18909
- if (config !== void 0) {
18910
- return config;
18927
+ const themeConfig = this.configs.get(filePath);
18928
+ if (themeConfig !== void 0) {
18929
+ return themeConfig;
18911
18930
  }
18912
- const hash = this.manifest?.[filePath];
18913
- if (hash) {
18914
- const config2 = await this.getCache().get(
18915
- `config:${hash}`
18916
- );
18917
- if (config2) {
18918
- this.configs.set(filePath, config2);
18919
- return config2;
18931
+ const hash = this.manifest?.get(filePath);
18932
+ if (!hash) {
18933
+ return null;
18934
+ }
18935
+ const cache = this.getCache();
18936
+ let config = await cache.get(`config:${hash}`);
18937
+ if (config) {
18938
+ const themeId = this.getThemeId();
18939
+ if (themeId && config.file?.url) {
18940
+ const fileUrl = await cache.get(
18941
+ `file:${themeId}:${config.hash}`
18942
+ );
18943
+ if (fileUrl) {
18944
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18945
+ }
18920
18946
  }
18921
- return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18947
+ this.configs.set(filePath, config);
18948
+ return config;
18922
18949
  }
18923
- return null;
18950
+ return this.fetchThemeConfigsFromSourceByPath(filePath, hash);
18924
18951
  }
18925
18952
  async fetchThemeConfigsByPath(pathPrefix, pathSuffix) {
18926
18953
  const paths = this.configPaths.filter(
@@ -18929,9 +18956,7 @@ var ThemeLoader = class _ThemeLoader {
18929
18956
  const configs = await Promise2.map(
18930
18957
  paths,
18931
18958
  (path) => this.fetchThemeConfig(path),
18932
- {
18933
- concurrency: 10
18934
- }
18959
+ { concurrency: 10 }
18935
18960
  );
18936
18961
  return configs.filter((config) => config !== null);
18937
18962
  }
@@ -18964,17 +18989,26 @@ var ThemeLoader = class _ThemeLoader {
18964
18989
  }
18965
18990
  const configHashesUnresolved = [];
18966
18991
  const configsByHash = /* @__PURE__ */ new Map();
18992
+ const themeId = this.getThemeId();
18993
+ const cache = this.getCache();
18967
18994
  await Promise2.map(
18968
- Object.values(manifest),
18969
- (configHash) => {
18970
- return this.getCache().get(`config:${configHash}`).then((config) => {
18971
- if (config) {
18972
- configsByHash.set(config.hash, config);
18973
- this.configs.set(config.file_path, config);
18974
- } else {
18975
- configHashesUnresolved.push(configHash);
18995
+ manifest.values(),
18996
+ async (configHash) => {
18997
+ let config = await cache.get(`config:${configHash}`);
18998
+ if (!config) {
18999
+ configHashesUnresolved.push(configHash);
19000
+ return;
19001
+ }
19002
+ if (themeId && config.file?.url) {
19003
+ const fileUrl = await cache.get(
19004
+ `file:${themeId}:${configHash}`
19005
+ );
19006
+ if (fileUrl) {
19007
+ config = { ...config, file: { ...config.file, url: fileUrl } };
18976
19008
  }
18977
- });
19009
+ }
19010
+ configsByHash.set(config.hash, config);
19011
+ this.configs.set(config.file_path, config);
18978
19012
  },
18979
19013
  { concurrency: 10 }
18980
19014
  );
@@ -18989,9 +19023,19 @@ var ThemeLoader = class _ThemeLoader {
18989
19023
  configsByHash.set(config.hash, config);
18990
19024
  this.configs.set(config.file_path, config);
18991
19025
  }
18992
- await Promise2.map(newConfigs, (config) => this.cacheThemeConfig(config), {
18993
- concurrency: 10
18994
- });
19026
+ await Promise2.map(
19027
+ newConfigs,
19028
+ async (config) => {
19029
+ const promises = [this.cacheThemeConfig(config)];
19030
+ if (themeId && config.file?.url) {
19031
+ promises.push(
19032
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
19033
+ );
19034
+ }
19035
+ await Promise2.all(promises);
19036
+ },
19037
+ { concurrency: 10 }
19038
+ );
18995
19039
  }
18996
19040
  return Array.from(configsByHash.values());
18997
19041
  }
@@ -19011,6 +19055,12 @@ var ThemeLoader = class _ThemeLoader {
19011
19055
  await this.getCache().set(`config:${config.hash}`, config);
19012
19056
  }
19013
19057
  }
19058
+ /**
19059
+ * Caches a CDN file url by config hash.
19060
+ */
19061
+ async cacheThemeFileUrl(themeId, configHash, fileUrl) {
19062
+ await this.getCache().set(`file:${themeId}:${configHash}`, fileUrl);
19063
+ }
19014
19064
  /**
19015
19065
  * Fetches the manifest (set of config hashes) for a theme version.
19016
19066
  */
@@ -19034,8 +19084,10 @@ var ThemeLoader = class _ThemeLoader {
19034
19084
  manifest = themeVersion.manifest;
19035
19085
  }
19036
19086
  }
19037
- this.manifest = manifest;
19038
- this.configPaths = Object.keys(manifest || {});
19087
+ if (manifest) {
19088
+ this.manifest = new Map(Object.entries(manifest));
19089
+ this.configPaths = [...this.manifest.keys()];
19090
+ }
19039
19091
  return this.manifest;
19040
19092
  }
19041
19093
  /**
@@ -19053,7 +19105,7 @@ var ThemeLoader = class _ThemeLoader {
19053
19105
  "/:themes:configs",
19054
19106
  {
19055
19107
  ...this.themeVersionQueryFilter(),
19056
- ...fetchAll ? {} : { hash: { $in: configHashes } },
19108
+ ...fetchAll ? void 0 : { hash: { $in: configHashes } },
19057
19109
  // TODO: paginate to support more than 1000 configs
19058
19110
  limit: 1e3,
19059
19111
  type: "theme",
@@ -19089,10 +19141,20 @@ var ThemeLoader = class _ThemeLoader {
19089
19141
  if (hash) {
19090
19142
  config.hash = hash;
19091
19143
  }
19092
- await this.cacheThemeConfig(config);
19144
+ const themeId = this.getThemeId();
19145
+ const promises = [this.cacheThemeConfig(config)];
19146
+ if (themeId && config.file?.url) {
19147
+ promises.push(
19148
+ this.cacheThemeFileUrl(themeId, config.hash, config.file.url)
19149
+ );
19150
+ }
19151
+ await Promise2.all(promises);
19093
19152
  }
19094
19153
  return config ?? null;
19095
19154
  }
19155
+ getThemeId() {
19156
+ return this.swell.swellHeaders["theme-id"];
19157
+ }
19096
19158
  /**
19097
19159
  * Generates a Swell API query filter for this theme version.
19098
19160
  */
@@ -19703,8 +19765,8 @@ var SwellTheme3 = class {
19703
19765
  /**
19704
19766
  * Preloads updated theme configs. Used to optimize initial theme load.
19705
19767
  */
19706
- async preloadThemeConfigs(version, configs) {
19707
- await this.themeLoader.preloadTheme(version, configs);
19768
+ async preloadThemeConfigs(payload) {
19769
+ await this.themeLoader.preloadTheme(payload);
19708
19770
  }
19709
19771
  getPageConfigPath(pageId, altTemplate) {
19710
19772
  if (this.shopifyCompatibility) {