Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behaviour Prefixes (for CDK v2 upgrade) #1401

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
44 changes: 33 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,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[] }[];
TheOrangePuff marked this conversation as resolved.
Show resolved Hide resolved

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

interface remapPath {
export interface remapPath {
from: string;
to?: string;
behaviour?: Partial<BehaviorOptions>;
Expand Down Expand Up @@ -537,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) {
Expand All @@ -560,6 +557,31 @@ export class StaticHosting extends Construct {
}
}

if (enableStaticFileRemap) {
const staticFileRemapPrefixes = props.behaviourPrefixes?.map(
prefix => `${prefix.prefix}/`
) || [""];
TheOrangePuff marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
Loading