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

fix(lambda-at-edge): serve HTML pages with no props (i.e static pages) properly on preview mode enabled #701

Merged
merged 2 commits into from
Oct 21, 2020
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
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