From e16a15396f5486e4d49ca478b9dc1b98cb6f6c85 Mon Sep 17 00:00:00 2001 From: danielconde Date: Tue, 30 Apr 2019 21:29:48 +0100 Subject: [PATCH] chore: add example for custom lambda handler --- .../app-with-custom-lambda-handler/README.md | 29 +++++++++++++++++ .../my-lambda-handler.js | 14 +++++++++ .../next.config.js | 3 ++ .../package.json | 31 +++++++++++++++++++ .../pages/home.js | 7 +++++ .../serverless.yml | 23 ++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 examples/app-with-custom-lambda-handler/README.md create mode 100644 examples/app-with-custom-lambda-handler/my-lambda-handler.js create mode 100644 examples/app-with-custom-lambda-handler/next.config.js create mode 100644 examples/app-with-custom-lambda-handler/package.json create mode 100644 examples/app-with-custom-lambda-handler/pages/home.js create mode 100644 examples/app-with-custom-lambda-handler/serverless.yml diff --git a/examples/app-with-custom-lambda-handler/README.md b/examples/app-with-custom-lambda-handler/README.md new file mode 100644 index 0000000000..34199fcc06 --- /dev/null +++ b/examples/app-with-custom-lambda-handler/README.md @@ -0,0 +1,29 @@ +## app-with-custom-lambda-handler + +### Running the example + +```shell +git clone https://github.com/danielcondemarin/serverless-nextjs-plugin +cd serverless-nextjs-plugin/examples/app-with-custom-lambda-handler +``` + +#### Install dependencies + +```shell +npm install +``` + +_next.config.js_ + +```js +module.exports = { + ... + target: "serverless" +}; +``` + +#### Deploy + +`serverless deploy` + +After deployment is finished, visit the `/home` page, go to CloudWatch and check the LogGroup for the HomePage lambda to see the extra logging added by `my-lambda-handler.js` diff --git a/examples/app-with-custom-lambda-handler/my-lambda-handler.js b/examples/app-with-custom-lambda-handler/my-lambda-handler.js new file mode 100644 index 0000000000..b636d03db3 --- /dev/null +++ b/examples/app-with-custom-lambda-handler/my-lambda-handler.js @@ -0,0 +1,14 @@ +const compat = require("serverless-nextjs-plugin/aws-lambda-compat"); + +module.exports = page => { + const handler = (event, context, callback) => { + // let's add some logging + console.log("URL: ", event.path); + + // render page + compat(page)(event, context, callback); + + console.log("This is cool (☞゚∀゚)☞"); + }; + return handler; +}; diff --git a/examples/app-with-custom-lambda-handler/next.config.js b/examples/app-with-custom-lambda-handler/next.config.js new file mode 100644 index 0000000000..174c51be68 --- /dev/null +++ b/examples/app-with-custom-lambda-handler/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + target: "serverless" +}; diff --git a/examples/app-with-custom-lambda-handler/package.json b/examples/app-with-custom-lambda-handler/package.json new file mode 100644 index 0000000000..870219b564 --- /dev/null +++ b/examples/app-with-custom-lambda-handler/package.json @@ -0,0 +1,31 @@ +{ + "name": "app-with-custom-lambda-handler", + "version": "1.0.0", + "description": "Example nextjs serverless app using a custom lambda handler", + "main": "index.js", + "repository": { + "type": "git", + "url": "git+https://github.com/danielcondemarin/serverless-nextjs-plugin.git" + }, + "keywords": [ + "serverless", + "nextjs", + "lambda" + ], + "author": "Daniel Conde Marin ", + "license": "ISC", + "bugs": { + "url": "https://github.com/danielcondemarin/serverless-nextjs-plugin/issues" + }, + "homepage": "https://github.com/danielcondemarin/serverless-nextjs-plugin#readme", + "dependencies": { + "next": "^8.0.3", + "react": "^16.8.4", + "react-dom": "^16.8.4" + }, + "devDependencies": { + "eslint-plugin-react": "^7.12.4", + "serverless": "^1.39.1", + "serverless-nextjs-plugin": "^1.2.0" + } +} diff --git a/examples/app-with-custom-lambda-handler/pages/home.js b/examples/app-with-custom-lambda-handler/pages/home.js new file mode 100644 index 0000000000..d25b7a77fd --- /dev/null +++ b/examples/app-with-custom-lambda-handler/pages/home.js @@ -0,0 +1,7 @@ +import React from "react"; + +function Index() { + return
Welcome to next.js serverless ⚡
; +} + +export default Index; diff --git a/examples/app-with-custom-lambda-handler/serverless.yml b/examples/app-with-custom-lambda-handler/serverless.yml new file mode 100644 index 0000000000..8ced378c64 --- /dev/null +++ b/examples/app-with-custom-lambda-handler/serverless.yml @@ -0,0 +1,23 @@ +service: app-with-custom-handler + +provider: + name: aws + runtime: nodejs8.10 + memorySize: 512 + +stage: dev +region: eu-west-1 + +plugins: + - serverless-nextjs + +custom: + serverless-nextjs: + nextConfigDir: ./ + customHandler: ./my-lambda-handler.js + +package: + # exclude everything + # page handlers are automatically included by the plugin + exclude: + - ./**