Deploy a static export Next.js site to Cloudfront and S3 while maintaining the ability to use dynamic routes.
Project description
Next.js Static Export S3 Site with Dynamic Routing
Have a more complex use case for Next.js 13? Perhaps check out cdk-nextjs-standalone.
Deploy a static export Next.js site to Cloudfront and S3 while maintaining the ability to use dynamic routes.
This effectively takes all of the benefits of Next.js, including routing, code-splitting, static HTML exporting, and also gives you the benefits of a SPA (single page application). This will be mostly useful for client-generated pages, but you can also partially server side generate some data should you choose.
This may also be useful if you use Next.js SSR in other frontends, and just want to keep a consistent experience for your developers.
Getting Started
You can use this construct effectively with no props, but here is a minimal example with a custom domain:
export class MyStaticSiteStack extends cdk.Stack {
private readonly hostedZone: route53.IHostedZone;
private readonly customDomainName: string;
private readonly site: NextjsExportS3DynamicRoutingSite;
constructor(scope: sst.App, id: string, props?: sst.StackProps) {
super(scope, id, props);
this.hostedZone = this.findHostedZone();
this.customDomainName = "yourdomain.com";
this.site = this.createNextJsSite();
this.createDnsRecord();
}
private findHostedZone(): route53.IHostedZone {
return route53.HostedZone.fromLookup(this, "HostedZone", {
domainName: this.customDomainName,
});
}
private createNextJsSite(): NextjsExportS3DynamicRoutingSite {
const certificate = new acm.Certificate(
this,
"Certificate",
{
domainName: this.customDomainName,
validation: acm.CertificateValidation.fromDns(this.hostedZone),
}
);
return new NextjsExportS3DynamicRoutingSite(this, "NextJsSite", {
distributionProps: {
certificate,
domainNames: [this.customDomainName],
}
});
}
private createDnsRecord = (): route53.ARecord => {
return new route53.ARecord(this, `AliasRecord`, {
recordName: this.customDomainName,
target: route53.RecordTarget.fromAlias(
new r53Targets.CloudFrontTarget(
this.site.cloudfrontDistribution
)
),
zone: this.hostedZone,
});
};
}
There are no outside requirements for this construct, and it will delete all of its resources when the stack is deleted.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cdk-nextjs-export-s3-dynamic-routing-1.2.6.tar.gz
.
File metadata
- Download URL: cdk-nextjs-export-s3-dynamic-routing-1.2.6.tar.gz
- Upload date:
- Size: 312.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 082e1ee4c9afa3d73054b65de1ea0f734ac378b64dcc01e0362c717508821014 |
|
MD5 | 9e733236df69f7d53f575ddbce39d9af |
|
BLAKE2b-256 | 6e74cba01718dc07fa4e8b813de357386ec6bfe48e8c83e2e6e374d101be7b96 |
File details
Details for the file cdk_nextjs_export_s3_dynamic_routing-1.2.6-py3-none-any.whl
.
File metadata
- Download URL: cdk_nextjs_export_s3_dynamic_routing-1.2.6-py3-none-any.whl
- Upload date:
- Size: 310.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aeb6980edca0f108ad3f86cbbcfcfd0f90621e33a6c76c345eaf96ef824f47f3 |
|
MD5 | 2d6a5647b6ac00627db3e5c7de49b09d |
|
BLAKE2b-256 | 9e76e28f4a24434a0ac9dff4a01845ab75adff8cd52325ebc247374701dca355 |