Skip to content
⚡ Serverless Framework Plugin for Nextjs 8
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__
classes
examples/basic-next-serverless-app
integration feat: add support for custom page routes Mar 16, 2019
lib feat: add support for custom page routes Mar 16, 2019
utils
.eslintignore
.eslintrc.json
.gitignore
.travis.yml test: add longer timeout to integration tests Mar 13, 2019
README.md docs: less redundant Mar 16, 2019
demo.gif docs: adding back in the demo.gif Mar 10, 2019
index.js refactor: declaring page functions in serveless under functions: is n… Mar 17, 2019
jest.integration.config.json
jest.integration.setup.js
package.json 1.2.0 Mar 16, 2019
resources.yml feat: replace bucketname in cf resource with plugin provided Feb 24, 2019

README.md

Serverless Nextjs Plugin

serverless Build Status npm version Coverage Status Codacy Badge

A serverless framework plugin to deploy nextjs apps.

The plugin targets Next 8 serverless mode

demo

Contents

Motivation

Next 8 released official support for serverless! It doesn't work out of the box with AWS Lambdas, instead, next provides a low level API which this plugin uses to deploy the serverless pages.

Nextjs serverless page handler signature:

exports.render = function(req, res) => {...}

AWS Lambda handler:

exports.handler = function(event, context, callback) {...}

A compat layer between the nextjs page bundles and AWS Lambda is added at build time:

const page = require(".next/serverless/pages/somePage.js");

module.exports.render = (event, context, callback) => {
  const { req, res } = compatLayer(event, callback);
  page.render(req, res);
};

Getting started

Installing

npm install --save-dev serverless-nextjs-plugin

The plugin only needs to know where your next.config.js file is located. Using your next configuration it will automatically build the application and compile the pages using the target: serverless.

Note it expects nextConfigDir to be a directory and not the actual file path.

nextApp
│   next.config.js
│   serverless.yml
└───pages
│   │   home.js
│   │   about.js
│   │   admin.js

Edit the serverless.yml and add:

plugins:
  - serverless-nextjs-plugin

custom:
  serverless-nextjs:
    nextConfigDir: "./"

package:
  exclude:
    - ./**/*

You can exclude everything. The plugin makes sure the page handlers are included in the artifacts.

Next configuration

module.exports = {
  assetPrefix: "https://s3.amazonaws.com/your-bucket-name"
};
Config Key Description
assetPrefix (Optional) When using a valid bucket URL the plugin will create a new S3 Bucket using the parsed name. On deployment, static assets will be uploaded to the bucket provisioned.

Deploying

serverless deploy

When running serverless deploy all your next pages will be automatically compiled, packaged and deployed.

The Lambda functions created for each page have by default the following configuration:

handler: /path/to/page/handler.render
events:
  - http:
      path: pageName # home, about, etc. Unless is the index page which is served at /
      method: get

If you need to change the default configuration, such as memorySize, timeout etc. use the top level provider which will override the functions configuration. For example, to change the memorySize to 512MB:

provider:
  name: aws
  runtime: nodejs8.10
  memorySize: 512
  ...

See this for more information.

Deploying a single page

If you need to deploy just one of your pages, simply run:

serverless deploy function --function pageFunctionName

where pageFunctionName will be the page file name + "Page". For example, to deploy pages/home.js, you can run:

serverless deploy function --function homePage

Overriding page configuration

You may want to have a different configuration for one or more of your page functions. This is possible by setting the pageConfig key in the plugin config:

plugins:
  - serverless-nextjs-plugin

custom:
  serverless-nextjs:
    nextConfigDir: ./
    pageConfig:
      about:
        memorySize: 512 # default is 1024
      home:
        timeout: 10 # default is 6

The example above will deploy the about page function with a smaller memorySize and the home page with a higher timeout than the default values.

You can set any function property described here. The values provided will be merged onto the plugin defaults.

Custom page routing

The default page route is /{pageName}. You may want to serve your page from a different path. This is possible by setting your own http path in the pageConfig. For example for pages/post.js:

class Post extends React.Component {
  static async getInitialProps({ query }) {
    return {
      slug: query.slug
    };
  }
  render() {
    return <h1>Post page: {this.props.slug}</h1>;
  }
}

export default Post;
plugins:
  - serverless-nextjs-plugin

custom:
  serverless-nextjs:
    nextConfigDir: ./
    pageConfig:
      post:
        events:
          - http:
              path: post/{slug}
              request:
                parameters:
                  paths:
                    slug: true

Examples

See the examples/ directory.

You can’t perform that action at this time.