Skip to content

Commit

Permalink
more customization
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Oct 10, 2022
1 parent 9b40d91 commit a04b651
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
15 changes: 15 additions & 0 deletions API.md

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

14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ All other required dependencies should be bundled by NextJs [output tracing](htt

## Cold start performance

**Testing with [sst-prisma](https://github.com/jetbridge/sst-prisma):**
#### Testing with [sst-prisma](https://github.com/jetbridge/sst-prisma)

`Duration: 616.43 ms Billed Duration: 617 ms Memory Size: 2048 MB Max Memory Used: 131 MB Init Duration: 481.08 ms`

**On my nextjs app using Material-UI**
#### On my nextjs app using Material-UI

`Duration: 957.56 ms Billed Duration: 958 ms Memory Size: 1024 MB Max Memory Used: 127 MB Init Duration: 530.86 ms`

<img width="1835" alt="next-server-mui" src="https://user-images.githubusercontent.com/245131/191592979-fe83f0a5-7926-4094-be9e-2f9193df5487.png">

## Heavily based on:
## Heavily based on

- https://github.com/iiroj/iiro.fi/commit/bd43222032d0dbb765e1111825f64dbb5db851d9
- https://github.com/sladg/nextjs-lambda
- https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/compat-layers/apigw-lambda-compat
- <https://github.com/iiroj/iiro.fi/commit/bd43222032d0dbb765e1111825f64dbb5db851d9>
- <https://github.com/sladg/nextjs-lambda>
- <https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/compat-layers/apigw-lambda-compat>
- [Serverless Stack](https://github.com/serverless-stack/sst)
- [RemixSite](https://github.com/serverless-stack/sst/blob/master/packages/resources/src/NextjsSite.ts) construct
- [NextjsSite](https://github.com/serverless-stack/sst/blob/master/packages/resources/src/RemixSite.ts) construct
Expand Down Expand Up @@ -126,4 +126,4 @@ class NextjsSst extends Nextjs {

### Edge functions

It should be possible to build the lambda handler as a Lambda@Edge function, the main blocker is resolving the CDK tokens in env vars on the server side because edge functions cannot have environment variables. These tokens are not present at build-time. One of these issues needs to be fixed for that to work most likely: https://github.com/vercel/next.js/issues/40827 https://github.com/aws/aws-cdk/issues/19257
It should be possible to build the lambda handler as a Lambda@Edge function, the main blocker is resolving the CDK tokens in env vars on the server side because edge functions cannot have environment variables. These tokens are not present at build-time. One of these issues needs to be fixed for that to work most likely: <https://github.com/vercel/next.js/issues/40827> <https://github.com/aws/aws-cdk/issues/19257>
20 changes: 16 additions & 4 deletions src/Nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import * as route53Targets from 'aws-cdk-lib/aws-route53-targets';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import * as fs from 'fs-extra';
import { NextJsAssetsDeployment } from './NextjsAssetsDeployment';
import { NextJsAssetsDeployment, NextjsAssetsDeploymentProps } from './NextjsAssetsDeployment';
import {
BaseSiteCdkDistributionProps,
BaseSiteDomainProps,
buildErrorResponsesForRedirectToIndex,
NextjsBaseProps,
} from './NextjsBase';
import { NextjsBuild } from './NextjsBuild';
import { NextJsLambda } from './NextjsLambda';
import { NextJsLambda, NextjsLambdaFunctionProps } from './NextjsLambda';

// contains server-side resolved environment vars in config bucket
// const CONFIG_ENV_JSON_PATH = 'next-env.json';
Expand All @@ -38,20 +38,31 @@ export interface NextjsCachePolicyProps {
* Resources that will be created automatically if not supplied.
*/
export interface NextjsCdkProps {
readonly bucket?: s3.BucketProps | s3.IBucket;
/**
* Pass in a value to override the default settings this construct uses to
* create the CDK `Distribution` internally.
*/
readonly distribution?: NextjsCdkDistributionProps;

/**
* Override the default CloudFront cache policies created internally.
*/
readonly cachePolicies?: NextjsCachePolicyProps;

/**
* Override the default CloudFront image origin request policy created internally
*/
readonly imageOriginRequestPolicy?: cloudfront.IOriginRequestPolicy;

/**
* Override static file deployment settings.
*/
readonly deployment?: NextjsAssetsDeploymentProps;

/**
* Override server lambda function settings.
*/
readonly lambda?: NextjsLambdaFunctionProps;
}

export interface NextjsProps extends NextjsBaseProps {
Expand Down Expand Up @@ -209,9 +220,10 @@ export class Nextjs extends Construct {

// build nextjs app
this.nextBuild = new NextjsBuild(this, id, props);
this.serverFunction = new NextJsLambda(this, 'Fn', { ...props, nextBuild: this.nextBuild });
this.serverFunction = new NextJsLambda(this, 'Fn', { ...props, nextBuild: this.nextBuild, ...props.cdk?.lambda });
this.assetsDeployment = new NextJsAssetsDeployment(this, 'AssetDeployment', {
...props,
...props.cdk?.deployment,
nextBuild: this.nextBuild,
});
this.bucket = this.assetsDeployment.bucket;
Expand Down
14 changes: 10 additions & 4 deletions src/NextjsAssetsDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface NextjsAssetsDeploymentProps extends NextjsBaseProps {
* Distribution to invalidate when assets change.
*/
readonly distribution?: cloudfront.IDistribution;

/**
* Set to true to delete old assets (defaults to false).
* Recommended to only set to true if you don't need the ability to roll back deployments.
*/
readonly prune?: boolean;
}

// interface EnvReplaceValues {
Expand Down Expand Up @@ -80,7 +86,7 @@ export class NextJsAssetsDeployment extends Construct {
destinationKeyPrefix: '_next/static',
sources: [Source.asset(staticDir)],
distribution: this.props.distribution, // invalidate Cloudfront distribution caches
prune: false, // do not delete stale files
prune: this.props.prune,
})
);
}
Expand All @@ -102,7 +108,7 @@ export class NextJsAssetsDeployment extends Construct {
destinationKeyPrefix: this.props.isPlaceholder ? '/placeholder' : '/',
sources: [Source.asset(zipFilePath)],
distribution: this.props.distribution,
prune: false,
prune: this.props.prune,
})
);
}
Expand Down Expand Up @@ -151,14 +157,14 @@ export class NextJsAssetsDeployment extends Construct {
});
// didn't change?
if (bodyPost !== bodyPre)
if (bodyPost === bodyPre)
return;
// upload
console.info('Rewrote', key, 'in bucket', bucket);
const putParams = {
...params,
Body: body,
Body: bodyPost,
ContentType: data.ContentType,
ContentEncoding: data.ContentEncoding,
CacheControl: data.CacheControl,
Expand Down

0 comments on commit a04b651

Please sign in to comment.