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

Commit 69beb5a

Browse files
author
Gehan Gonsalkorale
authored
feat(serverless-component): custom cloudfront.origins input (#376)
1 parent dd4648c commit 69beb5a

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-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

+25-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,29 @@ 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 =
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+
156179
const cloudFrontOrigins = [
157180
{
158181
url: bucketUrl,
@@ -165,7 +188,8 @@ class NextjsComponent extends Component {
165188
ttl: 86400
166189
}
167190
}
168-
}
191+
},
192+
...inputOrigins
169193
];
170194

171195
let apiEdgeLambdaOutputs;

0 commit comments

Comments
 (0)