@netlify/cache
Advanced tools
+41
-0
@@ -32,2 +32,3 @@ "use strict"; | ||
| getCacheStatus: () => getCacheStatus, | ||
| needsRevalidation: () => needsRevalidation, | ||
| setCacheHeaders: () => setCacheHeaders | ||
@@ -270,2 +271,15 @@ }); | ||
| }; | ||
| var needsRevalidation = (response) => { | ||
| const header = response.headers.get(CacheStatus); | ||
| if (!header) { | ||
| return false; | ||
| } | ||
| for (const value of header.split(",")) { | ||
| const { attributes, name } = parseCacheStatusValue(value); | ||
| if (name === CACHE_DURABLE && attributes.detail === "client-revalidate") { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
| var getCacheStatus = (input) => { | ||
@@ -341,2 +355,15 @@ if (typeof input === "string") { | ||
| if (cached) { | ||
| if (needsRevalidation(cached)) { | ||
| const { fetch: fetchFn = globalThis.fetch } = cacheOptions; | ||
| const revalidation = performBackgroundRevalidation(request, cache, cacheSettings, fetchFn); | ||
| if (onCachePut) { | ||
| await onCachePut(revalidation); | ||
| } else { | ||
| const netlifyGlobal = globalThis.Netlify; | ||
| const requestContext = netlifyGlobal?.context; | ||
| if (requestContext) { | ||
| requestContext.waitUntil(revalidation); | ||
| } | ||
| } | ||
| } | ||
| return cached; | ||
@@ -368,2 +395,15 @@ } | ||
| }; | ||
| var performBackgroundRevalidation = async (request, cache, cacheSettings, fetchFn) => { | ||
| try { | ||
| const fresh = await fetchFn(request); | ||
| if (!fresh.body) { | ||
| return; | ||
| } | ||
| const cacheResponse = new Response(fresh.body, fresh); | ||
| applyHeaders(cacheResponse.headers, cacheHeaders(cacheSettings)); | ||
| await cache.put(request, cacheResponse); | ||
| } catch (error) { | ||
| console.warn("`fetchWithCache` has failed to revalidate a stale response:", error); | ||
| } | ||
| }; | ||
@@ -387,3 +427,4 @@ // src/constants.ts | ||
| getCacheStatus, | ||
| needsRevalidation, | ||
| setCacheHeaders | ||
| }); |
+10
-1
@@ -133,2 +133,11 @@ import { N as NetlifyCache } from './cache-B9TsVKLp.cjs'; | ||
| /** | ||
| * Returns whether a cached response includes a signal that the client should | ||
| * perform a background revalidation. This may happen when using the Cache API | ||
| * with the `stale-while-revalidate` directive, since unlike the regular cache, | ||
| * the client is the one responsible for explicitly inserting new entries into | ||
| * the cache. So when this returns `true`, the caller should fetch the resource | ||
| * and write the fresh response back to the cache with `cache.put()`. | ||
| */ | ||
| declare const needsRevalidation: (response: Response) => boolean; | ||
| /** | ||
| * Retrieves information about how a response has interacted with Netlify's | ||
@@ -197,2 +206,2 @@ * global caching infrastructure, including whether the response has been | ||
| export { DAY, HOUR, MINUTE, WEEK, YEAR, cacheHeaders, caches, fetchWithCache, getCacheStatus, setCacheHeaders }; | ||
| export { DAY, HOUR, MINUTE, WEEK, YEAR, cacheHeaders, caches, fetchWithCache, getCacheStatus, needsRevalidation, setCacheHeaders }; |
+10
-1
@@ -133,2 +133,11 @@ import { N as NetlifyCache } from './cache-B9TsVKLp.js'; | ||
| /** | ||
| * Returns whether a cached response includes a signal that the client should | ||
| * perform a background revalidation. This may happen when using the Cache API | ||
| * with the `stale-while-revalidate` directive, since unlike the regular cache, | ||
| * the client is the one responsible for explicitly inserting new entries into | ||
| * the cache. So when this returns `true`, the caller should fetch the resource | ||
| * and write the fresh response back to the cache with `cache.put()`. | ||
| */ | ||
| declare const needsRevalidation: (response: Response) => boolean; | ||
| /** | ||
| * Retrieves information about how a response has interacted with Netlify's | ||
@@ -197,2 +206,2 @@ * global caching infrastructure, including whether the response has been | ||
| export { DAY, HOUR, MINUTE, WEEK, YEAR, cacheHeaders, caches, fetchWithCache, getCacheStatus, setCacheHeaders }; | ||
| export { DAY, HOUR, MINUTE, WEEK, YEAR, cacheHeaders, caches, fetchWithCache, getCacheStatus, needsRevalidation, setCacheHeaders }; |
+40
-0
@@ -234,2 +234,15 @@ // src/polyfill.ts | ||
| }; | ||
| var needsRevalidation = (response) => { | ||
| const header = response.headers.get(CacheStatus); | ||
| if (!header) { | ||
| return false; | ||
| } | ||
| for (const value of header.split(",")) { | ||
| const { attributes, name } = parseCacheStatusValue(value); | ||
| if (name === CACHE_DURABLE && attributes.detail === "client-revalidate") { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
| var getCacheStatus = (input) => { | ||
@@ -305,2 +318,15 @@ if (typeof input === "string") { | ||
| if (cached) { | ||
| if (needsRevalidation(cached)) { | ||
| const { fetch: fetchFn = globalThis.fetch } = cacheOptions; | ||
| const revalidation = performBackgroundRevalidation(request, cache, cacheSettings, fetchFn); | ||
| if (onCachePut) { | ||
| await onCachePut(revalidation); | ||
| } else { | ||
| const netlifyGlobal = globalThis.Netlify; | ||
| const requestContext = netlifyGlobal?.context; | ||
| if (requestContext) { | ||
| requestContext.waitUntil(revalidation); | ||
| } | ||
| } | ||
| } | ||
| return cached; | ||
@@ -332,2 +358,15 @@ } | ||
| }; | ||
| var performBackgroundRevalidation = async (request, cache, cacheSettings, fetchFn) => { | ||
| try { | ||
| const fresh = await fetchFn(request); | ||
| if (!fresh.body) { | ||
| return; | ||
| } | ||
| const cacheResponse = new Response(fresh.body, fresh); | ||
| applyHeaders(cacheResponse.headers, cacheHeaders(cacheSettings)); | ||
| await cache.put(request, cacheResponse); | ||
| } catch (error) { | ||
| console.warn("`fetchWithCache` has failed to revalidate a stale response:", error); | ||
| } | ||
| }; | ||
@@ -350,3 +389,4 @@ // src/constants.ts | ||
| getCacheStatus, | ||
| needsRevalidation, | ||
| setCacheHeaders | ||
| }; |
+1
-1
| { | ||
| "name": "@netlify/cache", | ||
| "version": "3.3.5", | ||
| "version": "3.4.0", | ||
| "description": "TypeScript utilities for interacting with the Netlify cache", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
65554
6.24%1578
6.05%7
40%30
25%