Skip to content

Commit

Permalink
fix: remove NextjsBaseProps (#173)
Browse files Browse the repository at this point in the history
* refactor: remove NextjsBaseProps

* 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 17, 2023
1 parent 48ab3b6 commit 08a5a16
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 1,109 deletions.
1,065 changes: 127 additions & 938 deletions API.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/major-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
- Remove `BaseSiteEnvironmentOutputsInfo, BaseSiteReplaceProps` exports as not used anymore
- Remove `compressionLevel` to simplify configuration. We use optimal for windows or max compression for unix
- Remove `nodeEnv` because it can be configured through `environment` prop.
- Remove `sharpLayerArn` because it's not used
- Remove `projectRoot` because it's not used
- Remove `NextjsBaseProps` to simplify props
- Remove `projectRoot` as it's not being used
- Remove `tempBuildDir` as it's not being used


## v3
Expand Down
82 changes: 48 additions & 34 deletions src/Nextjs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import * as fs from 'node:fs';
import * as os from 'os';
import * as path from 'path';
import { Distribution } from 'aws-cdk-lib/aws-cloudfront';
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, NextjsBaseProps } from './NextjsBase';
import { BaseSiteDomainProps } from './NextjsBase';
import { NextjsBuild } from './NextjsBuild';
import { NextjsDistribution, NextjsDistributionProps } from './NextjsDistribution';
import { NextjsImage } from './NextjsImage';
Expand Down Expand Up @@ -40,22 +37,7 @@ export interface NextjsDefaultsProps {
readonly distribution?: NextjsDistributionProps | any;
}

