Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage-s3): add cacheControlMaxAge option #10438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/upload/storage-adapters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ export default buildConfig({

See the the [AWS SDK Package](https://github.com/aws/aws-sdk-js-v3) and [`S3ClientConfig`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3) object for guidance on AWS S3 configuration.


| Option | Description | Default |
| -------------------- | -------------------------------------------------------------------- | ------------------------------------------ |
| `enabled` | Whether or not to enable the plugin | `true` |
| `collections` | Collections to apply the s3 adapter to | |
| `cacheControlMaxAge` | Cache-Control max-age in seconds | `undefined` (Cache-Control header omitted) |



## Azure Blob Storage
[`@payloadcms/storage-azure`](https://www.npmjs.com/package/@payloadcms/storage-azure)

Expand Down
23 changes: 20 additions & 3 deletions packages/storage-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ export type S3StorageOptions = {
*/

acl?: 'private' | 'public-read'

/**
* Bucket name to upload files to.
*
* Must follow [AWS S3 bucket naming conventions](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
*/

bucket: string

/**
* Cache-Control max-age in seconds
*
* @defaultvalue undefined
*/
cacheControlMaxAge?: number

/**
* Collection options to apply the S3 adapter to.
*/
Expand Down Expand Up @@ -102,7 +109,12 @@ export const s3Storage: S3StoragePlugin =
})(config)
}

function s3StorageInternal({ acl, bucket, config = {} }: S3StorageOptions): Adapter {
function s3StorageInternal({
acl,
bucket,
cacheControlMaxAge,
config = {},
}: S3StorageOptions): Adapter {
return ({ collection, prefix }): GeneratedAdapter => {
let storageClient: AWS.S3 | null = null
const getStorageClient: () => AWS.S3 = () => {
Expand All @@ -124,7 +136,12 @@ function s3StorageInternal({ acl, bucket, config = {} }: S3StorageOptions): Adap
getStorageClient,
prefix,
}),
staticHandler: getHandler({ bucket, collection, getStorageClient }),
staticHandler: getHandler({
bucket,
cacheControlMaxAge,
collection,
getStorageClient,
}),
}
}
}
14 changes: 13 additions & 1 deletion packages/storage-s3/src/staticHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'path'

interface Args {
bucket: string
cacheControlMaxAge?: number
collection: CollectionConfig
getStorageClient: () => AWS.S3
}
Expand All @@ -21,7 +22,12 @@ const streamToBuffer = async (readableStream: any) => {
return Buffer.concat(chunks)
}

export const getHandler = ({ bucket, collection, getStorageClient }: Args): StaticHandler => {
export const getHandler = ({
bucket,
cacheControlMaxAge,
collection,
getStorageClient,
}: Args): StaticHandler => {
return async (req, { params: { filename } }) => {
try {
const prefix = await getFilePrefix({ collection, filename, req })
Expand All @@ -42,6 +48,9 @@ export const getHandler = ({ bucket, collection, getStorageClient }: Args): Stat
return new Response(null, {
headers: new Headers({
'Accept-Ranges': String(object.AcceptRanges),
...(cacheControlMaxAge
? { 'Cache-Control': `public, max-age=${cacheControlMaxAge}` }
Comment on lines 50 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can replace cacheControlMaxAge with something like responseHeaders that also can be a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. I think it should be possible.

I tried to keep it consistent with the Vercel blob adapter for this PR.

: {}),
'Content-Length': String(object.ContentLength),
'Content-Type': String(object.ContentType),
ETag: String(object.ETag),
Expand All @@ -55,6 +64,9 @@ export const getHandler = ({ bucket, collection, getStorageClient }: Args): Stat
return new Response(bodyBuffer, {
headers: new Headers({
'Accept-Ranges': String(object.AcceptRanges),
...(cacheControlMaxAge
? { 'Cache-Control': `public, max-age=${cacheControlMaxAge}` }
: {}),
'Content-Length': String(object.ContentLength),
'Content-Type': String(object.ContentType),
ETag: String(object.ETag),
Expand Down