@swell/apps-sdk 1.0.166 → 1.0.168

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
@@ -14916,10 +14916,10 @@ function ShopifyArticle(instance, blog, blogCategory) {
14916
14916
  blog,
14917
14917
  (blog2) => blog2.date_updated || blog2.date_created
14918
14918
  ),
14919
- url: deferWith(
14920
- [blog, blog.category],
14921
- (blog2, blogCategory2) => blogCategory2 ? `/blogs/${blogCategory2?.slug}/${blog2.slug}` : ""
14922
- ),
14919
+ url: deferWith([blog, blog.category], (blog2, blogCategory2) => {
14920
+ const blogCategoryId = blogCategory2?.slug || blogCategory2?.id || blog2.category_id;
14921
+ return blogCategoryId ? `/blogs/${blogCategoryId}/${blog2.slug}` : "";
14922
+ }),
14923
14923
  user: defer(() => blog.author),
14924
14924
  // Comments not supported
14925
14925
  comment_post_url: "",
@@ -22659,11 +22659,18 @@ var HtmlCache = class {
22659
22659
  }
22660
22660
  canReadFromCache(request) {
22661
22661
  const method = request.method.toUpperCase();
22662
- return (method === "GET" || method === "HEAD") && this.isRequestCacheable(request);
22662
+ if (!(method === "GET" || method === "HEAD")) return false;
22663
+ const res = this.resolvePathRule(request);
22664
+ if (res.skip) return false;
22665
+ return this.isRequestCacheable(request);
22663
22666
  }
22664
22667
  canWriteToCache(request, response) {
22665
22668
  const method = request.method.toUpperCase();
22666
- return method === "GET" && response.ok && this.isRequestCacheable(request) && this.isResponseCacheable(response);
22669
+ if (method !== "GET" || !response.ok) return false;
22670
+ const res = this.resolvePathRule(request);
22671
+ if (res.skip) return false;
22672
+ if (!this.isRequestCacheable(request)) return false;
22673
+ return this.isResponseCacheable(response, res.rule);
22667
22674
  }
22668
22675
  createRevalidationRequest(request) {
22669
22676
  const headers = new Headers(request.headers);
@@ -22796,22 +22803,20 @@ var HtmlCache = class {
22796
22803
  }
22797
22804
  }
22798
22805
  isRequestCacheable(request) {
22799
- const url = new URL(request.url);
22800
22806
  if (request.headers.get("swell-deployment-mode") === "editor") return false;
22801
- if (this.cacheRules.pathRules) {
22802
- for (const rule of this.cacheRules.pathRules) {
22803
- if (this.pathMatches(rule.path, url.pathname) && rule.skip) {
22804
- return false;
22805
- }
22806
- }
22807
- }
22808
22807
  if (request.headers.get("cache-control")?.includes("no-cache"))
22809
22808
  return false;
22810
22809
  return true;
22811
22810
  }
22812
- isResponseCacheable(response) {
22813
- if (!response.headers.get("content-type")?.includes("text/html"))
22814
- return false;
22811
+ isResponseCacheable(response, matchedRule) {
22812
+ const contentType = response.headers.get("content-type") || "";
22813
+ const allowed = matchedRule?.contentTypes;
22814
+ if (Array.isArray(allowed) && allowed.length > 0) {
22815
+ const ok = allowed.some((ct) => contentType.toLowerCase().startsWith(ct));
22816
+ if (!ok) return false;
22817
+ } else {
22818
+ if (!contentType.includes("text/html")) return false;
22819
+ }
22815
22820
  if (response.headers.get("set-cookie")) return false;
22816
22821
  const cacheControl = response.headers.get("cache-control");
22817
22822
  if (cacheControl?.includes("no-store") || cacheControl?.includes("private"))
@@ -22823,30 +22828,10 @@ var HtmlCache = class {
22823
22828
  return mode === "preview" ? "preview" : "live";
22824
22829
  }
22825
22830
  getTTLForRequest(request) {
22826
- const url = new URL(request.url);
22827
- const mode = this.getDeploymentMode(request.headers);
22828
- if (this.cacheRules.pathRules) {
22829
- for (const rule of this.cacheRules.pathRules) {
22830
- if (this.pathMatches(rule.path, url.pathname) && rule.ttl !== void 0) {
22831
- return rule.ttl;
22832
- }
22833
- }
22834
- }
22835
- const defaults = this.cacheRules.defaults?.[mode];
22836
- return defaults?.ttl ?? DEFAULT_CACHE_RULES.defaults[mode].ttl;
22831
+ return this.resolvePathRule(request).effectiveTTL;
22837
22832
  }
22838
22833
  getSWRForRequest(request) {
22839
- const url = new URL(request.url);
22840
- const mode = this.getDeploymentMode(request.headers);
22841
- if (this.cacheRules.pathRules) {
22842
- for (const rule of this.cacheRules.pathRules) {
22843
- if (this.pathMatches(rule.path, url.pathname) && rule.swr !== void 0) {
22844
- return rule.swr;
22845
- }
22846
- }
22847
- }
22848
- const defaults = this.cacheRules.defaults?.[mode];
22849
- return defaults?.swr ?? DEFAULT_CACHE_RULES.defaults[mode].swr;
22834
+ return this.resolvePathRule(request).effectiveSWR;
22850
22835
  }
22851
22836
  getEntryAge(entry) {
22852
22837
  const t = Date.parse(entry.cacheTimeISO);
@@ -22870,6 +22855,29 @@ var HtmlCache = class {
22870
22855
  });
22871
22856
  return normalized;
22872
22857
  }
22858
+ resolvePathRule(request) {
22859
+ const url = new URL(request.url);
22860
+ const mode = this.getDeploymentMode(request.headers);
22861
+ const defaults = this.cacheRules.defaults?.[mode] ?? DEFAULT_CACHE_RULES.defaults[mode];
22862
+ let rule;
22863
+ if (this.cacheRules.pathRules) {
22864
+ for (const r of this.cacheRules.pathRules) {
22865
+ if (this.pathMatches(r.path, url.pathname)) {
22866
+ rule = r;
22867
+ break;
22868
+ }
22869
+ }
22870
+ }
22871
+ const effectiveTTL = (rule?.ttl !== void 0 ? rule.ttl : defaults?.ttl) ?? defaults.ttl;
22872
+ const effectiveSWR = (rule?.swr !== void 0 ? rule.swr : defaults?.swr) ?? defaults.swr;
22873
+ return {
22874
+ rule,
22875
+ mode,
22876
+ effectiveTTL,
22877
+ effectiveSWR,
22878
+ skip: Boolean(rule?.skip)
22879
+ };
22880
+ }
22873
22881
  normalizeSearchParams(searchParams) {
22874
22882
  const ignoredParams = [
22875
22883
  "utm_source",