Skip to content

Commit

Permalink
Merge pull request #239 from getlift/validate-assets-path
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored Jul 9, 2022
2 parents 9d2a079 + e4ffecc commit 857c588
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/constructs/aws/ServerSideWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ export class ServerSideWebsite extends AwsConstruct {
private createCacheBehaviors(bucket: Bucket): Record<string, BehaviorOptions> {
const behaviors: Record<string, BehaviorOptions> = {};
for (const pattern of Object.keys(this.getAssetPatterns())) {
if (pattern === "/" || pattern === "/*") {
throw new ServerlessError(
`Invalid key in 'constructs.${this.id}.assets': '/' and '/*' cannot be routed to assets because the root URL already serves the backend application running in Lambda. You must use a sub-path instead, for example '/assets/*'.`,
"LIFT_INVALID_CONSTRUCT_CONFIGURATION"
);
}
behaviors[pattern] = {
// Origins are where CloudFront fetches content
origin: new S3Origin(bucket),
Expand Down
20 changes: 20 additions & 0 deletions test/unit/serverSideWebsite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,26 @@ describe("server-side website", () => {
);
});

it("should validate the assets configuration", async () => {
await expect(() => {
return runServerless({
command: "package",
config: Object.assign(baseConfig, {
constructs: {
backend: {
type: "server-side-website",
assets: {
"/": "public",
},
},
},
}),
});
}).rejects.toThrowError(
"Invalid key in 'constructs.backend.assets': '/' and '/*' cannot be routed to assets because the root URL already serves the backend application running in Lambda. You must use a sub-path instead, for example '/assets/*'."
);
});

it("should allow to redirect to the main domain", async () => {
const { cfTemplate, computeLogicalId } = await runServerless({
command: "package",
Expand Down

0 comments on commit 857c588

Please sign in to comment.