From 255177ed9e5747a97de2961211e152929048579a Mon Sep 17 00:00:00 2001 From: Finn <65142636+finnholland@users.noreply.github.com> Date: Mon, 4 Nov 2024 11:26:30 +1030 Subject: [PATCH] Behaviour Prefixes (for CDK v2 upgrade) (#1401) * Added options for behaviour prefixes au nz etc * Added options for behaviour prefixes au nz etc * Fix: Rename behaviourPrefixes prop * Allow override of whole behaviourOptions, edited a comment * spread behaviouroverride props * removed excess edge lambda line --- .../lib/prerender-lambda-construct.ts | 8 ++-- packages/static-hosting/index.ts | 8 +++- packages/static-hosting/lib/static-hosting.ts | 47 ++++++++++++++----- 3 files changed, 46 insertions(+), 17 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 7835190d..dcc5dbe0 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -139,6 +139,16 @@ export interface StaticHostingProps { */ enableStaticFileRemap?: boolean; + /** + * Overrides default behaviour paths with a prefix and takes in behviour options to apply on the prefix behaviour + * + * @default true + */ + defaultBehaviourPrefixes?: { + prefix: string; + behaviourOverride: Partial; + }[]; + /** * Optional additional properties for static file remap behaviours * @@ -292,7 +302,7 @@ export interface StaticHostingProps { comment?: string; } -interface remapPath { +export interface remapPath { from: string; to?: string; behaviour?: Partial; @@ -537,16 +547,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) { @@ -560,6 +560,31 @@ export class StaticHosting extends Construct { } } + if (enableStaticFileRemap) { + const staticFileRemapPrefixes = props.defaultBehaviourPrefixes?.map( + prefix => `${prefix.prefix}/` + ) || [""]; + staticFileRemapPrefixes.forEach(prefix => { + this.staticFiles.forEach(path => { + additionalBehaviors[`${prefix}*.${path}`] = { + origin: s3Origin, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + }; + }); + }); + } + + props.defaultBehaviourPrefixes?.forEach(prefix => { + additionalBehaviors[`${prefix.prefix}*`] = { + origin: s3Origin, + viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS, + originRequestPolicy: originRequestPolicy, + cachePolicy: originCachePolicy, + responseHeadersPolicy: responseHeadersPolicy, + ...prefix.behaviourOverride, + }; + }); + if (props.responseHeadersPolicies?.defaultBehaviorResponseHeaderPolicy) { defaultBehavior.responseHeadersPolicy = props.responseHeadersPolicies.defaultBehaviorResponseHeaderPolicy;