@@ -4,7 +4,7 @@ import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'
4
4
import * as s3 from 'aws-cdk-lib/aws-s3'
5
5
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'
6
6
import { addOutput } from '../../common/cdk'
7
- import { CacheConfig } from '../../types'
7
+ import { DeployConfig } from '../../types'
8
8
import { HEADER_DEVICE_TYPE } from '../../constants'
9
9
10
10
interface CloudFrontPropsDistribution {
@@ -13,7 +13,7 @@ interface CloudFrontPropsDistribution {
13
13
requestEdgeFunction : cloudfront . experimental . EdgeFunction
14
14
viewerResponseEdgeFunction : cloudfront . experimental . EdgeFunction
15
15
viewerRequestLambdaEdge : cloudfront . experimental . EdgeFunction
16
- cacheConfig : CacheConfig
16
+ deployConfig : DeployConfig
17
17
imageTTL ?: number
18
18
}
19
19
@@ -35,25 +35,27 @@ export class CloudFrontDistribution extends Construct {
35
35
requestEdgeFunction,
36
36
viewerResponseEdgeFunction,
37
37
viewerRequestLambdaEdge,
38
- cacheConfig ,
38
+ deployConfig ,
39
39
renderServerDomain,
40
40
imageTTL
41
41
} = props
42
42
43
43
const splitCachePolicy = new cloudfront . CachePolicy ( this , 'SplitCachePolicy' , {
44
44
cachePolicyName : `${ id } -SplitCachePolicy` ,
45
45
queryStringBehavior : cloudfront . CacheQueryStringBehavior . allowList (
46
- ...defaultNextQueries . concat ( cacheConfig . cacheQueries ?? [ ] )
46
+ ...defaultNextQueries . concat ( deployConfig . cache . cacheQueries ?? [ ] )
47
47
) ,
48
- cookieBehavior : cacheConfig . cacheCookies ?. length
49
- ? cloudfront . CacheCookieBehavior . allowList ( ...cacheConfig . cacheCookies )
48
+ cookieBehavior : deployConfig . cache . cacheCookies ?. length
49
+ ? cloudfront . CacheCookieBehavior . allowList ( ...deployConfig . cache . cacheCookies )
50
50
: cloudfront . CacheCookieBehavior . none ( ) ,
51
51
headerBehavior : cloudfront . CacheHeaderBehavior . allowList (
52
52
...defaultNextHeaders ,
53
53
...Object . values ( HEADER_DEVICE_TYPE )
54
54
) ,
55
55
minTtl : NoCache ,
56
- defaultTtl : NoCache // no caching by default, cache value is going to be used from Cache-Control header.
56
+ defaultTtl : NoCache , // no caching by default, cache value is going to be used from Cache-Control header.
57
+ enableAcceptEncodingBrotli : true ,
58
+ enableAcceptEncodingGzip : true
57
59
} )
58
60
59
61
const longCachePolicy = new cloudfront . CachePolicy ( this , 'LongCachePolicy' , {
@@ -63,7 +65,9 @@ export class CloudFrontDistribution extends Construct {
63
65
headerBehavior : cloudfront . CacheHeaderBehavior . none ( ) ,
64
66
defaultTtl : OneMonthCache ,
65
67
maxTtl : OneMonthCache ,
66
- minTtl : OneMonthCache
68
+ minTtl : OneMonthCache ,
69
+ enableAcceptEncodingBrotli : true ,
70
+ enableAcceptEncodingGzip : true
67
71
} )
68
72
69
73
const imageTTLValue = imageTTL ? Duration . seconds ( imageTTL ) : OneDayCache
@@ -75,10 +79,24 @@ export class CloudFrontDistribution extends Construct {
75
79
headerBehavior : cloudfront . CacheHeaderBehavior . allowList ( ...defaultNextHeaders ) ,
76
80
defaultTtl : imageTTLValue ,
77
81
maxTtl : imageTTLValue ,
78
- minTtl : imageTTLValue
82
+ minTtl : imageTTLValue ,
83
+ enableAcceptEncodingBrotli : true ,
84
+ enableAcceptEncodingGzip : true
85
+ } )
86
+
87
+ const publicAssetsCachePolicy = new cloudfront . CachePolicy ( this , 'PublicAssetsCachePolicy' , {
88
+ cachePolicyName : `${ id } -PublicAssetsCachePolicy` ,
89
+ defaultTtl : deployConfig . publicAssets ?. ttl ? Duration . seconds ( deployConfig . publicAssets . ttl ) : NoCache ,
90
+ maxTtl : deployConfig . publicAssets ?. ttl ? Duration . seconds ( deployConfig . publicAssets . ttl ) : NoCache ,
91
+ minTtl : deployConfig . publicAssets ?. ttl ? Duration . seconds ( deployConfig . publicAssets . ttl ) : NoCache ,
92
+ enableAcceptEncodingBrotli : true ,
93
+ enableAcceptEncodingGzip : true
79
94
} )
80
95
81
96
const s3Origin = new origins . S3Origin ( staticBucket )
97
+ const publicFolderS3Origin = new origins . S3Origin ( staticBucket , {
98
+ originPath : '/public'
99
+ } )
82
100
const nextServerOrigin = new origins . HttpOrigin ( renderServerDomain , {
83
101
protocolPolicy : cloudfront . OriginProtocolPolicy . HTTP_ONLY ,
84
102
httpPort : 80
@@ -101,7 +119,8 @@ export class CloudFrontDistribution extends Construct {
101
119
eventType : cloudfront . LambdaEdgeEventType . VIEWER_REQUEST
102
120
}
103
121
] ,
104
- cachePolicy : splitCachePolicy
122
+ cachePolicy : splitCachePolicy ,
123
+ compress : true
105
124
} ,
106
125
defaultRootObject : '' ,
107
126
additionalBehaviors : {
@@ -113,16 +132,26 @@ export class CloudFrontDistribution extends Construct {
113
132
eventType : cloudfront . LambdaEdgeEventType . ORIGIN_REQUEST
114
133
}
115
134
] ,
116
- cachePolicy : splitCachePolicy
135
+ cachePolicy : splitCachePolicy ,
136
+ compress : true
117
137
} ,
118
138
'/_next/image*' : {
119
139
origin : nextServerOrigin ,
120
140
cachePolicy : imageCachePolicy
121
141
} ,
122
142
'/_next/*' : {
123
143
origin : s3Origin ,
124
- cachePolicy : longCachePolicy
125
- }
144
+ cachePolicy : longCachePolicy ,
145
+ compress : true
146
+ } ,
147
+ ...( deployConfig . publicAssets
148
+ ? {
149
+ [ `${ deployConfig . publicAssets . prefix } /*` ] : {
150
+ origin : publicFolderS3Origin ,
151
+ cachePolicy : publicAssetsCachePolicy
152
+ }
153
+ }
154
+ : { } )
126
155
}
127
156
} )
128
157
0 commit comments