Skip to content

Commit

Permalink
Upload dynamic SSG pages with revalidate-based expiry
Browse files Browse the repository at this point in the history
Again, not useful now, prerequisite for revalidation.
  • Loading branch information
jvarho committed Apr 28, 2021
1 parent bd988a4 commit 4423a47
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
12 changes: 10 additions & 2 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ const handleOriginResponse = async ({
"passthrough"
);
if (isSSG) {
const cacheControl = renderOpts.revalidate
? undefined
: "public, max-age=0, s-maxage=2678400, must-revalidate";
const expires = renderOpts.revalidate
? new Date(new Date().getTime() + 1000 * renderOpts.revalidate)
: undefined;
const baseKey = uri
.replace(/^\//, "")
.replace(/\.(json|html)$/, "")
Expand All @@ -666,14 +672,16 @@ const handleOriginResponse = async ({
Key: `${s3BasePath}${jsonKey}`,
Body: JSON.stringify(renderOpts.pageData),
ContentType: "application/json",
CacheControl: "public, max-age=0, s-maxage=2678400, must-revalidate"
CacheControl: cacheControl,
Expires: expires
};
const s3HtmlParams = {
Bucket: bucketName,
Key: `${s3BasePath}${htmlKey}`,
Body: html,
ContentType: "text/html",
CacheControl: "public, max-age=0, s-maxage=2678400, must-revalidate"
CacheControl: cacheControl,
Expires: expires
};
const { PutObjectCommand } = await import(
"@aws-sdk/client-s3/commands/PutObjectCommand"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,57 @@ describe("Lambda@Edge origin response", () => {
expect(decodedBody).toEqual("<div>Rendered Page</div>");
expect(cfResponse.status).toEqual(200);

expect(s3Client.send).toHaveBeenNthCalledWith(1, {
Command: "PutObjectCommand",
Bucket: "my-bucket.s3.amazonaws.com",
Key: "_next/data/build-id/fallback-blocking/not-yet-built.json",
Body: JSON.stringify({
page: "pages/fallback-blocking/[slug].js"
}),
ContentType: "application/json",
CacheControl: "public, max-age=0, s-maxage=2678400, must-revalidate"
});
expect(s3Client.send).toHaveBeenNthCalledWith(2, {
Command: "PutObjectCommand",
Bucket: "my-bucket.s3.amazonaws.com",
Key: "static-pages/build-id/fallback-blocking/not-yet-built.html",
Body: "<div>Rendered Page</div>",
ContentType: "text/html",
CacheControl: "public, max-age=0, s-maxage=2678400, must-revalidate"
expect(s3Client.send).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
Command: "PutObjectCommand",
Bucket: "my-bucket.s3.amazonaws.com",
Key: "_next/data/build-id/fallback-blocking/not-yet-built.json",
Body: JSON.stringify({
page: "pages/fallback-blocking/[slug].js"
}),
ContentType: "application/json"
})
);
expect(s3Client.send).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
Command: "PutObjectCommand",
Bucket: "my-bucket.s3.amazonaws.com",
Key: "static-pages/build-id/fallback-blocking/not-yet-built.html",
Body: "<div>Rendered Page</div>",
ContentType: "text/html"
})
);
});

it("uploads with revalidate-based expires", async () => {
const event = createCloudFrontEvent({
uri: "/fallback-blocking/not-yet-built.html",
host: "mydistribution.cloudfront.net",
config: { eventType: "origin-response" } as any,
response: {
headers: {},
status: "403"
} as any
});

mockPageRequire("pages/fallback-blocking/[slug].js");

await handler(event);

expect(
(s3Client.send as jest.Mock).mock.calls[0][0].Expires.getTime()
).toBeGreaterThan(new Date().getTime());
expect(
(s3Client.send as jest.Mock).mock.calls[0][0].Expires.getTime()
).toBeLessThan(new Date().getTime() + 300000);
expect(
(s3Client.send as jest.Mock).mock.calls[1][0].Expires.getTime()
).toBeGreaterThan(new Date().getTime());
expect(
(s3Client.send as jest.Mock).mock.calls[1][0].Expires.getTime()
).toBeLessThan(new Date().getTime() + 300000);
});

it("renders and uploads HTML and JSON for fallback SSG data requests", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
renderOpts: {
pageData: {
page: "pages/fallback-blocking/[slug].js"
}
},
revalidate: 300
}
});
}
Expand Down

0 comments on commit 4423a47

Please sign in to comment.