Skip to content

Commit

Permalink
fix(lambda-at-edge): fix for 404s on public files (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
dphang authored Aug 31, 2020
1 parent f580786 commit a854139
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,16 @@ const handleOriginResponse = async ({
}) => {
const response = event.Records[0].cf.response;
const request = event.Records[0].cf.request;
const uri = normaliseUri(request.uri);
const { status } = response;
if (status !== "403") {
const pagePath = router(manifest)(uri);
if (pagePath === "pages/404.html") {
// Set 404 status code for 404.html page. We do not need normalised URI as it will always be "/404.html"
if (request.uri === "/404.html") {
response.status = "404";
response.statusDescription = "Not Found";
}
return response;
}
const uri = normaliseUri(request.uri);
const { domainName, region } = request.origin!.s3!;
const bucketName = domainName.replace(`.s3.${region}.amazonaws.com`, "");
// It's usually better to do this outside the handler, but we need to know the bucket region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("Lambda@Edge", () => {

it("static 404 page should return CloudFront 404 status code after successful S3 origin response", async () => {
const event = createCloudFrontEvent({
uri: "/page/does/not/exist",
uri: "/404.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ describe("Lambda@Edge", () => {
await runRedirectTest(path, expectedRedirect);
}
);

it("terms.html should return 200 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/terms.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("200");
});
});

describe("Public files routing", () => {
Expand All @@ -194,6 +209,21 @@ describe("Lambda@Edge", () => {
expect(request.uri).toEqual("/manifest.json");
});

it("public file should return 200 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/manifest.json",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("200");
});

it.each`
path | expectedRedirect
${"/basepath/favicon.ico/"} | ${"/basepath/favicon.ico"}
Expand Down Expand Up @@ -511,6 +541,21 @@ describe("Lambda@Edge", () => {
expect(response.status).toEqual("404");
}
);

it("404.html should return 404 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/404.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("404");
});
});

describe("500 page", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ describe("Lambda@Edge", () => {
await runRedirectTest(path, expectedRedirect);
}
);

it("terms.html should return 200 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/terms.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("200");
});
});

describe("Public files routing", () => {
Expand All @@ -195,6 +210,21 @@ describe("Lambda@Edge", () => {
expect(request.uri).toEqual("/manifest.json");
});

it("public file should return 200 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/manifest.json",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("200");
});

it.each`
path | expectedRedirect
${"/favicon.ico/"} | ${"/favicon.ico"}
Expand Down Expand Up @@ -475,6 +505,21 @@ describe("Lambda@Edge", () => {
expect(response.status).toEqual("404");
}
);

it("404.html should return 404 status after successful S3 Origin response", async () => {
const event = createCloudFrontEvent({
uri: "/404.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
status: "200"
} as any
});

const response = (await handler(event)) as CloudFrontResultResponse;

expect(response.status).toEqual("404");
});
});

describe("500 page", () => {
Expand Down

0 comments on commit a854139

Please sign in to comment.