feat(storage-*): add support for browser-based caching via etags (#10014)

This PR makes changes to every storage adapter in order to add
browser-based caching by returning etags, then checking for them into
incoming requests and responding a status code of `304` so the data
doesn't have to be returned again.

Performance improvements for cached subsequent requests:

![image](https://github.com/user-attachments/assets/e51b812c-63a0-4bdb-a396-0f172982cb07)


This respects `disableCache` in the dev tools.



Also fixes a bug with getting the latest image when using the Vercel
Blob Storage adapter.
This commit is contained in:
Paul
2024-12-18 14:54:36 -06:00
committed by GitHub
parent 194a8c189a
commit ef90ebb395
5 changed files with 83 additions and 2 deletions

View File

@@ -49,6 +49,7 @@ export const getHandler = ({ utApi }: Args): StaticHandler => {
}
const key = getKeyFromFilename(retrievedDoc, filename)
if (!key) {
return new Response(null, { status: 404, statusText: 'Not Found' })
}
@@ -67,10 +68,25 @@ export const getHandler = ({ utApi }: Args): StaticHandler => {
const blob = await response.blob()
const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match')
const objectEtag = response.headers.get('etag') as string
if (etagFromHeaders && etagFromHeaders === objectEtag) {
return new Response(null, {
headers: new Headers({
'Content-Length': String(blob.size),
'Content-Type': blob.type,
ETag: objectEtag,
}),
status: 304,
})
}
return new Response(blob, {
headers: new Headers({
'Content-Length': String(blob.size),
'Content-Type': blob.type,
ETag: objectEtag,
}),
status: 200,
})