Skip to content

Commit

Permalink
Behaviour Prefixes (for CDK v2 upgrade) (#1401)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
finnholland authored Nov 4, 2024
1 parent 38c32dc commit 255177e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/prerender-proxy/lib/prerender-lambda-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/static-hosting/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
47 changes: 36 additions & 11 deletions packages/static-hosting/lib/static-hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BehaviorOptions>;
}[];

/**
* Optional additional properties for static file remap behaviours
*
Expand Down Expand Up @@ -292,7 +302,7 @@ export interface StaticHostingProps {
comment?: string;
}

interface remapPath {
export interface remapPath {
from: string;
to?: string;
behaviour?: Partial<BehaviorOptions>;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 255177e

Please sign in to comment.