Skip to content

Commit 227a760

Browse files
committed
chore: wip
1 parent 888a2d4 commit 227a760

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

storage/framework/core/cloud/src/cloud/cdn.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,42 @@ export class CdnStack {
303303
runtime: lambda.Runtime.NODEJS_20_X,
304304
handler: 'index.handler',
305305
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',
315309
}
316310
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,
320342
})
321343

322344
const commonBehavior: cloudfront.BehaviorOptions = {

storage/framework/ide/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Doyon
9494
dprint
9595
Drasner
9696
ecommerce
97+
edgelambda
9798
elasticloadbalancingv
9899
elbv
99100
encrypter

0 commit comments

Comments
 (0)