From 72cb72c7d8f37395dfa8f16c2305f278812711b9 Mon Sep 17 00:00:00 2001 From: J Sachs Date: Mon, 6 Feb 2023 17:48:22 +0100 Subject: [PATCH] feat: customize name of edge function stack id (#79) * feat: customize name of edge function stack id * docs: update API documentation * refactor: shorten stack id Co-authored-by: Mischa Spiegelmock --------- Co-authored-by: Mischa Spiegelmock --- API.md | 146 ++++++++++++++++++++++++++++++++++---- src/NextjsBase.ts | 4 +- src/NextjsBuild.ts | 4 +- src/NextjsDistribution.ts | 23 +++--- 4 files changed, 148 insertions(+), 29 deletions(-) diff --git a/API.md b/API.md index 9466080a..7f4cbfd1 100644 --- a/API.md +++ b/API.md @@ -2687,10 +2687,11 @@ const imageOptimizationProps: ImageOptimizationProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -2740,7 +2741,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -2782,6 +2783,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -2933,10 +2948,11 @@ const nextjsAssetsDeploymentProps: NextjsAssetsDeploymentProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -2987,7 +3003,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -3029,6 +3045,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -3149,10 +3179,11 @@ const nextjsBaseProps: NextjsBaseProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -3198,7 +3229,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -3240,6 +3271,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -3296,10 +3341,11 @@ const nextjsBuildProps: NextjsBuildProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -3345,7 +3391,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -3387,6 +3433,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -3600,10 +3660,11 @@ const nextjsDistributionProps: NextjsDistributionProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -3615,6 +3676,7 @@ const nextjsDistributionProps: NextjsDistributionProps = { ... } | cdk | NextjsDistributionCdkProps | Overrides for created CDK resources. | | customDomain | string \| NextjsDomainProps | The customDomain for this website. Supports domains that are hosted either on [Route 53](https://aws.amazon.com/route53/) or externally. | | lambdaOriginRequestPolicy | aws-cdk-lib.aws_cloudfront.IOriginRequestPolicy | Override the default CloudFront lambda origin request policy created internally. | +| stackPrefix | string | Optional value to prefix the edge function stack It defaults to "Nextjs". | | stageName | string | Include the name of your deployment stage if present. | --- @@ -3658,7 +3720,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -3700,6 +3762,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -3862,6 +3938,18 @@ Override the default CloudFront lambda origin request policy created internally. --- +##### `stackPrefix`Optional + +```typescript +public readonly stackPrefix: string; +``` + +- *Type:* string + +Optional value to prefix the edge function stack It defaults to "Nextjs". + +--- + ##### `stageName`Optional ```typescript @@ -4000,10 +4088,11 @@ const nextjsLambdaProps: NextjsLambdaProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -4051,7 +4140,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -4093,6 +4182,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript @@ -4184,10 +4287,11 @@ const nextjsProps: NextjsProps = { ... } | --- | --- | --- | | nextjsPath | string | Relative path to the directory where the NextJS project is located. | | buildPath | string | The directory to execute `npm run build` from. | -| compressionLevel | number | 0 - no compression, fatest 9 - maximum compression, slowest. | +| compressionLevel | number | 0 - no compression, fastest 9 - maximum compression, slowest. | | environment | {[ key: string ]: string} | Custom environment variables to pass to the NextJS build and runtime. | | isPlaceholder | boolean | Skip building app and deploy a placeholder. | | nodeEnv | string | Optional value for NODE_ENV during build and runtime. | +| packageManager | string | Optional value used to install NextJS node dependencies. | | projectRoot | string | Root of your project, if different from `nextjsPath`. | | quiet | boolean | Less build output. | | tempBuildDir | string | Directory to store temporary build files in. | @@ -4235,7 +4339,7 @@ public readonly compressionLevel: number; - *Type:* number - *Default:* 1 -0 - no compression, fatest 9 - maximum compression, slowest. +0 - no compression, fastest 9 - maximum compression, slowest. --- @@ -4277,6 +4381,20 @@ Optional value for NODE_ENV during build and runtime. --- +##### `packageManager`Optional + +```typescript +public readonly packageManager: string; +``` + +- *Type:* string + +Optional value used to install NextJS node dependencies. + +It defaults to "npm" + +--- + ##### `projectRoot`Optional ```typescript diff --git a/src/NextjsBase.ts b/src/NextjsBase.ts index 44307441..e40ff980 100644 --- a/src/NextjsBase.ts +++ b/src/NextjsBase.ts @@ -53,10 +53,10 @@ export interface NextjsBaseProps { * Optional value used to install NextJS node dependencies. * It defaults to "npm" */ - readonly packageManager?: "npm" | "yarn" | "pnpm"; + readonly packageManager?: 'npm' | 'yarn' | 'pnpm'; /** - * 0 - no compression, fatest + * 0 - no compression, fastest * 9 - maximum compression, slowest * @default 1 */ diff --git a/src/NextjsBuild.ts b/src/NextjsBuild.ts index 08d76a71..bebc7a08 100644 --- a/src/NextjsBuild.ts +++ b/src/NextjsBuild.ts @@ -133,8 +133,8 @@ export class NextjsBuild extends Construct { }; const buildPath = this.props.buildPath ?? nextjsPath; - const packageManager = this.props.packageManager ?? "npm"; - const buildCommand = packageManager === "yarn" ? ['build'] : ['run', 'build']; + const packageManager = this.props.packageManager ?? 'npm'; + const buildCommand = packageManager === 'yarn' ? ['build'] : ['run', 'build']; // run build console.debug(`├ Running "${packageManager} build" in`, buildPath); const buildResult = spawn.sync(packageManager, buildCommand, { diff --git a/src/NextjsDistribution.ts b/src/NextjsDistribution.ts index 4fff9fd9..caf79e95 100644 --- a/src/NextjsDistribution.ts +++ b/src/NextjsDistribution.ts @@ -113,6 +113,12 @@ export interface NextjsDistributionProps extends NextjsBaseProps { * Required if using SST. */ readonly stageName?: string; + + /** + * Optional value to prefix the edge function stack + * It defaults to "Nextjs" + */ + readonly stackPrefix?: string; } /** @@ -487,16 +493,10 @@ export class NextjsDistribution extends Construct { } private buildDistributionDomainNames(): string[] { - const { customDomain } = this.props; - const domainNames = []; - if (!customDomain) { - // no domain - } else if (typeof customDomain === 'string') { - domainNames.push(customDomain); - } else { - domainNames.push(customDomain.domainName); - } - return domainNames; + const customDomain = + typeof this.props.customDomain === 'string' ? this.props.customDomain : this.props.customDomain?.domainName; + + return customDomain ? [customDomain] : []; } /** @@ -535,7 +535,8 @@ export class NextjsDistribution extends Construct { retryAttempts: 1, // async retry attempts }, stackId: - `Nextjs-${this.props.stageName || app.stageName || 'default'}-EdgeFunctions-` + this.node.addr.substring(0, 5), + `${this.props.stackPrefix ?? 'Nextjs'}-${this.props.stageName || app.stageName || 'default'}-EdgeFn-` + + this.node.addr.substring(0, 5), }); fn.currentVersion.grantInvoke(new ServicePrincipal('edgelambda.amazonaws.com')); fn.currentVersion.grantInvoke(new ServicePrincipal('lambda.amazonaws.com'));