@@ -303,20 +303,42 @@ export class CdnStack {
303
303
runtime : lambda . Runtime . NODEJS_20_X ,
304
304
handler : 'index.handler' ,
305
305
code : lambda . Code . fromInline ( `
306
- exports.handler = (event, context, callback) => {
307
- const request = event.Records[0].cf.request;
308
- const uri = request.uri;
309
-
310
- if (uri.startsWith('/docs')) {
311
- request.uri = uri.replace('/docs', '') || '/';
312
- if (request.uri.endsWith('/')) {
313
- request.uri += 'index.html';
314
- }
306
+ const config = {
307
+ suffix: '.html',
308
+ appendToDirs: 'index.html',
315
309
}
316
310
317
- callback(null, request);
318
- };
319
- ` ) ,
311
+ const regexSuffixless = /\\/[^/.]+$/
312
+ const regexTrailingSlash = /.+\\/$/
313
+
314
+ exports.handler = (event, context, callback) => {
315
+ const request = event.Records[0].cf.request;
316
+ let uri = request.uri;
317
+
318
+ // Remove /docs prefix
319
+ if (uri.startsWith('/docs')) {
320
+ uri = uri.replace('/docs', '') || '/';
321
+ }
322
+
323
+ // Append ".html" to origin request
324
+ if (config.suffix && uri.match(regexSuffixless)) {
325
+ uri = uri + config.suffix;
326
+ }
327
+ // Append "index.html" to origin request
328
+ else if (config.appendToDirs && uri.match(regexTrailingSlash)) {
329
+ uri = uri + config.appendToDirs;
330
+ }
331
+
332
+ request.uri = uri;
333
+ callback(null, request);
334
+ };
335
+ ` ) ,
336
+ } )
337
+
338
+ new lambda . CfnPermission ( scope , 'DocsOriginRequestFunctionPermission' , {
339
+ action : 'lambda:InvokeFunction' ,
340
+ principal : 'edgelambda.amazonaws.com' ,
341
+ functionName : docsOriginRequestFunction . functionName ,
320
342
} )
321
343
322
344
const commonBehavior : cloudfront . BehaviorOptions = {
0 commit comments