From a549072a89a65e440e78d316824c162551521f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kj=C3=A6r?= Date: Mon, 16 Oct 2023 01:29:07 +0200 Subject: [PATCH] Fix edge-function --- posts/2023-10-16-the-stack-part-3.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/posts/2023-10-16-the-stack-part-3.md b/posts/2023-10-16-the-stack-part-3.md index f1cb84c..85b8ac1 100644 --- a/posts/2023-10-16-the-stack-part-3.md +++ b/posts/2023-10-16-the-stack-part-3.md @@ -1601,7 +1601,7 @@ export class Stack extends cdk.Stack { ? [ { functionVersion: rewriteUrl.currentVersion, - eventType: cloudfront.LambdaEdgeEventType.VIEWER_REQUEST, + eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST, }, ] : undefined, @@ -1661,20 +1661,19 @@ export class Stack extends cdk.Stack { And our Lambda@Edge function to rewrite urls is defined in `edge-functions/rewrite-urls.ts`: ```typescript -function handler(event) { - var request = event.request; - var uri = request.uri; +exports.handler = (event, _, callback) => { + let request = event.Records[0].cf.request; // Check whether the URI is missing a file name. - if (uri.endsWith("/")) { + if (request.uri.endsWith("/")) { request.uri += "index.html"; - } else if (!uri.includes(".")) { + } else if (!request.uri.includes(".")) { // Check whether the URI is missing a file extension. request.uri += "/index.html"; } - return request; -} + return callback(null, request); +}; ```