@swell/apps-sdk 1.0.183 → 1.0.185

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
@@ -3053,7 +3053,7 @@ var LANG_TO_COUNTRY_CODES = {
3053
3053
  var CURRENCY_COUNTRIES = Object.freeze({
3054
3054
  USD: { name: "United States", iso: "US" },
3055
3055
  EUR: { name: "Europe", iso: "EU" },
3056
- GBP: { name: "United Kingdom", iso: "UK" },
3056
+ GBP: { name: "United Kingdom", iso: "GB" },
3057
3057
  JPY: { name: "Japan", iso: "JP" },
3058
3058
  CAD: { name: "Canada", iso: "CA" },
3059
3059
  AUD: { name: "Australia", iso: "AU" },
@@ -4779,7 +4779,7 @@ function getSelectedSubscriptionPurchaseOptionPlan(selectedPurchaseOptionType, s
4779
4779
  return selectedPlan || subscriptionPurchaseOption.plans[0];
4780
4780
  }
4781
4781
  var SORT_OPTIONS = Object.freeze([
4782
- { value: "", name: "Featured" },
4782
+ { value: "manual", name: "Featured" },
4783
4783
  { value: "popularity", name: "Popularity", query: "popularity desc" },
4784
4784
  { value: "price_asc", name: "Price, low to high", query: "price asc" },
4785
4785
  { value: "price_desc", name: "Price, high to low", query: "price desc" },
@@ -4789,7 +4789,7 @@ var SORT_OPTIONS = Object.freeze([
4789
4789
  { value: "name_desc", name: "Product name, Z-A", query: "name desc" }
4790
4790
  ]);
4791
4791
  async function getProductFilters(swell, productQuery) {
4792
- const sortBy = swell.queryParams.sort || "";
4792
+ const sortBy = swell.queryParams.sort || "manual";
4793
4793
  const filterQuery = productQueryWithFilters(swell, productQuery);
4794
4794
  return {
4795
4795
  filter_options: await getProductFiltersByQuery(swell, filterQuery),
@@ -4836,7 +4836,7 @@ function productQueryWithFilters(swell, query) {
4836
4836
  },
4837
4837
  {}
4838
4838
  );
4839
- const sortBy = swell.queryParams.sort || "";
4839
+ const sortBy = swell.queryParams.sort || "manual";
4840
4840
  return {
4841
4841
  sort: SORT_OPTIONS.find((option) => option.value === sortBy)?.query || void 0,
4842
4842
  $filters: filters2,
@@ -17583,14 +17583,14 @@ function getRobotsGlobals(canonicalUrl) {
17583
17583
  default_groups: [
17584
17584
  {
17585
17585
  user_agent: RobotsRule.from("User-agent", "*"),
17586
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17586
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17587
17587
  rules: defaultRules.map(
17588
17588
  (rule) => RobotsRule.from(rule.directive, rule.value)
17589
17589
  )
17590
17590
  },
17591
17591
  {
17592
17592
  user_agent: RobotsRule.from("User-agent", "AhrefsBot"),
17593
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17593
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17594
17594
  rules: [
17595
17595
  RobotsRule.from("Crawl-delay", "10"),
17596
17596
  ...defaultRules.map(
@@ -17600,7 +17600,7 @@ function getRobotsGlobals(canonicalUrl) {
17600
17600
  },
17601
17601
  {
17602
17602
  user_agent: RobotsRule.from("User-agent", "AhrefsSiteAudit"),
17603
- // sitemap: RobotsRule.from('Sitemap', sitemapUrl),
17603
+ sitemap: RobotsRule.from("Sitemap", sitemapUrl),
17604
17604
  rules: [
17605
17605
  RobotsRule.from("Crawl-delay", "10"),
17606
17606
  ...defaultRules.map(
@@ -19560,6 +19560,35 @@ var DEFAULT_CACHE_RULES = {
19560
19560
  },
19561
19561
  pathRules: [{ path: "/checkout/*", skip: true }]
19562
19562
  };
19563
+ var SANITIZE_CLIENT_HEADERS = Object.freeze([
19564
+ "connection",
19565
+ "proxy-connection",
19566
+ "keep-alive",
19567
+ "transfer-encoding",
19568
+ "upgrade",
19569
+ "proxy-authenticate",
19570
+ "proxy-authorization",
19571
+ "te",
19572
+ "trailers",
19573
+ "via",
19574
+ "alt-svc",
19575
+ "content-length",
19576
+ "content-encoding",
19577
+ // Also strip any legacy internal metadata keys
19578
+ "x-original-ttl",
19579
+ "x-original-swr",
19580
+ "x-cache-time"
19581
+ ]);
19582
+ var CACHE_KEY_HEADERS = Object.freeze([
19583
+ "cookie",
19584
+ "swell-storefront-id",
19585
+ "swell-app-id",
19586
+ "swell-access-token",
19587
+ "swell-theme-version-hash",
19588
+ "swell-cache-modified",
19589
+ "x-locale",
19590
+ "swell-storefront-context"
19591
+ ]);
19563
19592
  var HtmlCache = class {
19564
19593
  epoch;
19565
19594
  backend;
@@ -19567,7 +19596,23 @@ var HtmlCache = class {
19567
19596
  constructor(epoch, backend, cacheRules = DEFAULT_CACHE_RULES) {
19568
19597
  this.epoch = epoch;
19569
19598
  this.backend = backend;
19570
- this.cacheRules = cacheRules;
19599
+ this.cacheRules = {
19600
+ ...cacheRules,
19601
+ pathRules: cacheRules.pathRules?.map((rule) => ({
19602
+ ...rule,
19603
+ regex: this.convertPathToRegex(rule.path)
19604
+ }))
19605
+ };
19606
+ }
19607
+ /**
19608
+ * Converts wildcard pattern to regex and tests against path.
19609
+ *
19610
+ * - `*` matches any characters except `/`
19611
+ * - `**` matches any characters including `/`
19612
+ */
19613
+ convertPathToRegex(pattern) {
19614
+ const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19615
+ return new RegExp(`^${regex}$`);
19571
19616
  }
19572
19617
  async get(request) {
19573
19618
  const trace = createTraceId();
@@ -19658,18 +19703,18 @@ var HtmlCache = class {
19658
19703
  );
19659
19704
  return;
19660
19705
  }
19661
- const cacheTimeISO = (/* @__PURE__ */ new Date()).toISOString();
19706
+ const date = /* @__PURE__ */ new Date();
19662
19707
  const headers = this.normalizeHeaders(response.headers);
19663
19708
  const entry = {
19664
19709
  status: response.status,
19665
19710
  statusText: response.statusText,
19666
19711
  headers,
19667
19712
  body,
19668
- cacheTimeISO,
19713
+ cacheTimeISO: date.toISOString(),
19669
19714
  ttl,
19670
19715
  swr,
19671
19716
  etag: this.quoteETag(headers["etag"] || md5(body)),
19672
- lastModifiedUTC: headers["last-modified"] || new Date(cacheTimeISO).toUTCString()
19717
+ lastModifiedUTC: headers["last-modified"] || date.toUTCString()
19673
19718
  };
19674
19719
  const hardExpireSeconds = ttl + swr;
19675
19720
  await this.backend.write(cacheKey, entry, hardExpireSeconds);
@@ -19694,8 +19739,13 @@ var HtmlCache = class {
19694
19739
  }
19695
19740
  }
19696
19741
  canReadFromCache(request) {
19697
- const method = request.method.toUpperCase();
19698
- if (!(method === "GET" || method === "HEAD")) return false;
19742
+ switch (request.method.toUpperCase()) {
19743
+ case "GET":
19744
+ case "HEAD":
19745
+ break;
19746
+ default:
19747
+ return false;
19748
+ }
19699
19749
  const res = this.resolvePathRule(request);
19700
19750
  if (res.skip) return false;
19701
19751
  return this.isRequestCacheable(request);
@@ -19761,7 +19811,7 @@ var HtmlCache = class {
19761
19811
  try {
19762
19812
  const ifModDate = new Date(ifModifiedSince);
19763
19813
  const lastModDate = new Date(lastModified);
19764
- if (isNaN(ifModDate.getTime()) || isNaN(lastModDate.getTime())) {
19814
+ if (Number.isNaN(ifModDate.getTime()) || Number.isNaN(lastModDate.getTime())) {
19765
19815
  return false;
19766
19816
  }
19767
19817
  return ifModDate >= lastModDate;
@@ -19791,31 +19841,25 @@ var HtmlCache = class {
19791
19841
  return `"${value}"`;
19792
19842
  }
19793
19843
  sanitizeClientHeaders(headers) {
19794
- const HOP_BY_HOP = [
19795
- "connection",
19796
- "proxy-connection",
19797
- "keep-alive",
19798
- "transfer-encoding",
19799
- "upgrade",
19800
- "proxy-authenticate",
19801
- "proxy-authorization",
19802
- "te",
19803
- "trailers",
19804
- "via",
19805
- "alt-svc",
19806
- "content-length"
19807
- ];
19808
- for (const h of HOP_BY_HOP) headers.delete(h);
19809
- headers.delete("content-encoding");
19810
- headers.delete("x-original-ttl");
19811
- headers.delete("x-original-swr");
19812
- headers.delete("x-cache-time");
19844
+ for (const name of SANITIZE_CLIENT_HEADERS) {
19845
+ headers.delete(name);
19846
+ }
19847
+ }
19848
+ getCacheKeyHeaders(headers) {
19849
+ const acc = new Headers();
19850
+ for (const name of CACHE_KEY_HEADERS) {
19851
+ const value = headers.get(name);
19852
+ if (value) {
19853
+ acc.set(name, value);
19854
+ }
19855
+ }
19856
+ return acc;
19813
19857
  }
19814
19858
  generateVersionHash(headers) {
19815
- const swellData = this.extractSwellData(headers);
19859
+ const swellData = this.extractSwellData(headers.get("cookie"));
19816
19860
  const versionFactors = {
19817
19861
  store: headers.get("swell-storefront-id") || "",
19818
- app: (headers.get("swell-app-id") || "") + "@" + (headers.get("host") || ""),
19862
+ app: headers.get("swell-app-id") || "",
19819
19863
  auth: headers.get("swell-access-token") || "",
19820
19864
  theme: headers.get("swell-theme-version-hash") || "",
19821
19865
  modified: headers.get("swell-cache-modified") || "",
@@ -19826,8 +19870,7 @@ var HtmlCache = class {
19826
19870
  };
19827
19871
  return md5(JSON.stringify(versionFactors));
19828
19872
  }
19829
- extractSwellData(headers) {
19830
- const cookie = headers.get("cookie");
19873
+ extractSwellData(cookie) {
19831
19874
  if (!cookie) return {};
19832
19875
  const swellDataMatch = cookie.match(/swell-data=([^;]+)/);
19833
19876
  if (!swellDataMatch) return {};
@@ -19879,15 +19922,6 @@ var HtmlCache = class {
19879
19922
  const age = (Date.now() - t) / 1e3;
19880
19923
  return age < 0 ? 0 : age;
19881
19924
  }
19882
- /**
19883
- * Converts wildcard pattern to regex and tests against path.
19884
- * - * matches any characters except /
19885
- * - ** matches any characters including /
19886
- */
19887
- pathMatches(pattern, path) {
19888
- const regex = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "___DOUBLE_STAR___").replace(/\*/g, "[^/]*").replace(/___DOUBLE_STAR___/g, ".*");
19889
- return new RegExp(`^${regex}$`).test(path);
19890
- }
19891
19925
  normalizeHeaders(headers) {
19892
19926
  const normalized = {};
19893
19927
  headers.forEach((value, key) => {
@@ -19898,16 +19932,10 @@ var HtmlCache = class {
19898
19932
  resolvePathRule(request) {
19899
19933
  const url = new URL(request.url);
19900
19934
  const mode = this.getDeploymentMode(request.headers);
19901
- const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults[mode];
19902
- let rule;
19903
- if (this.cacheRules.pathRules) {
19904
- for (const r of this.cacheRules.pathRules) {
19905
- if (this.pathMatches(r.path, url.pathname)) {
19906
- rule = r;
19907
- break;
19908
- }
19909
- }
19910
- }
19935
+ const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults?.[mode];
19936
+ const rule = this.cacheRules.pathRules?.find(
19937
+ (r) => r.regex.test(url.pathname)
19938
+ );
19911
19939
  const effectiveTTL = (rule?.ttl !== void 0 ? rule.ttl : defaults?.ttl) ?? defaults.ttl;
19912
19940
  const effectiveSWR = (rule?.swr !== void 0 ? rule.swr : defaults?.swr) ?? defaults.swr;
19913
19941
  return {
@@ -19919,27 +19947,29 @@ var HtmlCache = class {
19919
19947
  };
19920
19948
  }
19921
19949
  normalizeSearchParams(searchParams) {
19922
- const ignoredParams = [
19923
- "utm_source",
19924
- "utm_medium",
19925
- "utm_campaign",
19926
- "utm_content",
19927
- "utm_term",
19928
- "fbclid",
19929
- "gclid",
19930
- "gbraid",
19931
- "wbraid",
19932
- "ref",
19933
- "source",
19934
- "mc_cid",
19935
- "mc_eid"
19936
- ];
19937
19950
  const relevantParams = [];
19938
19951
  searchParams.forEach((value, key) => {
19939
- if (!ignoredParams.includes(key)) {
19940
- relevantParams.push(
19941
- `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19942
- );
19952
+ switch (key) {
19953
+ case "utm_source":
19954
+ case "utm_medium":
19955
+ case "utm_campaign":
19956
+ case "utm_content":
19957
+ case "utm_term":
19958
+ case "fbclid":
19959
+ case "gclid":
19960
+ case "gbraid":
19961
+ case "wbraid":
19962
+ case "ref":
19963
+ case "source":
19964
+ case "mc_cid":
19965
+ case "mc_eid":
19966
+ break;
19967
+ default: {
19968
+ relevantParams.push(
19969
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
19970
+ );
19971
+ break;
19972
+ }
19943
19973
  }
19944
19974
  });
19945
19975
  return relevantParams.sort().join("&");