cdk-nextjs-standalone
Version:
Deploy a NextJS app to AWS using CDK and OpenNext.
202 lines (201 loc) • 7.82 kB
TypeScript
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import { AddBehaviorOptions, CachePolicyProps, Distribution } from 'aws-cdk-lib/aws-cloudfront';
import { HttpOriginProps } from 'aws-cdk-lib/aws-cloudfront-origins';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { OptionalCloudFrontFunctionProps, OptionalDistributionProps, OptionalEdgeFunctionProps, OptionalS3OriginProps } from './generated-structs';
import { NextjsProps } from './Nextjs';
import { NextjsBuild } from './NextjsBuild';
import { NextjsDomain } from './NextjsDomain';
export interface ViewerRequestFunctionProps extends OptionalCloudFrontFunctionProps {
/**
* Cloudfront function code that runs on VIEWER_REQUEST.
* The following comments will be replaced with code snippets
* so you can customize this function.
*
* INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER: Add the required x-forwarded-host header.
* INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY: Improves open-next cache key.
*
* @default
* async function handler(event) {
* // INJECT_CLOUDFRONT_FUNCTION_HOST_HEADER
* // INJECT_CLOUDFRONT_FUNCTION_CACHE_HEADER_KEY
* }
*/
readonly code?: cloudfront.FunctionCode;
}
export interface NextjsDistributionDefaults {
/**
* Prevent the creation of a default response headers policy for static requests.
* Has no effect if a `staticBehaviorOptions.responseHeadersPolicy` is provided in {@link NextjsDistributionProps.overrides}
* @default false
*/
readonly staticResponseHeadersPolicy?: boolean;
/**
* Prevent the creation of a default cache policy for server requests.
* Has no effect if a `serverBehaviorOptions.cachePolicy` is provided in {@link NextjsDistributionProps.overrides}
* @default false
*/
readonly serverCachePolicy?: boolean;
/**
* Prevent the creation of a default response headers policy for server requests.
* Has no effect if a `serverBehaviorOptions.responseHeadersPolicy` is provided in {@link NextjsDistributionProps.overrides}
* @default false
*/
readonly serverResponseHeadersPolicy?: boolean;
/**
* Prevent the creation of a default cache policy for image requests.
* Has no effect if a `imageBehaviorOptions.cachePolicy` is provided in {@link NextjsDistributionProps.overrides}
* @default false
*/
readonly imageCachePolicy?: boolean;
/**
* Prevent the creation of a default response headers policy for image requests.
* Has no effect if a `imageBehaviorOptions.responseHeadersPolicy` is provided in {@link NextjsDistributionProps.overrides}
* @default false
*/
readonly imageResponseHeadersPolicy?: boolean;
}
export interface NextjsDistributionOverrides {
readonly viewerRequestFunctionProps?: ViewerRequestFunctionProps;
readonly distributionProps?: OptionalDistributionProps;
readonly edgeFunctionProps?: OptionalEdgeFunctionProps;
readonly imageBehaviorOptions?: AddBehaviorOptions;
readonly imageCachePolicyProps?: CachePolicyProps;
readonly imageResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps;
readonly imageHttpOriginProps?: HttpOriginProps;
readonly serverBehaviorOptions?: AddBehaviorOptions;
readonly serverCachePolicyProps?: CachePolicyProps;
readonly serverResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps;
readonly serverHttpOriginProps?: HttpOriginProps;
readonly staticBehaviorOptions?: AddBehaviorOptions;
readonly staticResponseHeadersPolicyProps?: cloudfront.ResponseHeadersPolicyProps;
readonly s3OriginProps?: OptionalS3OriginProps;
}
export interface NextjsDistributionProps {
/**
* @see {@link NextjsProps.basePath}
*/
readonly basePath?: NextjsProps['basePath'];
/**
* @see {@link NextjsProps.distribution}
*/
readonly distribution?: NextjsProps['distribution'];
/**
* Override lambda function url auth type
* @default "NONE"
*/
readonly functionUrlAuthType?: lambda.FunctionUrlAuthType;
/**
* Lambda function to optimize images.
* Must be provided if you want to serve dynamic requests.
*/
readonly imageOptFunction: lambda.IFunction;
/**
* @see {@link NextjsBuild}
*/
readonly nextBuild: NextjsBuild;
/**
* @see {@link NextjsDomain}
*/
readonly nextDomain?: NextjsDomain;
/**
* @see {@link NextjsProps.nextjsPath}
*/
readonly nextjsPath: NextjsProps['nextjsPath'];
/**
* Override props for every construct.
*/
readonly overrides?: NextjsDistributionOverrides;
/**
* Lambda function to route all non-static requests to.
* Must be provided if you want to serve dynamic requests.
*/
readonly serverFunction: lambda.IFunction;
/**
* Bucket containing static assets.
* Must be provided if you want to serve static files.
*/
readonly staticAssetsBucket: s3.IBucket;
/**
* @see {@link NextjsProps.streaming}
*/
readonly streaming?: boolean;
/**
* Supress the creation of default policies if
* none are provided by you
*/
readonly supressDefaults?: NextjsDistributionDefaults;
}
/**
* Create a CloudFront distribution to serve a Next.js application.
*/
export declare class NextjsDistribution extends Construct {
private props;
/**
* The internally created CloudFront `Distribution` instance.
*/
distribution: Distribution;
private commonBehaviorOptions;
/**
* Common security headers applied by default to all origins
* @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-response-headers-policies.html#managed-response-headers-policies-security
*/
private commonSecurityHeadersBehavior;
private s3Origin;
private staticBehaviorOptions;
private edgeLambdas;
private serverBehaviorOptions;
private imageBehaviorOptions;
constructor(scope: Construct, id: string, props: NextjsDistributionProps);
/**
* The CloudFront URL of the website.
*/
get url(): string;
/**
* The ID of the internally created CloudFront Distribution.
*/
get distributionId(): string;
/**
* The domain name of the internally created CloudFront Distribution.
*/
get distributionDomain(): string;
private get isFnUrlIamAuth();
private createStaticBehaviorOptions;
private get fnUrlAuthType();
/**
* Once CloudFront OAC is released, remove this to reduce latency.
*/
private createEdgeLambda;
private createServerBehaviorOptions;
private useCloudFrontFunctionHostHeader;
private useCloudFrontFunctionCacheHeaderKey;
/**
* If this doesn't run, then Next.js Server's `request.url` will be Lambda Function
* URL instead of domain
*/
private createCloudFrontFnAssociations;
private createImageBehaviorOptions;
/**
* Creates or uses user specified CloudFront Distribution adding behaviors
* needed for Next.js.
*/
private getCloudFrontDistribution;
/**
* Creates default CloudFront Distribution. Note, this construct will not
* create a CloudFront Distribution if one is passed in by user.
*/
private createCloudFrontDistribution;
/**
* this needs to be added last so that it doesn't override any other behaviors
* when basePath is set, we emulate the "default behavior" (*) and / as `/base-path/*`
* @private
*/
private addRootPathBehavior;
private addStaticBehaviorsToDistribution;
/**
* Optionally prepends base path to given path pattern.
*/
private getPathPattern;
}