You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(assets): Use entity-tags to revalidate cached remote images (#12426)
* feat(assets): Store etag to refresh cached images without a full download
* Seperate loading and revalidating functions
* Add changeset
* Updates based on requested changes
* Wording changes, use stale cache on failure to revalidate
* Add If-Modified-Since as cache revalidation method
* Update .changeset/red-poems-pay.md
Co-authored-by: Sarah Rainsberger <[email protected]>
---------
Co-authored-by: Matt Kane <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
Astro will now store [entity tags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) and the [Last-Modified](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified) date for cached remote images and use them to revalidate the cache when it goes stale.
// For remote images, instead of saving the image directly, we save a JSON file with the image dataand expiration date from the server
167
+
// For remote images, instead of saving the image directly, we save a JSON file with the image data, expiration date, etag and last-modified date from the server
* Revalidate a cached remote asset using its entity-tag or modified date.
34
+
* Uses the [If-None-Match](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match) and [If-Modified-Since](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since)
35
+
* headers to check with the remote server if the cached version of a remote asset is still up to date.
36
+
* The remote server may respond that the cached asset is still up-to-date if the entity-tag or modification time matches (304 Not Modified), or respond with an updated asset (200 OK)
37
+
* @param src - url to remote asset
38
+
* @param revalidationData - an object containing the stored Entity-Tag of the cached asset and/or the Last Modified time
39
+
* @returns An ImageData object containing the asset data, a new expiry time, and the asset's etag. The data buffer will be empty if the asset was not modified.
// Asset not modified: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304
53
+
if(!res.ok&&res.status!==304){
54
+
thrownewError(
55
+
`Failed to revalidate cached remote image ${src}. The request did not return a 200 OK / 304 NOT MODIFIED response. (received ${res.status}${res.statusText})`,
56
+
);
57
+
}
58
+
59
+
constdata=Buffer.from(awaitres.arrayBuffer());
60
+
61
+
if(res.ok&&!data.length){
62
+
// Server did not include body but indicated cache was stale
63
+
returnawaitloadRemoteImage(src);
64
+
}
65
+
66
+
// calculate an expiration date based on the response's TTL
67
+
constpolicy=newCachePolicy(
68
+
webToCachePolicyRequest(req),
69
+
webToCachePolicyResponse(
70
+
res.ok ? res : newResponse(null,{status: 200,headers: res.headers}),
71
+
),// 304 responses themselves are not cachable, so just pretend to get the refreshed TTL
0 commit comments