From 6b14c3068deb8fe2c125d8a0f8aed8330421b72f Mon Sep 17 00:00:00 2001 From: finn Date: Tue, 17 Sep 2024 13:27:30 +0930 Subject: [PATCH 1/6] Added options for behaviour prefixes au nz etc --- .../lib/prerender-lambda-construct.ts | 8 ++--- packages/static-hosting/index.ts | 8 +++-- packages/static-hosting/lib/static-hosting.ts | 34 ++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/prerender-proxy/lib/prerender-lambda-construct.ts b/packages/prerender-proxy/lib/prerender-lambda-construct.ts index 198a7cf9..1eedd39c 100644 --- a/packages/prerender-proxy/lib/prerender-lambda-construct.ts +++ b/packages/prerender-proxy/lib/prerender-lambda-construct.ts @@ -34,25 +34,25 @@ export class PrerenderLambda extends Construct { this.prerenderCheckFunction = new PrerenderCheckFunction( this, - "PrerenderViewerRequest", + `${id}-PrerenderViewerRequest`, props.prerenderCheckOptions ); this.prerenderFunction = new PrerenderFunction( this, - "PrerenderOriginRequest", + `${id}-PrerenderOriginRequest`, props.prerenderProps ); this.errorResponseFunction = new ErrorResponseFunction( this, - "ErrorResponse", + `${id}-ErrorResponse`, props.errorResponseProps ); this.cacheControlFunction = new CloudFrontCacheControl( this, - "PrerenderCloudFrontCacheControl", + `${id}-PrerenderCloudFrontCacheControl`, props.cacheControlProps ); } diff --git a/packages/static-hosting/index.ts b/packages/static-hosting/index.ts index d42a225a..e2302988 100644 --- a/packages/static-hosting/index.ts +++ b/packages/static-hosting/index.ts @@ -1,4 +1,8 @@ -import { StaticHosting, StaticHostingProps } from "./lib/static-hosting"; +import { + StaticHosting, + StaticHostingProps, + remapPath, +} from "./lib/static-hosting"; import { CSP } from "./types/csp"; -export { StaticHosting, StaticHostingProps, CSP }; +export { StaticHosting, StaticHostingProps, CSP, remapPath }; diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index de7a4828..1ed6d476 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -139,6 +139,13 @@ export interface StaticHostingProps { */ enableStaticFileRemap?: boolean; + /** + * Any prefixes to remapping that should be included in the path such as au or nz + * + * @default true + */ + behaviourPrefixes?: { prefix: string; edgeLambdas: EdgeLambda[] }[]; + /** * Optional additional properties for static file remap behaviours * @@ -292,7 +299,7 @@ export interface StaticHostingProps { comment?: string; } -interface remapPath { +export interface remapPath { from: string; to?: string; behaviour?: Partial; @@ -560,6 +567,31 @@ export class StaticHosting extends Construct { } } + if (enableStaticFileRemap) { + const staticFileRemapPrefixes = props.behaviourPrefixes?.map( + prefix => `${prefix.prefix}/` + ) || [""]; + staticFileRemapPrefixes.forEach(prefix => { + this.staticFiles.forEach(path => { + additionalBehaviors[`${prefix}*.${path}`] = { + origin: s3Origin, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + }; + }); + }); + } + + props.behaviourPrefixes?.forEach(prefix => { + additionalBehaviors[`${prefix.prefix}*`] = { + origin: s3Origin, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + edgeLambdas: prefix.edgeLambdas, + originRequestPolicy: originRequestPolicy, + cachePolicy: originCachePolicy, + responseHeadersPolicy: responseHeadersPolicy, + }; + }); + if (props.responseHeadersPolicies?.defaultBehaviorResponseHeaderPolicy) { defaultBehavior.responseHeadersPolicy = props.responseHeadersPolicies.defaultBehaviorResponseHeaderPolicy; From 5a60fe10ae9e970fcccf068c1ea5d3999e155400 Mon Sep 17 00:00:00 2001 From: finn Date: Tue, 17 Sep 2024 13:27:30 +0930 Subject: [PATCH 2/6] Added options for behaviour prefixes au nz etc --- packages/static-hosting/lib/static-hosting.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index 1ed6d476..61c7e268 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -544,16 +544,6 @@ export class StaticHosting extends Construct { } } - if (enableStaticFileRemap) { - for (const path of this.staticFiles) { - additionalBehaviors[`*.${path}`] = { - origin: s3Origin, - viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, - ...props.staticFileRemapOptions, - }; - } - } - // Note: A given path may override if the same path is defined both remapPaths and remapBackendPaths. This is an // unlikely scenario but worth noting. e.g. `/robots.txt` should be defined in one of the above but not both. if (props.remapPaths) { From 0d7f5d81296911b4db20c84f3d889bb5b665d434 Mon Sep 17 00:00:00 2001 From: finnholland <65142636+finnholland@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:25:48 +1030 Subject: [PATCH 3/6] Fix: Rename behaviourPrefixes prop --- packages/static-hosting/lib/static-hosting.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index 61c7e268..cf049143 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -140,11 +140,14 @@ export interface StaticHostingProps { enableStaticFileRemap?: boolean; /** - * Any prefixes to remapping that should be included in the path such as au or nz + * Overrides default behaviour paths with a prefix and takes in options to apply to each static file behaviour * * @default true */ - behaviourPrefixes?: { prefix: string; edgeLambdas: EdgeLambda[] }[]; + defaultBehaviourPrefixes?: { + prefix: string; + behaviourOverride: Partial; + }[]; /** * Optional additional properties for static file remap behaviours @@ -558,7 +561,7 @@ export class StaticHosting extends Construct { } if (enableStaticFileRemap) { - const staticFileRemapPrefixes = props.behaviourPrefixes?.map( + const staticFileRemapPrefixes = props.defaultBehaviourPrefixes?.map( prefix => `${prefix.prefix}/` ) || [""]; staticFileRemapPrefixes.forEach(prefix => { @@ -571,11 +574,11 @@ export class StaticHosting extends Construct { }); } - props.behaviourPrefixes?.forEach(prefix => { + props.defaultBehaviourPrefixes?.forEach(prefix => { additionalBehaviors[`${prefix.prefix}*`] = { origin: s3Origin, viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, - edgeLambdas: prefix.edgeLambdas, + edgeLambdas: prefix.behaviourOverride.edgeLambdas, originRequestPolicy: originRequestPolicy, cachePolicy: originCachePolicy, responseHeadersPolicy: responseHeadersPolicy, From faa19f8d1fbebe5cfe4242c1585aa3a9f7090102 Mon Sep 17 00:00:00 2001 From: finnholland <65142636+finnholland@users.noreply.github.com> Date: Fri, 25 Oct 2024 09:30:22 +1030 Subject: [PATCH 4/6] Allow override of whole behaviourOptions, edited a comment --- packages/static-hosting/lib/static-hosting.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index cf049143..73e456bc 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -140,7 +140,7 @@ export interface StaticHostingProps { enableStaticFileRemap?: boolean; /** - * Overrides default behaviour paths with a prefix and takes in options to apply to each static file behaviour + * Overrides default behaviour paths with a prefix and takes in behviour options to apply on the prefix behaviour * * @default true */ @@ -576,12 +576,17 @@ export class StaticHosting extends Construct { props.defaultBehaviourPrefixes?.forEach(prefix => { additionalBehaviors[`${prefix.prefix}*`] = { - origin: s3Origin, - viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + origin: prefix.behaviourOverride.origin ?? s3Origin, + viewerProtocolPolicy: + prefix.behaviourOverride.viewerProtocolPolicy ?? + ViewerProtocolPolicy.REDIRECT_TO_HTTPS, edgeLambdas: prefix.behaviourOverride.edgeLambdas, - originRequestPolicy: originRequestPolicy, - cachePolicy: originCachePolicy, - responseHeadersPolicy: responseHeadersPolicy, + originRequestPolicy: + prefix.behaviourOverride.originRequestPolicy ?? originRequestPolicy, + cachePolicy: prefix.behaviourOverride.cachePolicy ?? originCachePolicy, + responseHeadersPolicy: + prefix.behaviourOverride.responseHeadersPolicy ?? + responseHeadersPolicy, }; }); From ace316aaa8ddfb7e666ac6ed311e7aecf7092961 Mon Sep 17 00:00:00 2001 From: finnholland <65142636+finnholland@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:38:19 +1030 Subject: [PATCH 5/6] spread behaviouroverride props --- packages/static-hosting/lib/static-hosting.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index 73e456bc..be47d391 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -576,17 +576,15 @@ export class StaticHosting extends Construct { props.defaultBehaviourPrefixes?.forEach(prefix => { additionalBehaviors[`${prefix.prefix}*`] = { - origin: prefix.behaviourOverride.origin ?? s3Origin, + origin: s3Origin, viewerProtocolPolicy: - prefix.behaviourOverride.viewerProtocolPolicy ?? ViewerProtocolPolicy.REDIRECT_TO_HTTPS, edgeLambdas: prefix.behaviourOverride.edgeLambdas, - originRequestPolicy: - prefix.behaviourOverride.originRequestPolicy ?? originRequestPolicy, - cachePolicy: prefix.behaviourOverride.cachePolicy ?? originCachePolicy, + originRequestPolicy: originRequestPolicy, + cachePolicy: originCachePolicy, responseHeadersPolicy: - prefix.behaviourOverride.responseHeadersPolicy ?? responseHeadersPolicy, + ...prefix.behaviourOverride }; }); From 4dc214df13e00be221c5e0bb6c6c1f0d1339b710 Mon Sep 17 00:00:00 2001 From: finnholland <65142636+finnholland@users.noreply.github.com> Date: Fri, 1 Nov 2024 15:22:03 +1030 Subject: [PATCH 6/6] removed excess edge lambda line --- packages/static-hosting/lib/static-hosting.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index be47d391..b1fd7cae 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -577,14 +577,11 @@ export class StaticHosting extends Construct { props.defaultBehaviourPrefixes?.forEach(prefix => { additionalBehaviors[`${prefix.prefix}*`] = { origin: s3Origin, - viewerProtocolPolicy: - ViewerProtocolPolicy.REDIRECT_TO_HTTPS, - edgeLambdas: prefix.behaviourOverride.edgeLambdas, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, originRequestPolicy: originRequestPolicy, cachePolicy: originCachePolicy, - responseHeadersPolicy: - responseHeadersPolicy, - ...prefix.behaviourOverride + responseHeadersPolicy: responseHeadersPolicy, + ...prefix.behaviourOverride, }; });