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

Commit

Permalink
fix(lambda-at-edge): serve HTML pages with no props (i.e static pages…
Browse files Browse the repository at this point in the history
…) properly on preview mode enabled (#701)
  • Loading branch information
dphang authored Oct 21, 2020
1 parent 06d4edb commit 57cd668
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ const handleOriginRequest = async ({
}
}

const isStaticPage = pages.html.nonDynamic[uri];
const isStaticPage = pages.html.nonDynamic[uri]; // plain page without any props
const isPrerenderedPage = prerenderManifest.routes[uri]; // prerendered pages are also static pages like "pages.html" above, but are defined in the prerender-manifest
const origin = request.origin as CloudFrontOrigin;
const s3Origin = origin.s3 as CloudFrontS3Origin;
Expand Down Expand Up @@ -328,7 +328,9 @@ const handleOriginRequest = async ({
s3Origin.domainName = normalisedS3DomainName;

S3Check: if (
// Note: public files and static pages (HTML pages with no props) don't have JS files needed for preview mode, always serve from S3.
isPublicFile ||
isStaticPage ||
(isHTMLPage && !isPreviewRequest) ||
(hasFallback && !isPreviewRequest) ||
(isDataReq && !isPreviewRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,39 @@ describe("Lambda@Edge", () => {
expect(decodedBody).toBe("pages/preview.js");
expect(response.status).toBe(200);
});

it("HTML page without any props served from S3 on preview mode", async () => {
const event = createCloudFrontEvent({
uri: `/terms${trailingSlash ? "/" : ""}`,
host: "mydistribution.cloudfront.net",
requestHeaders: {
cookie: [
{
key: "Cookie",
value: "__next_preview_data=abc; __prerender_bypass=def"
}
]
}
});

const result = await handler(event);

const request = result as CloudFrontRequest;

expect(request.origin).toEqual({
s3: {
authMethod: "origin-access-identity",
domainName: "my-bucket.s3.amazonaws.com",
path: "/static-pages",
region: "us-east-1"
}
});
expect(request.uri).toEqual("/terms.html");
expect(request.headers.host[0].key).toEqual("host");
expect(request.headers.host[0].value).toEqual(
"my-bucket.s3.amazonaws.com"
);
});
});

describe("Public files routing", () => {
Expand Down

0 comments on commit 57cd668

Please sign in to comment.