Open source Next.js serverless adapter

Star us on GitHubView README

OpenNext takes the Next.js build output and converts it into a package that can be deployed to any functions as a service platform.

The goal is to support all Next.js 13 features including, SSG, ISR, SSR, Image Optimization, and Middleware.

OpenNext is what allows SST to deploy Next.js apps to AWS using Lambda, Lambda@Edge, CloudFront, and S3.

$ npx create-next-app
$ npx create-sst
$ npx sst deploy

Deploy your Next.js app to AWS with SST.

While Vercel is great, it's not a good option if all your infrastructure is on AWS. Hosting it in your AWS account makes it easy to integrate with your backend. And it's a lot cheaper than Vercel.

Next.js, unlike Remix or Astro, doesn't have a way to self-host using serverless. You can run it as a Node application. This however doesn't work the same way as it does on Vercel.

There have been several attempts to fix this. Broadly falling into two categories; open source framework specific implementations, or closed source SaaS products.

Most of the open source options, like serverless-nextjs are dead. Their implementation can also be specific to the framework they are targeting.

Closed source SaaS products like Amplify have incomplete or incorrect implementations. And because they are closed source, you'll need to file a support ticket to get them updated.

The goal of OpenNext is to create an open source, framework agnostic, serverless adapter for Next.js.

OpenNext is currently built for AWS but we plan to support any functions as a service platform.

We need your help keeping it up to date and feature complete. Make sure to join us on Discord and star us on GitHub.

Background

Modern SSR applications like Next.js are made up of two parts.

  1. The infrastructure necessary to run your app. Typically this includes things like serverless functions, an S3 bucket, a CDN, and serverless edge functions.

  2. The code, called the adapter, that wraps around your Next.js app and translates function requests and responses to your app.

OpenNext is the adapter.

Problem

In the past, library authors have ended up duplicating the adapter implementation across various projects. Meaning that they also need to upgrade them separately to support new Next.js features. This is hard to do. And not surprisingly these efforts tend to fizzle out.

It makes more sense to put together a community effort to maintain a single, feature complete, implementation that can be used across projects.

Usage

OpenNext is meant to be used by library authors and currently only supports AWS. Here's what you'll need to implement in your library to deploy a serverless Next.js app to AWS.

  1. Run open-next build to generate the .open-next directory.

    This contains a directory for your app that can be either run in a Lambda function or Lambda@Edge, a directory for an image optimization Lambda function, and another directory for your static assets that'll be uploaded to S3.

  2. Create the infrastructure for your app with your preferred tool — SST, CDK, Serverless Framework, Terraform, etc. SST has the NextjsSite construct for this. You can look at the source of the SST construct to see what infrastructure you need. We'll document this in detail soon.

  3. Finally, upload the generated assets in step 1 to the infrastructure you created.

Note that, if you are not a library author and are just looking for a way to deploy your serverless Next.js app to AWS, follow this tutorial.