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

Pass through cloudfront.origins inputs and expand relative paths #376

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/serverless-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ myNextApplication:
headers: [CloudFront-Is-Desktop-Viewer, CloudFront-Is-Mobile-Viewer, CloudFront-Is-Tablet-Viewer]
api: # options for lambdas that handle API request
ttl: 10
origins: # options for custom origins and behaviors
- url: /static
pathPatterns:
/wp-content/*:
ttl: 10
- url: https://old-static.com
pathPatterns:
/old-static/*:
ttl: 10
```

The example above adds headers that can be forwarded to the SSR lambda, and sets the *ttl* for api lambdas.
Expand Down
22 changes: 21 additions & 1 deletion packages/serverless-component/__tests__/custom-inputs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,30 @@ describe("Custom inputs", () => {
[
{ api: { ttl: 500, "lambda@edge": "ignored value" } },
{ api: { ttl: 500 } } // expecting lambda@edge value to be ignored
],
[
{
origins: [
"http://some-origin",
"/relative",
{ url: "http://diff-origin" },
{ url: "/diff-relative" }
]
},
{
origins: [
"http://some-origin",
"http://bucket-xyz.s3.amazonaws.com/relative",
{ url: "http://diff-origin" },
{ url: "http://bucket-xyz.s3.amazonaws.com/diff-relative" }
]
}
]
])("Custom cloudfront inputs", (inputCloudfrontConfig, expectedInConfig) => {
const fixturePath = path.join(__dirname, "./fixtures/generic-fixture");
const defaultCloudfrontInputs = expectedInConfig.defaults || {};
const apiCloudfrontInputs = expectedInConfig.api || {};
const originCloudfrontInputs = expectedInConfig.origins || [];
const cloudfrontConfig = {
defaults: {
ttl: 0,
Expand Down Expand Up @@ -301,7 +320,8 @@ describe("Custom inputs", () => {
},
private: true,
url: "http://bucket-xyz.s3.amazonaws.com"
}
},
...originCloudfrontInputs
]
};

Expand Down
26 changes: 25 additions & 1 deletion packages/serverless-component/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ class NextjsComponent extends Component {
};

const bucketUrl = `http://${bucketOutputs.name}.s3.amazonaws.com`;

// If origin is relative path then prepend the bucketUrl
// e.g. /path => http://bucket.s3.aws.com/path
const expandRelativeUrls = origin => {
const originUrl = typeof origin === "string" ? origin : origin.url;
const fullOriginUrl =
originUrl.charAt(0) === "/" ? `${bucketUrl}${originUrl}` : originUrl;

if (typeof origin === "string") {
return fullOriginUrl;
} else {
return {
...origin,
url: fullOriginUrl
};
}
};
// Parse origins from inputs
const inputOrigins = (
(inputs.cloudfront && inputs.cloudfront.origins) ||
[]
).map(expandRelativeUrls);

const cloudFrontOrigins = [
{
url: bucketUrl,
Expand All @@ -165,7 +188,8 @@ class NextjsComponent extends Component {
ttl: 86400
}
}
}
},
...inputOrigins
];

let apiEdgeLambdaOutputs;
Expand Down