export interface NextjsProps extends NextjsBaseProps {
/**
* Optional S3 Bucket to use, defaults to assets bucket
*/
readonly imageOptimizationBucket?: s3.IBucket;
/**
* Allows you to override defaults for the resources created by this
* construct.
*/
readonly defaults?: NextjsDefaultsProps;
/**
* Skips running Next.js build. Useful if you want to deploy `Nextjs` but
* haven't made any changes to Next.js app code.
* @default false
*/
readonly skipBuild?: boolean;
export interface NextjsProps {
/**
* Optional value to prefix the Next.js site under a /prefix path on CloudFront.
* Usually used when you deploy multiple Next.js sites on same domain using /sub-path
Expand All @@ -66,11 +48,56 @@ export interface NextjsProps extends NextjsBaseProps {
* @example "/my-base-path"
*/
readonly basePath?: string;
/**
* Optional value used to install NextJS node dependencies.
* @default 'npx --yes open-next@^2 build'
*/
readonly buildCommand?: string;
/**
* The directory to execute `npm run build` from. By default, it is `nextjsPath`.
* Can be overridden, particularly useful for monorepos where `build` is expected to run
* at the root of the project.
*/
readonly buildPath?: string;
/**
* Allows you to override defaults for the resources created by this
* construct.
*/
readonly defaults?: NextjsDefaultsProps;
/**
* Optional CloudFront Distribution created outside of this construct that will
* be used to add Next.js behaviors and origins onto. Useful with `basePath`.
*/
readonly distribution?: Distribution;
/**
* Custom environment variables to pass to the NextJS build **and** runtime.
*/
readonly environment?: Record<string, string>;
/**
* Optional S3 Bucket to use, defaults to assets bucket
*/
readonly imageOptimizationBucket?: s3.IBucket;
/**
* Relative path to the directory where the NextJS project is located.
* Can be the root of your project (`.`) or a subdirectory (`packages/web`).
*/
readonly nextjsPath: string;
/**
* Less build output.
*/
readonly quiet?: boolean;
/**
* Skips running Next.js build. Useful if you want to deploy `Nextjs` but
* haven't made any changes to Next.js app code.
* @default false
*/
readonly skipBuild?: boolean;
/**
* By default all CloudFront cache will be invalidated on deployment.
* This can be set to true to skip the full cache invalidation, which
* could be important for some users.
*/
readonly skipFullInvalidation?: boolean;
}

/**
Expand Down Expand Up @@ -113,17 +140,6 @@ export class Nextjs extends Construct {
*/
public distribution: NextjsDistribution;

/**
* Where build-time assets for deployment are stored.
*/
public get tempBuildDir(): string {
return this.props.tempBuildDir
? path.resolve(
path.join(this.props.tempBuildDir, `nextjs-cdk-build-${this.node.id}-${this.node.addr.substring(0, 4)}`)
)
: fs.mkdtempSync(path.join(os.tmpdir(), 'nextjs-cdk-build-'));
}

/**
* Revalidation handler and queue.
*/
Expand All @@ -136,7 +152,7 @@ export class Nextjs extends Construct {
super(scope, id);

// build nextjs app
this.nextBuild = new NextjsBuild(this, id, { ...props, tempBuildDir: this.tempBuildDir });
this.nextBuild = new NextjsBuild(this, id, props);

// deploy nextjs static assets to s3
this.staticAssets = new NextjsStaticAssets(this, 'StaticAssets', {
Expand All @@ -148,7 +164,6 @@ export class Nextjs extends Construct {

this.serverFunction = new NextjsServer(this, 'Server', {
...props,
tempBuildDir: this.tempBuildDir,
nextBuild: this.nextBuild,
lambda: props.defaults?.lambda,
staticAssetBucket: this.staticAssets.bucket,
Expand All @@ -172,7 +187,6 @@ export class Nextjs extends Construct {
...props,
...props.defaults?.distribution,
staticAssetsBucket: this.staticAssets.bucket,
tempBuildDir: this.tempBuildDir,
nextBuild: this.nextBuild,
serverFunction: this.serverFunction.lambdaFunction,
imageOptFunction: this.imageOptimizationFunction,
Expand Down
59 changes: 0 additions & 59 deletions src/NextjsBase.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,6 @@
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
import { IHostedZone } from 'aws-cdk-lib/aws-route53';

/**
* Common props shared across NextJS-related CDK constructs.
*/
export interface NextjsBaseProps {
/**
* Relative path to the directory where the NextJS project is located.
* Can be the root of your project (`.`) or a subdirectory (`packages/web`).
*/
readonly nextjsPath: string;

/**
* The directory to execute `npm run build` from. By default, it is `nextjsPath`.
* Can be overridden, particularly useful for monorepos where `build` is expected to run
* at the root of the project.
*/
readonly buildPath?: string;

/**
* Root of your project, if different from `nextjsPath`.
* Defaults to current working directory.
*/
readonly projectRoot?: string;

/**
* Custom environment variables to pass to the NextJS build and runtime.
*/
readonly environment?: Record<string, string>;

/**
* Directory to store temporary build files in.
* Defaults to os.tmpdir().
*/
readonly tempBuildDir?: string; // move to NextjsBuildProps?

/**
* Optional value used to install NextJS node dependencies.
* @default 'npx --yes open-next@^2 build'
*/
readonly buildCommand?: string;

/**
* Less build output.
*/
readonly quiet?: boolean;

/**
* Optional arn for the sharp lambda layer.
* If omitted, the layer will be created.
*/
readonly sharpLayerArn?: string;

/**
* By default all CloudFront cache will be invalidated on deployment.
* This can be set to true to skip the full cache invalidation, which
* could be important for some users.
*/
readonly skipFullInvalidation?: boolean;
}

///// stuff below taken from https://github.com/serverless-stack/sst/blob/8d377e941467ced81d8cc31ee67d5a06550f04d4/packages/resources/src/BaseSite.ts

export interface BaseSiteDomainProps {
Expand Down
30 changes: 25 additions & 5 deletions src/NextjsBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@ import {
NEXTJS_CACHE_DIR,
NEXTJS_BUILD_DYNAMODB_PROVIDER_FN_DIR,
} from './constants';
import { NextjsBaseProps } from './NextjsBase';
import type { NextjsProps } from './Nextjs';
import { NextjsBucketDeployment } from './NextjsBucketDeployment';
import { listDirectory } from './utils/list-directories';

export interface NextjsBuildProps extends NextjsBaseProps {
export interface NextjsBuildProps {
/**
* @see `NextjsProps.skipBuild`
* @see {@link NextjsProps.buildCommand}
*/
readonly skipBuild?: boolean;
readonly buildCommand?: NextjsProps['buildCommand'];
/**
* @see {@link NextjsProps.buildPath}
*/
readonly buildPath?: NextjsProps['buildPath'];
/**
* @see {@link NextjsProps.environment}
*/
readonly environment?: NextjsProps['environment'];
/**
* @see {@link NextjsProps.nextjsPath}
*/
readonly nextjsPath: NextjsProps['nextjsPath'];
/**
* @see {@link NextjsProps.quiet}
*/
readonly quiet?: NextjsProps['quiet'];
/**
* @see {@link NextjsProps.skipBuild}
*/
readonly skipBuild?: NextjsProps['skipBuild'];
}

/**
Expand Down Expand Up @@ -113,7 +133,7 @@ export class NextjsBuild extends Construct {
const buildCommand = this.props.buildCommand ?? 'npx open-next@^2 build';
// run build
if (!this.props.quiet) {
console.debug(`Running "${buildCommand}" in`, buildPath);
console.debug(`Running "${buildCommand}" in`, buildPath);
}
// will throw if build fails - which is desired
execSync(buildCommand, {
Expand Down
89 changes: 32 additions & 57 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import * as route53Targets from 'aws-cdk-lib/aws-route53-targets';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { DEFAULT_STATIC_MAX_AGE, NEXTJS_BUILD_DIR, NEXTJS_STATIC_DIR } from './constants';
import { BaseSiteDomainProps, NextjsBaseProps } from './NextjsBase';
import { NextjsProps } from './Nextjs';
import { BaseSiteDomainProps } from './NextjsBase';
import { NextjsBuild } from './NextjsBuild';

export interface NextjsDomainProps extends BaseSiteDomainProps {}
Expand Down Expand Up @@ -47,45 +48,19 @@ export interface NextjsOriginRequestPolicyProps {
readonly imageOptimizationOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;
}

export interface NextjsDistributionProps extends NextjsBaseProps {
export interface NextjsDistributionProps {
/**
* Bucket containing static assets.
* Must be provided if you want to serve static files.
*/
readonly staticAssetsBucket: s3.IBucket;

/**
* Lambda function to route all non-static requests to.
* Must be provided if you want to serve dynamic requests.
*/
readonly serverFunction: lambda.IFunction;

/**
* Lambda function to optimize images.
* Must be provided if you want to serve dynamic requests.
*/
readonly imageOptFunction: lambda.IFunction;

/**
* Overrides for created CDK resources.
* @see {@link NextjsProps.basePath}
*/
readonly cdk?: NextjsDistributionCdkProps;

/**
* Built NextJS app.
*/
readonly nextBuild: NextjsBuild;

readonly basePath?: NextjsProps['basePath'];
/**
* Override the default CloudFront cache policies created internally.
*/
readonly cachePolicies?: NextjsCachePolicyProps;

/**
* Override the default CloudFront origin request policies created internally.
* Overrides for created CDK resources.
*/
readonly originRequestPolicies?: NextjsOriginRequestPolicyProps;

readonly cdk?: NextjsDistributionCdkProps;
/**
* The customDomain for this website. Supports domains that are hosted
* either on [Route 53](https://aws.amazon.com/route53/) or externally.
Expand All @@ -107,42 +82,42 @@ export interface NextjsDistributionProps extends NextjsBaseProps {
* });
*/
readonly customDomain?: string | NextjsDomainProps;

/**
* Include the name of your deployment stage if present.
* Used to name the edge functions stack.
* Required if using SST.
*/
readonly stageName?: string;

/**
* Optional value to prefix the edge function stack
* It defaults to "Nextjs"
* @see {@link NextjsProps.distribution}
*/
readonly stackPrefix?: string;

readonly distribution?: NextjsProps['distribution'];
/**
* Override lambda function url auth type
* @default "NONE"
*/
readonly functionUrlAuthType?: lambda.FunctionUrlAuthType;

/**
* Optional value to prefix the Next.js site under a /prefix path on CloudFront.
* Usually used when you deploy multiple Next.js sites on same domain using /sub-path
*
* Note, you'll need to set [basePath](https://nextjs.org/docs/app/api-reference/next-config-js/basePath)
* in your `next.config.ts` to this value and ensure any files in `public`
* folder have correct prefix.
* @example "/my-base-path"
* Lambda function to optimize images.
* Must be provided if you want to serve dynamic requests.
*/
readonly basePath?: string;

readonly imageOptFunction: lambda.IFunction;
/**
* @see {@link NextjsBuild}
*/
readonly nextBuild: NextjsBuild;
/**
* Optional CloudFront Distribution created outside of this construct that will
* be used to add Next.js behaviors and origins onto. Useful with `basePath`.
* @see {@link NextjsProps.nextjsPath}
*/
readonly distribution?: Distribution;
readonly nextjsPath: NextjsProps['nextjsPath'];
/**
* Override the default CloudFront origin request policies created internally.
*/
readonly originRequestPolicies?: NextjsOriginRequestPolicyProps;
/**
* 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;
}

/**
Expand Down
Loading

0 comments on commit 08a5a16

Please sign in to comment.