Skip to content

Commit 21fc1d1

Browse files
authored
feat(s3-deployment): add some convenient methods to CacheControl (#25477)
This PR adds following convenient methods to `CacheControl`. | method | directive | RFC | |-|-|-| | `CacheControl.noStore()` | `no-store` | [RFC9111](https://www.rfc-editor.org/rfc/rfc9111.html), Section 5.2.2.4 | | `CacheControl.mustUnderstand()` | `must-understand` | RFC9111, Section 5.2.2.3 | | `CacheControl.immutable()` | `immutable` | [RFC8246](https://www.rfc-editor.org/rfc/rfc8246.html) | | `CacheControl.staleWhileRevalidate(duration)` | `stale-while-revalidate=<duration>` | [RFC5861](https://www.rfc-editor.org/rfc/rfc5861.html) | | `CacheControl.staleIfError(duration)` | `stale-if-error=<duration>` | RFC5861 | For more information about these Cache-Control directives, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0f40880 commit 21fc1d1

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/aws-cdk-lib/aws-s3-deployment/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,19 @@ declare const destinationBucket: s3.Bucket;
157157
new s3deploy.BucketDeployment(this, 'BucketDeployment', {
158158
sources: [s3deploy.Source.asset('./website', { exclude: ['index.html'] })],
159159
destinationBucket,
160-
cacheControl: [s3deploy.CacheControl.fromString('max-age=31536000,public,immutable')],
160+
cacheControl: [
161+
s3deploy.CacheControl.maxAge(Duration.days(365)),
162+
s3deploy.CacheControl.immutable(),
163+
],
161164
prune: false,
162165
});
163166

164167
new s3deploy.BucketDeployment(this, 'HTMLBucketDeployment', {
165168
sources: [s3deploy.Source.asset('./website', { exclude: ['*', '!index.html'] })],
166169
destinationBucket,
167-
cacheControl: [s3deploy.CacheControl.fromString('max-age=0,no-cache,no-store,must-revalidate')],
170+
cacheControl: [
171+
s3deploy.CacheControl.maxAge(Duration.seconds(0)),
172+
],
168173
prune: false,
169174
});
170175
```

packages/aws-cdk-lib/aws-s3-deployment/lib/bucket-deployment.ts

+25
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,16 @@ export class CacheControl {
631631
*/
632632
public static noTransform() { return new CacheControl('no-transform'); }
633633

634+
/**
635+
* Sets 'no-store'.
636+
*/
637+
public static noStore() { return new CacheControl('no-store'); }
638+
639+
/**
640+
* Sets 'must-understand'.
641+
*/
642+
public static mustUnderstand() { return new CacheControl('must-understand'); }
643+
634644
/**
635645
* Sets 'public'.
636646
*/
@@ -641,6 +651,11 @@ export class CacheControl {
641651
*/
642652
public static setPrivate() { return new CacheControl('private'); }
643653

654+
/**
655+
* Sets 'immutable'.
656+
*/
657+
public static immutable() { return new CacheControl('immutable'); }
658+
644659
/**
645660
* Sets 'proxy-revalidate'.
646661
*/
@@ -656,6 +671,16 @@ export class CacheControl {
656671
*/
657672
public static sMaxAge(t: cdk.Duration) { return new CacheControl(`s-maxage=${t.toSeconds()}`); }
658673

674+
/**
675+
* Sets 'stale-while-revalidate=<duration-in-seconds>'.
676+
*/
677+
public static staleWhileRevalidate(t: cdk.Duration) { return new CacheControl(`stale-while-revalidate=${t.toSeconds()}`); }
678+
679+
/**
680+
* Sets 'stale-if-error=<duration-in-seconds>'.
681+
*/
682+
public static staleIfError(t: cdk.Duration) { return new CacheControl(`stale-if-error=${t.toSeconds()}`); }
683+
659684
/**
660685
* Constructs a custom cache control key from the literal value.
661686
*/

packages/aws-cdk-lib/aws-s3-deployment/test/bucket-deployment.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,16 @@ test('cache control type has correct values', () => {
514514
expect(s3deploy.CacheControl.mustRevalidate().value).toEqual('must-revalidate');
515515
expect(s3deploy.CacheControl.noCache().value).toEqual('no-cache');
516516
expect(s3deploy.CacheControl.noTransform().value).toEqual('no-transform');
517+
expect(s3deploy.CacheControl.noStore().value).toEqual('no-store');
518+
expect(s3deploy.CacheControl.mustUnderstand().value).toEqual('must-understand');
517519
expect(s3deploy.CacheControl.setPublic().value).toEqual('public');
518520
expect(s3deploy.CacheControl.setPrivate().value).toEqual('private');
521+
expect(s3deploy.CacheControl.immutable().value).toEqual('immutable');
519522
expect(s3deploy.CacheControl.proxyRevalidate().value).toEqual('proxy-revalidate');
520523
expect(s3deploy.CacheControl.maxAge(cdk.Duration.minutes(1)).value).toEqual('max-age=60');
521524
expect(s3deploy.CacheControl.sMaxAge(cdk.Duration.minutes(1)).value).toEqual('s-maxage=60');
525+
expect(s3deploy.CacheControl.staleWhileRevalidate(cdk.Duration.minutes(1)).value).toEqual('stale-while-revalidate=60');
526+
expect(s3deploy.CacheControl.staleIfError(cdk.Duration.minutes(1)).value).toEqual('stale-if-error=60');
522527
expect(s3deploy.CacheControl.fromString('only-if-cached').value).toEqual('only-if-cached');
523528
});
524529

0 commit comments

Comments
 (0)