Skip to content

Commit

Permalink
Serverless component v2 (#137)
Browse files Browse the repository at this point in the history
* add serverless-nextjs-component
  • Loading branch information
danielcondemarin authored Sep 6, 2019
1 parent 57d1139 commit 94b6b55
Show file tree
Hide file tree
Showing 79 changed files with 2,299 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ examples/basic-next-serverless-app/build
.serverless
.next
sls-next-build
package-lock.json
.serverless_nextjs
package-lock.json
.DS_Store
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manifest.json
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ os:
- osx
language: node_js
node_js:
- "8"
- "11.10"
- "10.16"
- "12.7.0"
install:
- npm install
- cd packages/serverless-nextjs-plugin && npm install && cd ../../
- cd packages/serverless-nextjs-component && npm install && cd ../../
- cd integration/app-with-serverless-offline && npm install && cd ../../
script:
- npm run lint
Expand Down
2 changes: 1 addition & 1 deletion jest.integration.setup.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
jest.setTimeout(35000);
jest.setTimeout(35 * 1000);
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(10 * 1000);
29 changes: 20 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint .",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"integration": "jest --config jest.integration.config.json --setupTestFrameworkScriptFile=./jest.integration.setup.js",
Expand All @@ -32,18 +33,18 @@
"homepage": "https://github.com/danielcondemarin/serverless-nextjs-plugin#readme",
"devDependencies": {
"adm-zip": "^0.4.13",
"coveralls": "^3.0.3",
"coveralls": "^3.0.6",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.8.0",
"jest-when": "^2.5.0",
"jest": "^24.9.0",
"jest-when": "^2.7.0",
"lerna": "^3.13.4",
"next": "^8.1.0",
"prettier": "^1.17.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"serverless": "^1.42.3",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"serverless": "^1.51.0",
"serverless-offline": "^4.10.0"
},
"jest": {
Expand All @@ -55,21 +56,31 @@
"coverageDirectory": "<rootDir>/coverage/",
"coveragePathIgnorePatterns": [
"<rootDir>/packages/serverless-nextjs-plugin/utils/yml/cfSchema.js",
"<rootDir>/packages/serverless-nextjs-plugin/utils/test"
"<rootDir>/packages/serverless-nextjs-plugin/utils/test",
"/.serverless_nextjs/",
"/fixtures/",
"/examples/"
],
"testPathIgnorePatterns": [
"/.next/",
"/node_modules/"
"/node_modules/",
"/fixtures/"
],
"modulePathIgnorePatterns": [
"<rootDir>/examples/",
"/examples/",
"<rootDir>/integration"
],
"modulePaths": [
"<rootDir>/packages/serverless-nextjs-plugin/"
],
"setupFiles": [
"<rootDir>/jest.setup.js"
]
},
"dependencies": {
"@serverless/aws-cloudfront": "^3.1.0",
"@serverless/aws-lambda": "^3.0.0",
"@serverless/aws-s3": "^3.1.0",
"opencollective-postinstall": "^2.0.2"
},
"collective": {
Expand Down
2 changes: 2 additions & 0 deletions packages/serverless-nextjs-component/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.serverless_nextjs
!__tests__/fixtures/simple-app/.next
107 changes: 107 additions & 0 deletions packages/serverless-nextjs-component/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Serverless Nextjs Component

A zero configuration Nextjs 9.0 [serverless component](https://github.com/serverless-components/) with full feature parity.

[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)
[![Build Status](https://travis-ci.org/danielcondemarin/serverless-nextjs-plugin.svg?branch=master)](https://travis-ci.org/danielcondemarin/serverless-nextjs-plugin)
[![Financial Contributors on Open Collective](https://opencollective.com/serverless-nextjs-plugin/all/badge.svg?label=financial+contributors)](https://opencollective.com/serverless-nextjs-plugin) [![npm version](https://badge.fury.io/js/serverless-nextjs-plugin.svg)](https://badge.fury.io/js/serverless-nextjs-plugin)
[![Coverage Status](https://coveralls.io/repos/github/danielcondemarin/serverless-nextjs-plugin/badge.svg?branch=master)](https://coveralls.io/github/danielcondemarin/serverless-nextjs-plugin?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/c0d3aa2a86cb4ce98772a02015f46314)](https://www.codacy.com/app/danielcondemarin/serverless-nextjs-plugin?utm_source=github.com&utm_medium=referral&utm_content=danielcondemarin/serverless-nextjs-plugin&utm_campaign=Badge_Grade)

## Contents

- [Motivation](#motivation)
- [Design principles](#design-principles)
- [Features](#features)
- [Getting started](#getting-started)
- [Custom domain name](#custom-domain-name)
- [Architecture](#architecture)
- [FAQ](#faq)

### Motivation

Since Nextjs 8.0, [serverless mode](https://nextjs.org/blog/next-8#serverless-nextjs) 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](https://github.com/danielcondemarin/serverless-nextjs-plugin) which focuses on addressing core issues like [next 9 support](https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/101), [better development experience](https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/59), [the 200 CloudFormation resource limit](https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/17) and [performance](https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/13).

### Design principles

1. Zero configuration by default

There is no configuration needed. You can extend defaults based on your application needs.

2. 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 feature we know and love.

3. 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.

### Features

- [x] [Server side rendered pages at the Edge](https://github.com/zeit/next.js#fetching-data-and-component-lifecycle).
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!
- [x] [API Routes](https://nextjs.org/docs#api-routes).
Similarly to the server side rendered pages, API requests are also served from the CloudFront edge locations using Lambda@Edge.
- [x] [Dynamic pages / route segments](https://github.com/zeit/next.js/#dynamic-routing).
- [x] [Automatic prerendering](https://github.com/zeit/next.js/#automatic-prerendering).
Statically optimised pages compiled by next are served from CloudFront edge locations with low latency and cost.
- [x] [Client assets](https://github.com/zeit/next.js/#cdn-support-with-asset-prefix).
Nextjs build assets `/_next/*` served from CloudFront.
- [x] [User static / public folders](https://github.com/zeit/next.js#static-file-serving-eg-images).
Any of your assets in the static or public folders are uploaded to S3 and served from CloudFront automatically.

### Getting started

Add your next application to the serverless.yml:

```yml
# serverless.yml

myNextApplication:
component: @serverless/nextjs
```
And simply deploy:
```bash
$ serverless
```

### Custom domain name (Coming soon!)

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:

```yml
# serverless.yml

myNextApplication:
component: @serverless/nextjs
inputs:
domain: myfrontend.example.com
```
### Architecture
The application architecture deployed by the component is the following with minor variations:
![architecture](./arch.png)
### FAQ
#### Is it one monolith Lambda or one Lambda per serverless page?
Only one Lambda is provisioned. There are a few reasons why:
- Simplicity. One lambda responsible for server side rendering or serving the API requests is very easy to manage. On the other hand, one lambda per page is a large surface area for a web app. For example a next application with 40+ pages would have resulted in 40+ lambda functions to maintain.
- Deployment speed. Is much faster building and deploying one lambda function.
Of course there are tradeoffs ... An architecture using one lambda per page in theory results in lower boot times. However, the implementation of this component is designed to ensure a minimum amount of compute happens at the Lambda@Edge.
#### How do I interact with other AWS Services within my app?
See `examples/dynamodb-crud` for an example Todo application that interacts with DynamoDB.

#### Should I use the [serverless-nextjs-plugin](https://github.com/danielcondemarin/serverless-nextjs-plugin/tree/master/packages/serverless-nextjs-plugin) or this component?

Users are enocouraged to use this component instead of the `serverless-nextjs-plugin`. This component was built and designed to fix issues the plugin has like the [CloudFormation resource limit](https://github.com/danielcondemarin/serverless-nextjs-plugin/issues/17).
Loading

0 comments on commit 94b6b55

Please sign in to comment.