Skip to content

Commit b824746

Browse files
committed
chore: wip
1 parent c18a136 commit b824746

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ export class CdnStack {
307307
code: lambda.Code.fromInline(`
308308
const config = {
309309
suffix: '.html',
310-
appendToDirs: 'index.html',
311310
}
312311
313312
const regexSuffixless = /\\/[^/.]+$/
@@ -317,18 +316,24 @@ export class CdnStack {
317316
const request = event.Records[0].cf.request;
318317
let uri = request.uri;
319318
320-
// Remove /docs prefix
321-
if (uri.startsWith('/docs')) {
322-
uri = uri.replace('/docs', '') || '/';
319+
if (uri === '/docs' || uri === '/docs/') {
320+
uri = '/index.html'
321+
callback(null, request)
322+
return
323323
}
324324
325325
// Append ".html" to origin request
326326
if (uri.match(regexSuffixless)) {
327-
uri = uri + config.suffix;
327+
request.uri = uri + suffix
328+
callback(null, request)
329+
return
328330
}
329-
// Append "index.html" to origin request
330-
else if (uri.match(regexTrailingSlash)) {
331-
uri = uri + config.appendToDirs;
331+
332+
// Remove trailing slash and append ".html" to origin request
333+
if (uri.match(regexTrailingSlash)) {
334+
request.uri = uri.slice(0, -1) + '.html'
335+
callback(null, request)
336+
return
332337
}
333338
334339
request.uri = uri;
@@ -343,6 +348,28 @@ export class CdnStack {
343348
functionName: docsOriginRequestFunction.functionName,
344349
})
345350

351+
// const docsOriginResponseFunction = new lambda.Function(scope, 'DocsOriginResponseFunction', {
352+
// functionName: `${this.props.slug}-${this.props.appEnv}-docs-origin-response-function`,
353+
// description: 'Custom origin response function for the docs',
354+
// runtime: lambda.Runtime.NODEJS_20_X,
355+
// handler: 'index.handler',
356+
// code: lambda.Code.fromInline(`
357+
// exports.handler = (event, context, callback) => {
358+
// const response = event.Records[0].cf.response;
359+
// response.headers['x-custom-header'] = {
360+
// value: 'custom-value',
361+
// };
362+
// callback(null, response);
363+
// };
364+
// `),
365+
// })
366+
367+
// new lambda.CfnPermission(scope, 'DocsOriginResponseFunctionPermission', {
368+
// action: 'lambda:InvokeFunction',
369+
// principal: 'edgelambda.amazonaws.com',
370+
// functionName: docsOriginResponseFunction.functionName,
371+
// })
372+
346373
const commonBehavior: cloudfront.BehaviorOptions = {
347374
origin,
348375
compress: true,
@@ -356,6 +383,10 @@ export class CdnStack {
356383
functionVersion: docsOriginRequestFunction.currentVersion,
357384
eventType: cloudfront.LambdaEdgeEventType.ORIGIN_REQUEST,
358385
},
386+
// {
387+
// functionVersion: docsOriginResponseFunction.currentVersion,
388+
// eventType: cloudfront.LambdaEdgeEventType.ORIGIN_RESPONSE,
389+
// },
359390
],
360391
// functionAssociations: [
361392
// {

storage/framework/core/cloud/src/edge/origin-request.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const config = {
22
suffix: '.html',
3-
appendToDirs: 'index.html',
43
removeTrailingSlash: false,
54
}
65

@@ -11,18 +10,24 @@ const regexTrailingSlash = /.+\/$/ // e.g. "/some/" or "/some/page/" but not roo
1110
export function handler(event: any, context: any, callback: any) {
1211
const { request } = event.Records[0].cf
1312
const { uri } = request
14-
const { suffix, appendToDirs } = config
13+
const { suffix } = config
14+
15+
if (uri === '/') {
16+
request.uri = '/index.html'
17+
callback(null, request)
18+
return
19+
}
1520

1621
// Append ".html" to origin request
17-
if (suffix && uri.match(regexSuffixless)) {
22+
if (uri.match(regexSuffixless)) {
1823
request.uri = uri + suffix
1924
callback(null, request)
2025
return
2126
}
2227

23-
// Append "index.html" to origin request
24-
if (appendToDirs && uri.match(regexTrailingSlash)) {
25-
request.uri = uri + appendToDirs
28+
// Remove trailing slash and append ".html" to origin request
29+
if (uri.match(regexTrailingSlash)) {
30+
request.uri = `${uri.slice(0, -1)}.html`
2631
callback(null, request)
2732
return
2833
}

0 commit comments

Comments
 (0)