Skip to content

Commit

Permalink
feat: extract NextjsDomain from NextjsDistribution (#174)
Browse files Browse the repository at this point in the history
* feat: NextjsDomain

* docs: note changes

* fix: make getHostedZone private

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* fix: certificate and nextjsPath on NextDistribution

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* fix: make NextjsDomain optional for NextjsDistribution

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* fix: allow overriding Distribution domainNames and certificate

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* feat: remove domainAliases, create dns records for alternateNames, add isWildcardCertificate prop to create wildcard certificate by default

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* docs: improve NextjsDomain TSDoc

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* docs: add Cloudflare example doc

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* fix: remove isWildcardCertificate

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* docs: improve docs

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
bestickley and github-actions authored Nov 28, 2023
1 parent 08a5a16 commit 99842fe
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 470 deletions.
418 changes: 231 additions & 187 deletions API.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/major-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Remove `NextjsBaseProps` to simplify props
- Remove `projectRoot` as it's not being used
- Remove `tempBuildDir` as it's not being used
- Create `NextjsDomain`. Remove custom domain related props from `NextjsDistribution`.


## v3
Expand Down
2 changes: 1 addition & 1 deletion open-next
39 changes: 24 additions & 15 deletions src/Nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
import { FunctionOptions } from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { BaseSiteDomainProps } from './NextjsBase';
import { NextjsBuild } from './NextjsBuild';
import { NextjsDistribution, NextjsDistributionProps } from './NextjsDistribution';
import { NextjsDomain, NextjsDomainProps } from './NextjsDomain';
import { NextjsImage } from './NextjsImage';
import { NextjsInvalidation } from './NextjsInvalidation';
import { NextjsRevalidation } from './NextjsRevalidation';
import { NextjsServer } from './NextjsServer';
import { NextjsStaticAssets, NextjsStaticAssetsProps } from './NextjsStaticAssets';

export interface NextjsDomainProps extends BaseSiteDomainProps {}

/**
* Defaults for created resources.
* Why `any`? see https://github.com/aws/jsii/issues/2901
Expand Down Expand Up @@ -69,6 +67,11 @@ export interface NextjsProps {
* be used to add Next.js behaviors and origins onto. Useful with `basePath`.
*/
readonly distribution?: Distribution;
/**
* Props to configure {@link NextjsDomain}. See details on how to customize at
* {@link NextjsDomainProps}
*/
readonly domainProps?: NextjsDomainProps;
/**
* Custom environment variables to pass to the NextJS build **and** runtime.
*/
Expand Down Expand Up @@ -119,27 +122,26 @@ export class Nextjs extends Construct {
* The main NextJS server handler lambda function.
*/
public serverFunction: NextjsServer;

/**
* The image optimization handler lambda function.
*/
public imageOptimizationFunction: NextjsImage;

/**
* Built NextJS project output.
*/
public nextBuild: NextjsBuild;

/**
* Asset deployment to S3.
*/
public staticAssets: NextjsStaticAssets;

/**
* Optional Route53 Hosted Zone, ACM Certificate, and Route53 DNS Records
*/
public domain?: NextjsDomain;
/**
* CloudFront distribution.
*/
public distribution: NextjsDistribution;

/**
* Revalidation handler and queue.
*/
Expand All @@ -156,41 +158,48 @@ export class Nextjs extends Construct {

// deploy nextjs static assets to s3
this.staticAssets = new NextjsStaticAssets(this, 'StaticAssets', {
basePath: props.basePath,
bucket: props.defaults?.assetDeployment?.bucket,
environment: props.environment,
nextBuild: this.nextBuild,
basePath: props.basePath,
});

this.serverFunction = new NextjsServer(this, 'Server', {
...props,
nextBuild: this.nextBuild,
lambda: props.defaults?.lambda,
staticAssetBucket: this.staticAssets.bucket,
});
// build image optimization
this.imageOptimizationFunction = new NextjsImage(this, 'ImgOptFn', {
...props,
nextBuild: this.nextBuild,
bucket: props.imageOptimizationBucket || this.bucket,
lambdaOptions: props.defaults?.lambda,
nextBuild: this.nextBuild,
});

// build revalidation queue and handler function
this.revalidation = new NextjsRevalidation(this, 'Revalidation', {
...props,
lambdaOptions: props.defaults?.lambda,
nextBuild: this.nextBuild,
serverFunction: this.serverFunction,
});

if (this.props.domainProps) {
this.domain = new NextjsDomain(this, 'Domain', this.props.domainProps);
}
this.distribution = new NextjsDistribution(this, 'Distribution', {
...props,
nextjsPath: props.nextjsPath,
basePath: props.basePath,
distribution: props.distribution,
...props.defaults?.distribution,
staticAssetsBucket: this.staticAssets.bucket,
nextBuild: this.nextBuild,
nextDomain: this.domain,
serverFunction: this.serverFunction.lambdaFunction,
imageOptFunction: this.imageOptimizationFunction,
});
if (this.domain) {
this.domain.createDnsRecords(this.distribution.distribution);
}

if (!this.props.skipFullInvalidation) {
new NextjsInvalidation(this, 'Invalidation', {
Expand All @@ -204,7 +213,7 @@ export class Nextjs extends Construct {
* URL of Next.js App.
*/
public get url(): string {
const customDomain = this.distribution.customDomainName;
const customDomain = this.props.domainProps?.domainName;
return customDomain ? `https://${customDomain}` : this.distribution.url;
}

Expand Down
38 changes: 0 additions & 38 deletions src/NextjsBase.ts

This file was deleted.

Loading

0 comments on commit 99842fe

Please sign in to comment.