This repository was archived by the owner on Jan 28, 2025. It is now read-only.
File tree 3 files changed +55
-2
lines changed
packages/serverless-component
3 files changed +55
-2
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,15 @@ myNextApplication:
111
111
headers: [CloudFront-Is-Desktop-Viewer, CloudFront-Is-Mobile-Viewer, CloudFront-Is-Tablet-Viewer]
112
112
api: # options for lambdas that handle API request
113
113
ttl: 10
114
+ origins: # options for custom origins and behaviors
115
+ - url: /static
116
+ pathPatterns:
117
+ /wp-content/*:
118
+ ttl: 10
119
+ - url: https://old-static.com
120
+ pathPatterns:
121
+ /old-static/*:
122
+ ttl: 10
114
123
` ` `
115
124
116
125
The example above adds headers that can be forwarded to the SSR lambda, and sets the *ttl* for api lambdas.
Original file line number Diff line number Diff line change @@ -256,11 +256,30 @@ describe("Custom inputs", () => {
256
256
[
257
257
{ api : { ttl : 500 , "lambda@edge" : "ignored value" } } ,
258
258
{ api : { ttl : 500 } } // expecting lambda@edge value to be ignored
259
+ ] ,
260
+ [
261
+ {
262
+ origins : [
263
+ "http://some-origin" ,
264
+ "/relative" ,
265
+ { url : "http://diff-origin" } ,
266
+ { url : "/diff-relative" }
267
+ ]
268
+ } ,
269
+ {
270
+ origins : [
271
+ "http://some-origin" ,
272
+ "http://bucket-xyz.s3.amazonaws.com/relative" ,
273
+ { url : "http://diff-origin" } ,
274
+ { url : "http://bucket-xyz.s3.amazonaws.com/diff-relative" }
275
+ ]
276
+ }
259
277
]
260
278
] ) ( "Custom cloudfront inputs" , ( inputCloudfrontConfig , expectedInConfig ) => {
261
279
const fixturePath = path . join ( __dirname , "./fixtures/generic-fixture" ) ;
262
280
const defaultCloudfrontInputs = expectedInConfig . defaults || { } ;
263
281
const apiCloudfrontInputs = expectedInConfig . api || { } ;
282
+ const originCloudfrontInputs = expectedInConfig . origins || [ ] ;
264
283
const cloudfrontConfig = {
265
284
defaults : {
266
285
ttl : 0 ,
@@ -301,7 +320,8 @@ describe("Custom inputs", () => {
301
320
} ,
302
321
private : true ,
303
322
url : "http://bucket-xyz.s3.amazonaws.com"
304
- }
323
+ } ,
324
+ ...originCloudfrontInputs
305
325
]
306
326
} ;
307
327
Original file line number Diff line number Diff line change @@ -153,6 +153,29 @@ class NextjsComponent extends Component {
153
153
} ;
154
154
155
155
const bucketUrl = `http://${ bucketOutputs . name } .s3.amazonaws.com` ;
156
+
157
+ // If origin is relative path then prepend the bucketUrl
158
+ // e.g. /path => http://bucket.s3.aws.com/path
159
+ const expandRelativeUrls = origin => {
160
+ const originUrl = typeof origin === "string" ? origin : origin . url ;
161
+ const fullOriginUrl =
162
+ originUrl . charAt ( 0 ) === "/" ? `${ bucketUrl } ${ originUrl } ` : originUrl ;
163
+
164
+ if ( typeof origin === "string" ) {
165
+ return fullOriginUrl ;
166
+ } else {
167
+ return {
168
+ ...origin ,
169
+ url : fullOriginUrl
170
+ } ;
171
+ }
172
+ } ;
173
+ // Parse origins from inputs
174
+ const inputOrigins = (
175
+ ( inputs . cloudfront && inputs . cloudfront . origins ) ||
176
+ [ ]
177
+ ) . map ( expandRelativeUrls ) ;
178
+
156
179
const cloudFrontOrigins = [
157
180
{
158
181
url : bucketUrl ,
@@ -165,7 +188,8 @@ class NextjsComponent extends Component {
165
188
ttl : 86400
166
189
}
167
190
}
168
- }
191
+ } ,
192
+ ...inputOrigins
169
193
] ;
170
194
171
195
let apiEdgeLambdaOutputs ;
You can’t perform that action at this time.
0 commit comments