Skip to content

Commit

Permalink
chore: add example for custom lambda handler
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcondemarin committed Apr 30, 2019
1 parent 77b206d commit e16a153
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/app-with-custom-lambda-handler/README.md
Original file line number Diff line number Diff line change
@@ -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`
14 changes: 14 additions & 0 deletions examples/app-with-custom-lambda-handler/my-lambda-handler.js
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 3 additions & 0 deletions examples/app-with-custom-lambda-handler/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
target: "serverless"
};
31 changes: 31 additions & 0 deletions examples/app-with-custom-lambda-handler/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}
7 changes: 7 additions & 0 deletions examples/app-with-custom-lambda-handler/pages/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

function Index() {
return <div>Welcome to next.js serverless ⚡</div>;
}

export default Index;
23 changes: 23 additions & 0 deletions examples/app-with-custom-lambda-handler/serverless.yml
Original file line number Diff line number Diff line change
@@ -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:
- ./**

0 comments on commit e16a153

Please sign in to comment.