Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basePath and allow multiple next.js sites on same distribution (passing custom distribution) #148

Merged
merged 11 commits into from
Oct 23, 2023
1 change: 1 addition & 0 deletions .gitignore

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

1 change: 1 addition & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
projenrcTs: true,
tsconfig: { compilerOptions: { ...commonTscOptions } },
tsconfigDev: { compilerOptions: { ...commonTscOptions } },
gitignore: ['.idea'],
// dependency config
jsiiVersion: '~5.0.0',
cdkVersion: '2.93.0',
Expand Down
248 changes: 236 additions & 12 deletions API.md

Large diffs are not rendered by default.

42 changes: 9 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,15 @@ new WebStack(app, 'web');

Available on [Construct Hub](https://constructs.dev/packages/cdk-nextjs-standalone/).

## Customization

### Increased Security
```ts
import { RemovalPolicy, Stack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { CfnWebAcl } from "aws-cdk-lib/aws-wafv2";
import { SecurityPolicyProtocol, type DistributionProps } from "aws-cdk-lib/aws-cloudfront";
import { Nextjs, type NextjsDistributionProps } from "cdk-nextjs-standalone";
import { Bucket, BlockPublicAccess, BucketEncryption } from "aws-cdk-lib/aws-s3";

// Because of `WebAcl`, this stack must be deployed in us-east-1. If you want
// to deploy Nextjs in another region, add WAF in separate stack deployed in us-east-1
export class UiStack {
constructor(scope: Construct, id: string) {
const webAcl = new CfnWebAcl(this, "WebAcl", { ... });
new Nextjs(this, "NextSite", {
nextjsPath: "...",
defaults: {
distribution: {
functionUrlAuthType: FunctionUrlAuthType.AWS_IAM,
cdk: {
distribution: {
webAclId: webAcl.attrArn,
minimumProtocolVersion: SecurityPolicyProtocol.TLS_V1_2_2021,
} as DistributionProps,
},
} satisfies Partial<NextjsDistributionProps>,
},
});
}
}
```
## Examples

See example CDK apps [here](./examples) including:
- App Router
- Pages Router
- App/Pages Router
- High Security
- Multiple Sites
To deploy an example, make sure to read the [README.md](./examples/README.md)

### Discord Chat

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Each example app utilizes [open-next](https://github.com/sst/open-next)'s exampl
## Prerequisites
1. `git clone https://github.com/jetbridge/cdk-nextjs.git`
1. `yarn install`
1. `yarn build` (or faster option: `yarn compile`)
1. `yarn build`

## Setup Example Next.js Apps
After cloning this repository in order to run the example apps or e2e tests, run:
Expand Down
8 changes: 8 additions & 0 deletions examples/multiple-sites/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out

!tsconfig.json
5 changes: 5 additions & 0 deletions examples/multiple-sites/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Multiple Sites Example

NOTE: in order for this example to work, you need to make the following manual updates:
- Update ../../open-next/examples/app-router/next.config.js to have a `basePath: "/app-router"` and `assetPrefix: "/app-router"`
- Update ../../open-next/examples/pages-router/next.config.js to have a `basePath: "/pages-router"` and `assetPrefix: "/app-router"`
60 changes: 60 additions & 0 deletions examples/multiple-sites/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"app": "pnpm tsx src/app.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
],
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
"@aws-cdk/aws-redshift:columnId": true,
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
"@aws-cdk/aws-kms:aliasNameRef": true,
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true
}
}
21 changes: 21 additions & 0 deletions examples/multiple-sites/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "cdk-nextjs-standalone-example-multiple-sites",
"version": "0.1.0",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"@types/node": "20.5.7",
"aws-cdk": "2.94.0",
"esbuild": "^0.19.3",
"tsx": "^3.12.10",
"typescript": "~5.2.2"
},
"dependencies": {
"aws-cdk-lib": "2.94.0",
"cdk-nextjs-standalone": "link:../..",
"constructs": "^10.0.0"
}
}
Loading
Loading