@netlify/cache 1.4.0 → 1.5.0

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.
@@ -43,7 +43,21 @@ __export(main_exports, {
43
43
  });
44
44
  module.exports = __toCommonJS(main_exports);
45
45
 
46
+ // src/bootstrap/errors.ts
47
+ var ERROR_CODES = {
48
+ invalid_vary: "Responses must not use unsupported directives of the `Netlify-Vary` header (https://ntl.fyi/cache_api_invalid_vary).",
49
+ no_cache: "Responses must not set cache control headers with the `private`, `no-cache` or `no-store` directives (https://ntl.fyi/cache_api_no_cache).",
50
+ low_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_low_ttl).",
51
+ no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
52
+ no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
53
+ no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
54
+ invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
55
+ status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
56
+ };
57
+ var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
58
+
46
59
  // src/headers.ts
60
+ var ErrorDetail = "netlify-programmable-error";
47
61
  var ResourceHeaders = "netlify-programmable-headers";
48
62
  var ResourceStatus = "netlify-programmable-status";
49
63
  var ResourceStore = "netlify-programmable-store";
@@ -155,7 +169,7 @@ var NetlifyCache = class {
155
169
  }
156
170
  const context = __privateGet(this, _getContext).call(this);
157
171
  const resourceURL = extractAndValidateURL(request);
158
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
172
+ const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
159
173
  body: response.body,
160
174
  headers: {
161
175
  ...this[getInternalHeaders](context),
@@ -166,6 +180,11 @@ var NetlifyCache = class {
166
180
  duplex: "half",
167
181
  method: "POST"
168
182
  });
183
+ if (!cacheResponse.ok) {
184
+ const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
185
+ const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
186
+ console.warn(`Failed to write to the cache: ${errorMessage}`);
187
+ }
169
188
  }
170
189
  };
171
190
  _base64Encode = new WeakMap();
@@ -1,13 +1,42 @@
1
- import {
2
- NetlifyForwardedHost,
3
- ResourceHeaders,
4
- ResourceStatus,
5
- ResourceStore,
6
- UserAgent,
7
- __privateAdd,
8
- __privateGet,
9
- __privateSet
10
- } from "../chunk-I5FZDZ6V.js";
1
+ var __accessCheck = (obj, member, msg) => {
2
+ if (!member.has(obj))
3
+ throw TypeError("Cannot " + msg);
4
+ };
5
+ var __privateGet = (obj, member, getter) => {
6
+ __accessCheck(obj, member, "read from private field");
7
+ return getter ? getter.call(obj) : member.get(obj);
8
+ };
9
+ var __privateAdd = (obj, member, value) => {
10
+ if (member.has(obj))
11
+ throw TypeError("Cannot add the same private member more than once");
12
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
+ };
14
+ var __privateSet = (obj, member, value, setter) => {
15
+ __accessCheck(obj, member, "write to private field");
16
+ setter ? setter.call(obj, value) : member.set(obj, value);
17
+ return value;
18
+ };
19
+
20
+ // src/bootstrap/errors.ts
21
+ var ERROR_CODES = {
22
+ invalid_vary: "Responses must not use unsupported directives of the `Netlify-Vary` header (https://ntl.fyi/cache_api_invalid_vary).",
23
+ no_cache: "Responses must not set cache control headers with the `private`, `no-cache` or `no-store` directives (https://ntl.fyi/cache_api_no_cache).",
24
+ low_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_low_ttl).",
25
+ no_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_no_directive).",
26
+ no_ttl: "Responses must have a cache control header with a `max-age` or `s-maxage` directive (https://ntl.fyi/cache_api_no_ttl).",
27
+ no_status: "Responses must specify a status code (https://ntl.fyi/cache_api_no_status).",
28
+ invalid_directive: "Responses must have a cache control header with caching directives (https://ntl.fyi/cache_api_invalid_directive).",
29
+ status: "Responses must have a status code between 200 and 299 (https://ntl.fyi/cache_api_status)."
30
+ };
31
+ var GENERIC_ERROR = "The server has returned an unexpected error (https://ntl.fyi/cache_api_error).";
32
+
33
+ // src/headers.ts
34
+ var ErrorDetail = "netlify-programmable-error";
35
+ var ResourceHeaders = "netlify-programmable-headers";
36
+ var ResourceStatus = "netlify-programmable-status";
37
+ var ResourceStore = "netlify-programmable-store";
38
+ var NetlifyForwardedHost = "netlify-forwarded-host";
39
+ var UserAgent = "user-agent";
11
40
 
