feat(storage-*): include modified headers into the response headers of files when using adapters (#12096)
This PR makes it so that `modifyResponseHeaders` is supported in our
adapters when set on the collection config. Previously it would be
ignored.
This means that users can now modify or append new headers to what's
returned by each service.
```ts
import type { CollectionConfig } from 'payload'
export const Media: CollectionConfig = {
slug: 'media',
upload: {
modifyResponseHeaders: ({ headers }) => {
const newHeaders = new Headers(headers) // Copy existing headers
newHeaders.set('X-Frame-Options', 'DENY') // Set new header
return newHeaders
},
},
}
```
Also adds support for `void` return on the `modifyResponseHeaders`
function in the case where the user just wants to use existing headers
and doesn't need more control.
eg:
```ts
import type { CollectionConfig } from 'payload'
export const Media: CollectionConfig = {
slug: 'media',
upload: {
modifyResponseHeaders: ({ headers }) => {
headers.set('X-Frame-Options', 'DENY') // You can directly set headers without returning
},
},
}
```
Manual testing checklist (no CI e2es setup for these envs yet):
- [x] GCS
- [x] S3
- [x] Azure
- [x] UploadThing
- [x] Vercel Blob
---------
Co-authored-by: James <james@trbl.design>
This commit is contained in:
@@ -9,9 +9,13 @@ type Args = {
|
||||
}
|
||||
|
||||
export const getHandler = ({ utApi }: Args): StaticHandler => {
|
||||
return async (req, { doc, params: { clientUploadContext, collection, filename } }) => {
|
||||
return async (
|
||||
req,
|
||||
{ doc, headers: incomingHeaders, params: { clientUploadContext, collection, filename } },
|
||||
) => {
|
||||
try {
|
||||
let key: string
|
||||
const collectionConfig = req.payload.collections[collection]?.config
|
||||
|
||||
if (
|
||||
clientUploadContext &&
|
||||
@@ -21,7 +25,6 @@ export const getHandler = ({ utApi }: Args): StaticHandler => {
|
||||
) {
|
||||
key = clientUploadContext.key
|
||||
} else {
|
||||
const collectionConfig = req.payload.collections[collection]?.config
|
||||
let retrievedDoc = doc
|
||||
|
||||
if (!retrievedDoc) {
|
||||
@@ -82,23 +85,32 @@ export const getHandler = ({ utApi }: Args): StaticHandler => {
|
||||
const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match')
|
||||
const objectEtag = response.headers.get('etag')
|
||||
|
||||
let headers = new Headers(incomingHeaders)
|
||||
|
||||
headers.append('Content-Length', String(blob.size))
|
||||
headers.append('Content-Type', blob.type)
|
||||
|
||||
if (objectEtag) {
|
||||
headers.append('ETag', objectEtag)
|
||||
}
|
||||
|
||||
if (
|
||||
collectionConfig?.upload &&
|
||||
typeof collectionConfig.upload === 'object' &&
|
||||
typeof collectionConfig.upload.modifyResponseHeaders === 'function'
|
||||
) {
|
||||
headers = collectionConfig.upload.modifyResponseHeaders({ headers }) || headers
|
||||
}
|
||||
|
||||
if (etagFromHeaders && etagFromHeaders === objectEtag) {
|
||||
return new Response(null, {
|
||||
headers: new Headers({
|
||||
'Content-Length': String(blob.size),
|
||||
'Content-Type': blob.type,
|
||||
ETag: objectEtag,
|
||||
}),
|
||||
headers,
|
||||
status: 304,
|
||||
})
|
||||
}
|
||||
|
||||
return new Response(blob, {
|
||||
headers: new Headers({
|
||||
'Content-Length': String(blob.size),
|
||||
'Content-Type': blob.type,
|
||||
ETag: objectEtag!,
|
||||
}),
|
||||
headers,
|
||||
status: 200,
|
||||
})
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user