Skip to content

Commit

Permalink
Merge pull request #89 from CoSchedule/origin-request-policy-props
Browse files Browse the repository at this point in the history
feat: Allow all origin request policies to be passed as props
  • Loading branch information
revmischa authored Mar 10, 2023
2 parents 9c1de90 + 114fd56 commit 566c315
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 20 deletions.
82 changes: 77 additions & 5 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 45 additions & 15 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface NextjsCachePolicyProps {
readonly staticClientMaxAgeDefault?: Duration;
}

export interface NextjsOriginRequestPolicyProps {
readonly lambdaOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;
readonly fallbackOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;
readonly imageOptimizationOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;
}

export interface NextjsDistributionProps extends NextjsBaseProps {
/**
* Bucket containing static assets.
Expand Down Expand Up @@ -81,9 +87,9 @@ export interface NextjsDistributionProps extends NextjsBaseProps {
readonly cachePolicies?: NextjsCachePolicyProps;

/**
* Override the default CloudFront lambda origin request policy created internally
* Override the default CloudFront origin request policies created internally.
*/
readonly lambdaOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;
readonly originRequestPolicies?: NextjsOriginRequestPolicyProps;

/**
* The customDomain for this website. Supports domains that are hosted
Expand Down Expand Up @@ -180,6 +186,20 @@ export class NextjsDistribution extends Construct {
comment: 'Nextjs Lambda Origin Request Policy',
};

public static fallbackOriginRequestPolicyProps: cloudfront.OriginRequestPolicyProps = {
cookieBehavior: cloudfront.OriginRequestCookieBehavior.all(), // pretty much disables caching - maybe can be changed
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.all(),
headerBehavior: cloudfront.OriginRequestHeaderBehavior.all(),
comment: 'Nextjs Fallback Origin Request Policy',
};

public static imageOptimizationOriginRequestPolicyProps: cloudfront.OriginRequestPolicyProps = {
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.allowList('q', 'w', 'url'),
headerBehavior: cloudfront.OriginRequestHeaderBehavior.allowList('accept'),
cookieBehavior: cloudfront.OriginRequestCookieBehavior.none(),
comment: 'Nextjs Image Optimization Origin Request Policy',
};

protected props: NextjsDistributionProps;

/////////////////////
Expand Down Expand Up @@ -276,7 +296,7 @@ export class NextjsDistribution extends Construct {
/////////////////////

private createCloudFrontDistribution(): cloudfront.Distribution {
const { cdk: cdkProps, cachePolicies, lambdaOriginRequestPolicy: lambdaOriginRequestPolicyOverride } = this.props;
const { cdk: cdkProps, cachePolicies, originRequestPolicies } = this.props;
const cfDistributionProps = cdkProps?.distribution;

// build domainNames
Expand Down Expand Up @@ -306,7 +326,8 @@ export class NextjsDistribution extends Construct {
const imageCachePolicy = cachePolicies?.imageCachePolicy ?? this.createCloudFrontImageCachePolicy();

// origin request policies
const lambdaOriginRequestPolicy = lambdaOriginRequestPolicyOverride ?? this.createLambdaOriginRequestPolicy();
const lambdaOriginRequestPolicy =
originRequestPolicies?.lambdaOriginRequestPolicy ?? this.createLambdaOriginRequestPolicy();

// main server function origin (lambda URL HTTP origin)
const fnUrl = this.props.serverFunction.addFunctionUrl({
Expand All @@ -324,11 +345,8 @@ export class NextjsDistribution extends Construct {
authType: lambda.FunctionUrlAuthType.NONE,
});
const imageOptFunctionOrigin = new origins.HttpOrigin(Fn.parseDomainName(imageOptFnUrl.url));
const imageOptORP = new cloudfront.OriginRequestPolicy(this, 'ImageOptPolicy', {
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.allowList('q', 'w', 'url'),
headerBehavior: cloudfront.OriginRequestHeaderBehavior.allowList('accept'),
cookieBehavior: cloudfront.OriginRequestCookieBehavior.none(),
});
const imageOptORP =
originRequestPolicies?.imageOptimizationOriginRequestPolicy ?? this.createImageOptimizationOriginRequestPolicy();

// lambda behavior edge function
const lambdaOriginRequestEdgeFn = this.buildLambdaOriginRequestEdgeFunction();
Expand Down Expand Up @@ -398,12 +416,8 @@ export class NextjsDistribution extends Construct {
// requests to fallback origin group (default behavior)
// used for S3 and lambda. would prefer to forward all headers to lambda but need to strip out host
// TODO: try to do this with headers whitelist or edge lambda
const fallbackOriginRequestPolicy = new cloudfront.OriginRequestPolicy(this, 'FallbackOriginRequestPolicy', {
cookieBehavior: cloudfront.OriginRequestCookieBehavior.all(), // pretty much disables caching - maybe can be changed
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.all(),
headerBehavior: cloudfront.OriginRequestHeaderBehavior.all(),
comment: 'Nextjs Fallback Origin Request Policy',
});
const fallbackOriginRequestPolicy =
originRequestPolicies?.fallbackOriginRequestPolicy ?? this.createFallbackOriginRequestPolicy();

// if we don't have a static file called index.html then we should
// redirect to the lambda handler
Expand Down Expand Up @@ -474,6 +488,22 @@ export class NextjsDistribution extends Construct {
);
}

private createFallbackOriginRequestPolicy(): cloudfront.OriginRequestPolicy {
return new cloudfront.OriginRequestPolicy(
this,
'FallbackOriginRequestPolicy',
NextjsDistribution.fallbackOriginRequestPolicyProps
);
}

private createImageOptimizationOriginRequestPolicy(): cloudfront.OriginRequestPolicy {
return new cloudfront.OriginRequestPolicy(
this,
'ImageOptPolicy',
NextjsDistribution.imageOptimizationOriginRequestPolicyProps
);
}

private createCloudFrontLambdaCachePolicy(): cloudfront.CachePolicy {
return new cloudfront.CachePolicy(this, 'LambdaCache', NextjsDistribution.lambdaCachePolicyProps);
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
NextjsDistributionProps,
NextjsDomainProps,
NextjsCachePolicyProps,
NextjsOriginRequestPolicyProps,
} from './NextjsDistribution';

// L3 constructs
Expand Down

0 comments on commit 566c315

Please sign in to comment.