A zero configuration Nextjs 9.0 serverless component with full feature parity.
Since Nextjs 8.0, serverless mode was introduced which provides a new low level API which projects like this can use to deploy onto different cloud providers. This project is a better version of the serverless plugin which focuses on addressing core issues like next 9 support, better development experience, the 200 CloudFormation resource limit and performance.
- Zero configuration by default
There is no configuration needed. You can extend defaults based on your application needs.
- Feature parity with nextjs
Users of this component should be able to use nextjs development tooling, aka next dev
. It is the component's job to deploy your application ensuring parity with all of next's features we know and love.
- Fast deployments / no CloudFormation resource limits.
With a simplified architecture and no use of CloudFormation, there are no limits to how many pages you can have in your application, plus deployment times are very fast! with the exception of CloudFront propagation times of course.
- Server side rendered pages at the Edge. Pages that need server side compute to render are hosted on Lambda@Edge. The component takes care of all the routing for you so there is no configuration needed. Because rendering happens at the CloudFront edge locations latency is very low!
- API Routes. Similarly to the server side rendered pages, API requests are also served from the CloudFront edge locations using Lambda@Edge.
- Dynamic pages / route segments.
- Automatic prerendering. Statically optimised pages compiled by next are served from CloudFront edge locations with low latency and cost.
- Client assets.
Nextjs build assets
/_next/*
served from CloudFront. - User static / public folders. Any of your assets in the static or public folders are uploaded to S3 and served from CloudFront automatically.
Install the next.js component:
npm install serverless-next.js --save-dev
Add your next application to the serverless.yml:
# serverless.yml
myNextApplication:
component: serverless-next.js
Set your aws credentials in a .env
file:
AWS_ACCESS_KEY_ID=accesskey
AWS_SECRET_ACCESS_KEY=sshhh
And simply deploy:
$ serverless
In most cases you wouldn't want to use CloudFront's distribution domain to access your application. Instead, you can specify a custom domain name.
First, make sure you've purchased your domain within Route53. Then simply configure your subdomain
and domain
like the example below.
# serverless.yml
myNextApplication:
component: serverless-next.js
inputs:
domain: ["www", "example.com"] # [ sub-domain, domain ]
Four Cache Behaviours are created in CloudFront.
The first two _next/*
and static/*
forward the requests to S3.
The third is associated to a lambda function which is responsible for handling three types of requests.
-
Server side rendered page. Any page that defines
getInitialProps
method will be rendered at this level and the response is returned immediately to the user. -
Statically optimised page. Requests to pages that were pre-compiled by next to HTML are forwarded to S3.
-
Public resources. Requests to root level resources like
/robots.txt
,/favicon.ico
,/manifest.json
, etc. These are forwarded to S3.
The reason why 2. and 3. have to go through Lambda@Edge first is because the routes don't conform to a pattern like _next/*
or static/*
. Also, one cache behaviour per route is a bad idea because CloudFront only allows 25 per distribution.
The fourth cache behaviour handles next API requests api/*
.
Name | Type | Default Value | Description |
---|---|---|---|
domain | Array |
null |
For example ['admin', 'portal.com'] . |
bucketName | string |
null |
Custom bucket name where static assets are stored. By default is autogenerated. |
nextConfigDir | string |
. |
Directory where your application next.config.js file is. This input is useful when the serverless.yml is not in the same directory as the next app. |
Custom inputs can be configured like this:
myNextApp:
component: serverless-next.js
inputs:
bucketName: my-bucket
Make sure your serverless.yml
uses the serverless-components
format. serverless components differ from the original serverless framework, even though they are both accessible via the same CLI.
✅ Do
# serverless.yml
myNextApp:
component: serverless-next.js
myTable:
component: serverless/aws-dynamodb
inputs:
name: Customers
# other components
❌ Don't
# serverless.yml
provider:
name: aws
runtime: nodejs10.x
region: eu-west-1
myNextApp:
component: serverless-next.js
Resources: ...
Note how the correct yaml doesn't declare a provider
, Resources
, etc.
For deploying, don't run serverless deploy
. Simply run serverless
and that deploys your components declared in the serverless.yml
file.
For more information about serverless components go here.
Should I use the serverless-nextjs-plugin or this component?
Users are encouraged to use this component instead of the serverless-nextjs-plugin
. This component was built and designed using lessons learned from the serverless plugin.
See examples/dynamodb-crud
for an example Todo application that interacts with DynamoDB.
You need to commit your application state in source control. That is the files under the .serverless
directory.
The serverless team is currently working on remote state storage so this won't be necessary in the future.
Serverless next.js is regionless. By design, serverless-next.js
applications will be deployed across the globe to every CloudFront edge location. The lambda might look like is only deployed to us-east-1
but behind the scenes, it is replicated to every other region.
Please see the contributing guide.
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]