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(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>
Copy file name to clipboardExpand all lines: docs/upload/overview.mdx
+44-2Lines changed: 44 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,6 +116,7 @@ _An asterisk denotes that an option is required._
116
116
|**`withMetadata`**| If specified, appends metadata to the output image file. Accepts a boolean or a function that receives `metadata` and `req`, returning a boolean. |
117
117
|**`hideFileInputOnCreate`**| Set to `true` to prevent the admin UI from showing file inputs during document creation, useful for programmatic file generation. |
118
118
|**`hideRemoveFile`**| Set to `true` to prevent the admin UI having a way to remove an existing file while editing. |
119
+
|**`modifyResponseHeaders`**| Accepts an object with existing `headers` and allows you to manipulate the response headers for media files. [More](#modifying-response-headers)|
119
120
120
121
### Payload-wide Upload Options
121
122
@@ -453,7 +454,7 @@ To fetch files from **restricted URLs** that would otherwise be blocked by CORS,
453
454
454
455
Here’s how to configure the pasteURL option to control remote URL fetching:
@@ -519,3 +520,44 @@ _An asterisk denotes that an option is required._
519
520
## Access Control
520
521
521
522
All files that are uploaded to each Collection automatically support the `read`[Access Control](/docs/access-control/overview) function from the Collection itself. You can use this to control who should be allowed to see your uploads, and who should not.
523
+
524
+
## Modifying response headers
525
+
526
+
You can modify the response headers for files by specifying the `modifyResponseHeaders` option in your upload config. This option accepts an object with existing headers and allows you to manipulate the response headers for media files.
527
+
528
+
### Modifying existing headers
529
+
530
+
With this method you can directly interface with the `Headers` object and modify the existing headers to append or remove headers.
531
+
532
+
```ts
533
+
importtype { CollectionConfig } from'payload'
534
+
535
+
exportconst Media:CollectionConfig= {
536
+
slug: 'media',
537
+
upload: {
538
+
modifyResponseHeaders: ({ headers }) => {
539
+
headers.set('X-Frame-Options', 'DENY') // You can directly set headers without returning
540
+
},
541
+
},
542
+
}
543
+
```
544
+
545
+
### Return new headers
546
+
547
+
You can also return a new `Headers` object with the modified headers. This is useful if you want to set new headers or remove existing ones.
0 commit comments