Skip to main content

Next.js Static Export S3 Site with Dynamic Routing

NPM

View on Construct Hub

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.

Metadata

Release files for cdk-nextjs-export-s3-dynamic-routing 1.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdk-nextjs-export-s3-dynamic-routing 1.2.6
File Size Uploaded
cdk-nextjs-export-s3-dynamic-routing-1.2.6.tar.gz 312.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdk-nextjs-export-s3-dynamic-routing 1.2.6
File Interpreter ABI Platform
cdk_nextjs_export_s3_dynamic_routing-1.2.6-py3-none-any.whl Python 3 none any Details

Total release size: 623.6 kB

Release files / cdk-nextjs-export-s3-dynamic-routing-1.2.6.tar.gz

Download URL cdk-nextjs-export-s3-dynamic-routing-1.2.6.tar.gz
Size 312.7 kB
Tags Source
SHA-256 checksum
How to use checksums
082e1ee4c9afa3d73054b65de1ea0f734ac378b64dcc01e0362c717508821014
BLAKE2b-256 checksum
How to use checksums
6e74cba01718dc07fa4e8b813de357386ec6bfe48e8c83e2e6e374d101be7b96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release files / cdk_nextjs_export_s3_dynamic_routing-1.2.6-py3-none-any.whl

Download URL cdk_nextjs_export_s3_dynamic_routing-1.2.6-py3-none-any.whl
Size 310.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aeb6980edca0f108ad3f86cbbcfcfd0f90621e33a6c76c345eaf96ef824f47f3
BLAKE2b-256 checksum
How to use checksums
9e76e28f4a24434a0ac9dff4a01845ab75adff8cd52325ebc247374701dca355
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release history Release notifications | RSS feed

This release

1.2.6 This release

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.6

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page