This repository was archived by the owner on Jan 28, 2025. It is now read-only.
File tree 3 files changed +54
-2
lines changed
packages/serverless-component
3 files changed +54
-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,28 @@ 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 = originUrl . charAt ( 0 ) === '/'
162
+ ? `${ bucketUrl } ${ originUrl } `
163
+ : originUrl
164
+
165
+ if ( typeof origin === 'string' ) {
166
+ return fullOriginUrl
167
+ } else {
168
+ return {
169
+ ...origin ,
170
+ url : fullOriginUrl
171
+ }
172
+ }
173
+ }
174
+ // Parse origins from inputs
175
+ const inputOrigins = ( ( inputs . cloudfront && inputs . cloudfront . origins ) || [ ] )
176
+ . map ( expandRelativeUrls )
177
+
156
178
const cloudFrontOrigins = [
157
179
{
158
180
url : bucketUrl ,
@@ -165,7 +187,8 @@ class NextjsComponent extends Component {
165
187
ttl : 86400
166
188
}
167
189
}
168
- }
190
+ } ,
191
+ ...inputOrigins ,
169
192
] ;
170
193
171
194
let apiEdgeLambdaOutputs ;
You can’t perform that action at this time.
0 commit comments