12
41
  // src/bootstrap/cache.ts
13
42
  var allowedProtocols = /* @__PURE__ */ new Set(["http:", "https:"]);
@@ -114,7 +143,7 @@ var NetlifyCache = class {
114
143
  }
115
144
  const context = __privateGet(this, _getContext).call(this);
116
145
  const resourceURL = extractAndValidateURL(request);
117
- await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
146
+ const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
118
147
  body: response.body,
119
148
  headers: {
120
149
  ...this[getInternalHeaders](context),
@@ -125,6 +154,11 @@ var NetlifyCache = class {
125
154
  duplex: "half",
126
155
  method: "POST"
127
156
  });
157
+ if (!cacheResponse.ok) {
158
+ const errorDetail = cacheResponse.headers.get(ErrorDetail) ?? "";
159
+ const errorMessage = ERROR_CODES[errorDetail] || GENERIC_ERROR;
160
+ console.warn(`Failed to write to the cache: ${errorMessage}`);
161
+ }
128
162
  }
129
163
  };
130
164
  _base64Encode = new WeakMap();
package/dist/main.js CHANGED
@@ -1,10 +1,9 @@
1
- import {
2
- CacheStatus,
3
- NetlifyCacheId,
4
- NetlifyCacheTag,
5
- NetlifyCdnCacheControl,
6
- NetlifyVary
7
- } from "./chunk-I5FZDZ6V.js";
1
+ // src/headers.ts
2
+ var CacheStatus = "cache-status";
3
+ var NetlifyCacheId = "netlify-cache-id";
4
+ var NetlifyCacheTag = "netlify-cache-tag";
5
+ var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
6
+ var NetlifyVary = "netlify-vary";
8
7
 
9
8
  // src/cache-headers/validation.ts
10
9
  var ensureArray = (value) => Array.isArray(value) ? value : [value];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/cache",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "TypeScript utilities for interacting with the Netlify cache",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1,46 +0,0 @@
1
- var __accessCheck = (obj, member, msg) => {
2
- if (!member.has(obj))
3
- throw TypeError("Cannot " + msg);
4
- };
5
- var __privateGet = (obj, member, getter) => {
6
- __accessCheck(obj, member, "read from private field");
7
- return getter ? getter.call(obj) : member.get(obj);
8
- };
9
- var __privateAdd = (obj, member, value) => {
10
- if (member.has(obj))
11
- throw TypeError("Cannot add the same private member more than once");
12
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
- };
14
- var __privateSet = (obj, member, value, setter) => {
15
- __accessCheck(obj, member, "write to private field");
16
- setter ? setter.call(obj, value) : member.set(obj, value);
17
- return value;
18
- };
19
-
20
- // src/headers.ts
21
- var CacheStatus = "cache-status";
22
- var NetlifyCacheId = "netlify-cache-id";
23
- var NetlifyCacheTag = "netlify-cache-tag";
24
- var NetlifyCdnCacheControl = "netlify-cdn-cache-control";
25
- var NetlifyVary = "netlify-vary";
26
- var ResourceHeaders = "netlify-programmable-headers";
27
- var ResourceStatus = "netlify-programmable-status";
28
- var ResourceStore = "netlify-programmable-store";
29
- var NetlifyForwardedHost = "netlify-forwarded-host";
30
- var UserAgent = "user-agent";
31
-
32
- export {
33
- __privateGet,
34
- __privateAdd,
35
- __privateSet,
36
- CacheStatus,
37
- NetlifyCacheId,
38
- NetlifyCacheTag,
39
- NetlifyCdnCacheControl,
40
- NetlifyVary,
41
- ResourceHeaders,
42
- ResourceStatus,
43
- ResourceStore,
44
- NetlifyForwardedHost,
45
- UserAgent
46
- };