Skip to content

Commit

Permalink
fix: cloudfront function regex and return request (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryasilva authored Mar 1, 2025
1 parent 2193608 commit 5a2e542
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class NextjsDistribution extends Construct {
}

private useCloudFrontFunctionHostHeader() {
return `event.request.headers["x-forwarded-host"] = event.request.headers.host;`;
return ` event.request.headers["x-forwarded-host"] = event.request.headers.host;`;
}

private useCloudFrontFunctionCacheHeaderKey() {
Expand Down Expand Up @@ -373,7 +373,7 @@ export class NextjsDistribution extends Construct {
? event.request.cookies["__prerender_bypass"].value
: "";
}
const crypto = require("crypto")
const crypto = require("crypto");
const hashedKey = crypto.createHash("md5").update(cacheKey).digest("hex");
event.request.headers["x-open-next-cache-key"] = { value: hashedKey };
`;
Expand All @@ -390,14 +390,15 @@ export class NextjsDistribution extends Construct {
async function handler(event) {
// INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER
// INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY
return event.request;
}
`;
code = code.replace(
/^\s*\/\/\s*INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER.*$/i,
/^\s*\/\/\s*INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER.*$/im,
this.useCloudFrontFunctionHostHeader()
);
code = code.replace(
/^\s*\/\/\s*INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY.*$/i,
/^\s*\/\/\s*INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY.*$/im,
this.useCloudFrontFunctionCacheHeaderKey()
);
const cloudFrontFn = new cloudfront.Function(this, 'CloudFrontFn', {
Expand Down

0 comments on commit 5a2e542

Please sign in to comment.