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:  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:
@@ -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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user