Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 36e4203

Browse files
committed
Pass through cloudfront.origins inputs and expand relative paths
1 parent dd4648c commit 36e4203

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

packages/serverless-component/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ myNextApplication:
111111
headers: [CloudFront-Is-Desktop-Viewer, CloudFront-Is-Mobile-Viewer, CloudFront-Is-Tablet-Viewer]
112112
api: # options for lambdas that handle API request
113113
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
114123
```
115124

116125
The example above adds headers that can be forwarded to the SSR lambda, and sets the *ttl* for api lambdas.

packages/serverless-component/__tests__/custom-inputs.test.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,30 @@ describe("Custom inputs", () => {
256256
[
257257
{ api: { ttl: 500, "lambda@edge": "ignored value" } },
258258
{ 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+
}
259277
]
260278
])("Custom cloudfront inputs", (inputCloudfrontConfig, expectedInConfig) => {
261279
const fixturePath = path.join(__dirname, "./fixtures/generic-fixture");
262280
const defaultCloudfrontInputs = expectedInConfig.defaults || {};
263281
const apiCloudfrontInputs = expectedInConfig.api || {};
282+
const originCloudfrontInputs = expectedInConfig.origins || [];
264283
const cloudfrontConfig = {
265284
defaults: {
266285
ttl: 0,
@@ -301,7 +320,8 @@ describe("Custom inputs", () => {
301320
},
302321
private: true,
303322
url: "http://bucket-xyz.s3.amazonaws.com"
304-
}
323+
},
324+
...originCloudfrontInputs
305325
]
306326
};
307327

packages/serverless-component/serverless.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@ class NextjsComponent extends Component {
153153
};
154154

155155
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+
156178
const cloudFrontOrigins = [
157179
{
158180
url: bucketUrl,
@@ -165,7 +187,8 @@ class NextjsComponent extends Component {
165187
ttl: 86400
166188
}
167189
}
168-
}
190+
},
191+
...inputOrigins,
169192
];
170193

171194
let apiEdgeLambdaOutputs;

0 commit comments

Comments
 (0)