From 6b14c3068deb8fe2c125d8a0f8aed8330421b72f Mon Sep 17 00:00:00 2001 From: finn Date: Tue, 17 Sep 2024 13:27:30 +0930 Subject: [PATCH] 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;