Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Serverless component v2 #137

Merged
merged 72 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
603d77d
initial commit
Jul 27, 2019
4d56939
test: aws mocks everywhere
danielcondemarin Jul 27, 2019
de5810e
test: use deploy tests
danielcondemarin Jul 28, 2019
890ad28
test: add .next/static folder to fixtures
danielcondemarin Jul 28, 2019
6758338
test: move aws-sdk mocking to serverless test script
danielcondemarin Jul 28, 2019
6f05650
test: increase test timeout and minor refactor
danielcondemarin Jul 28, 2019
5d363bb
build: update travis node versions
danielcondemarin Aug 3, 2019
ac10bfb
test: still working on getting the s3 upload spy working
danielcondemarin Aug 11, 2019
e24cf11
initial commit lambda@edge router
danielcondemarin Aug 15, 2019
520375e
initial commit for ssr mini router
danielcondemarin Aug 15, 2019
ea41b1a
update manifest fixture
danielcondemarin Aug 15, 2019
df765c0
ignore manifest fixtures to prevent breaking regexes
danielcondemarin Aug 15, 2019
d912b7b
initial commit serverless next component
danielcondemarin Aug 16, 2019
d1c7dfe
working on build manifest
danielcondemarin Aug 16, 2019
c605e1c
adding integration with aws api gateway component
danielcondemarin Aug 16, 2019
f41aa54
adding api gateway and lambda components
danielcondemarin Aug 18, 2019
ab0ca08
trying @serverless/backend component instead
danielcondemarin Aug 18, 2019
575a258
adding ssr lambda handler
danielcondemarin Aug 18, 2019
9714e38
move router to component repo
danielcondemarin Aug 18, 2019
0c7c717
fix order of operations and add next-aws-lambda dep
danielcondemarin Aug 18, 2019
d5ce95e
test: consolidate router and ssr handler tests in one suite
danielcondemarin Aug 19, 2019
5f57e18
delete separate mini router package
danielcondemarin Aug 19, 2019
15cf74b
docs(serverless-component): add initial README
danielcondemarin Aug 20, 2019
3f2b208
docs(serverless-component): add initial README
danielcondemarin Aug 20, 2019
c88a2d4
docs(serverless-component): add design principles
danielcondemarin Aug 20, 2019
e097e39
docs(serverless-component): add badges
danielcondemarin Aug 20, 2019
ac5ad37
docs(serverless-component): fix link and lambda@edge section
danielcondemarin Aug 20, 2019
dea7687
docs(serverless-component): fix link and lambda@edge section
danielcondemarin Aug 20, 2019
d3d90cf
docs(serverless-component): fix link again
danielcondemarin Aug 20, 2019
e7d334b
add most recent next and react dev deps
danielcondemarin Aug 21, 2019
9d52be3
render _error page when path is not matched to any page
danielcondemarin Aug 21, 2019
2f9f896
adds support for ssr api to handler html as well
danielcondemarin Aug 21, 2019
2c2c990
fix build tests
danielcondemarin Aug 27, 2019
f463267
ssr handler now is async, yaygit add .
danielcondemarin Aug 27, 2019
6949df9
use aws-s3 to upload assets
danielcondemarin Aug 27, 2019
11a9ed8
remove bucket on remove()
danielcondemarin Aug 27, 2019
8f67704
initial commit lambda_at_edge compat layer
Aug 28, 2019
7d83039
working on the response aspect of the next-aws-cloudfront
Aug 29, 2019
418ad1d
working on cloudfront cache behaviours
danielcondemarin Aug 31, 2019
4d5645f
Merge branch 'serverless-component' of https://github.com/danielconde…
danielcondemarin Aug 31, 2019
7c2750d
lambda@edge router now working
danielcondemarin Sep 1, 2019
abf3e32
Merge branch 'master' of https://github.com/danielcondemarin/serverle…
danielcondemarin Sep 2, 2019
aa37260
lambda@edge ssr optimisation almost complete
danielcondemarin Sep 2, 2019
3a6e85f
draft commit to get lambda@edge ssr working
danielcondemarin Sep 2, 2019
71199ea
docs: update README
danielcondemarin Sep 2, 2019
1a82287
docs: minor fix
danielcondemarin Sep 2, 2019
de29b52
fix special cloudfront headers
danielcondemarin Sep 3, 2019
0061450
remove api gateway
danielcondemarin Sep 3, 2019
41a8b61
fix page require
danielcondemarin Sep 3, 2019
56c943a
handle api requests
danielcondemarin Sep 4, 2019
f606908
build next using separate process with execa (more reliable)
danielcondemarin Sep 4, 2019
a611f6d
update docs, arch and pass allowedhttpmethods
danielcondemarin Sep 4, 2019
cff76a0
update output url
danielcondemarin Sep 4, 2019
8437497
update docs content
danielcondemarin Sep 4, 2019
baddfec
add API Routes support to docs
danielcondemarin Sep 4, 2019
0977308
add dynamodb example
danielcondemarin Sep 4, 2019
ff31bf4
minor amend
danielcondemarin Sep 4, 2019
45a02e0
ignore DS_Store
Sep 5, 2019
0889427
add fake aws creds to dev server
danielcondemarin Sep 6, 2019
4bcf180
update example with global dynamodb setup
danielcondemarin Sep 6, 2019
3d74563
remove unnecesary call
danielcondemarin Sep 6, 2019
9ed16be
add FAQ
danielcondemarin Sep 6, 2019
466cb40
fix tests
danielcondemarin Sep 6, 2019
f988e90
npm install serverless component on travis
danielcondemarin Sep 6, 2019
4b74b5b
update example docs
danielcondemarin Sep 6, 2019
4dc88d0
update example docs
danielcondemarin Sep 6, 2019
3d00679
Merge branch 'serverless-component-v2' of https://github.com/danielco…
danielcondemarin Sep 6, 2019
2f646e7
update component deps
danielcondemarin Sep 6, 2019
bf5fe1e
skip failing test temporarily
danielcondemarin Sep 6, 2019
c710141
update root deps
danielcondemarin Sep 6, 2019
2a65f2b
replace deploy with package plugin tests
danielcondemarin Sep 6, 2019
94a4b0f
add missing test for 404 page
danielcondemarin Sep 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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