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;