Skip to content

Commit 55feb54

Browse files
committed
cloudfront implementation
1 parent 81194cf commit 55feb54

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
CloudFrontClient,
3+
CreateInvalidationCommand,
4+
} from "@aws-sdk/client-cloudfront";
5+
import type { CDNInvalidationHandler } from "types/overrides";
6+
7+
const cloudfront = new CloudFrontClient({});
8+
export default {
9+
name: "cloudfront",
10+
invalidatePaths: async (paths) => {
11+
//TODO: test the constructed paths
12+
const constructedPaths = paths.flatMap(({ path, isAppRouter }) =>
13+
isAppRouter
14+
? [`${path}`, `${path}?_rsc=*`]
15+
: [
16+
`${path}`,
17+
`_next/data/${process.env.BUILD_ID}${path === "/" ? "/index" : path}.json*`,
18+
],
19+
);
20+
await cloudfront.send(
21+
new CreateInvalidationCommand({
22+
DistributionId: process.env.CLOUDFRONT_DISTRIBUTION_ID!,
23+
InvalidationBatch: {
24+
// Do we need to limit the number of paths? Or batch them into multiple commands?
25+
Paths: { Quantity: constructedPaths.length, Items: constructedPaths },
26+
CallerReference: `${Date.now()}`,
27+
},
28+
}),
29+
);
30+
},
31+
} satisfies CDNInvalidationHandler;

0 commit comments

Comments
 (0)