Skip to content

Commit d005ee3

Browse files
author
sw-yx
committed
document timout and node-fetch
1 parent 520b908 commit d005ee3

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Diff for: README.md

+22-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The goal is to make it easy to write Lambda's with transpiled JS/TypeScript feat
66

77
## New Alternative to Netlify Lambda
88

9-
Netlify-Lambda uses webpack to bundle up your functions and their dependencies for you, however this is not the only approach. If you have native node modules (or other dependencies that dont expect to be bundled like [the Firebase SDK](https://github.com/netlify/netlify-lambda/issues/112)) then you may want to try the zipping approach.
9+
Netlify-Lambda uses webpack to bundle up your functions and their dependencies for you, however this is not the only approach. If you have native node modules (or other dependencies that dont expect to be bundled like [the Firebase SDK](https://github.com/netlify/netlify-lambda/issues/112)) then you may want to try the zipping approach.
1010

1111
We have recently soft released a new library to do this for you: https://github.com/netlify/zip-it-and-ship-it. This is integrated into the Netlify CLI. There is [more documentation here](https://www.netlify.com/docs/cli/#unbundled-javascript-function-deploys) in the official CLI docs and support is available through [our regular channels](https://www.netlify.com/support/).
1212

@@ -64,6 +64,7 @@ It also watches your files and restarts the dev server on change. Note: if you a
6464
- You need a [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/) file with a `functions` field.
6565
- Every function needs to be a top-level js/ts/mjs file. You can have subfolders inside the `netlify-lambda` folder, but those are only for supporting files to be imported by your top level function. Files that end with `.spec.*` or `.test.*` will be ignored so you can [colocate your tests](https://github.com/netlify/netlify-lambda/issues/99).
6666
- Function signatures follow the [AWS event handler](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) syntax but must be named `handler`. [We use Node v8](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) so `async` functions **are** supported ([beware common mistakes](https://serverless.com/blog/common-node8-mistakes-in-lambda/)!). Read [Netlify Functions docs](https://www.netlify.com/docs/functions/#javascript-lambda-functions) for more info.
67+
- Functions [time out in 10 seconds](https://www.netlify.com/docs/functions/#custom-deployment-options) by default although extensions can be requested. We [try to replicate this locally](https://github.com/netlify/netlify-lambda/pull/116).
6768

6869
<details>
6970
<summary><b>Environment variables in build and branch context</b></summary>
@@ -77,7 +78,6 @@ If you need local-only environment variables that you don't place in `netlify.to
7778

7879
</details>
7980

80-
8181
<summary>
8282
<b>Lambda function examples</b>
8383
</summary>
@@ -116,14 +116,17 @@ export async function handler(event, context) {
116116

117117
## Using with `create-react-app`, Gatsby, and other development servers
118118

119-
### Why you need to proxy (for beginners)
119+
<details>
120+
<summary><b>Why you need to proxy (for beginners)</b></summary>
120121

121122
`react-scripts` (the underlying library for `create-react-app`) and other popular development servers often set up catchall serving for you; in other words, if you try to request a route that doesn't exist, the dev server will try to serve you `/index.html`. This is problematic when you are trying to hit a local API endpoint like `netlify-lambda` sets up for you - your browser will attempt to parse the `index.html` file as JSON. This is why you may see this error:
122123

123124
`Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0`
124125

125126
If this desribes your situation, then you need to proxy for local development. Read on. Don't worry it's easier than it looks.
126127

128+
</details>
129+
127130
### Proxying for local development
128131

129132
> ⚠️IMPORTANT! PLEASE READ THIS ESPECIALLY IF YOU HAVE CORS ISSUES⚠️
@@ -212,9 +215,8 @@ If you need to use additional webpack modules or loaders, you can specify an add
212215

213216
For example, have a file with:
214217

215-
216218
```js
217-
// webpack.functions.js
219+
// webpack.functions.js
218220
module.exports = {
219221
optimization: { minimize: false }
220222
};
@@ -273,6 +275,7 @@ There are additional CLI options:
273275
-c --config
274276
-p --port
275277
-s --static
278+
-t --timeout
276279
```
277280

278281
### --config option
@@ -281,15 +284,18 @@ If you need to use additional webpack modules or loaders, you can specify an add
281284

282285
For example, have a file with:
283286

284-
285287
```js
286-
// webpack.functions.js
288+
// webpack.functions.js
287289
module.exports = {
288290
optimization: { minimize: false }
289291
};
290292
```
291293

292-
Then specify `netlify-lambda serve --config ./webpack.functions.js`.
294+
Then specify `netlify-lambda serve --config ./webpack.functions.js`.
295+
296+
### --timeout option
297+
298+
(This is for local dev/serving only) The default function timeout is 10 seconds. If you need to adjust this because you have requested extra timeout, pass a timeout number here. Thanks to [@naipath](https://github.com/netlify/netlify-lambda/pull/116) for this feature.
293299

294300
### --port option
295301

@@ -316,9 +322,8 @@ To debug lambdas, you can use the `--inspect` flag. Additionally:
316322

317323
For example, have a file with:
318324

319-
320325
```js
321-
// webpack.functions.js
326+
// webpack.functions.js
322327
module.exports = {
323328
optimization: { minimize: false }
324329
};
@@ -328,6 +333,12 @@ Then specify `netlify-lambda serve --config ./webpack.functions.js`. If using VS
328333

329334
Netlify Functions [run in Node v8.10](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) and you may need to run the same version to mirror the environment locally. Also make sure to check that you aren't [committing one of these common Node 8 mistakes in Lambda!](https://serverless.com/blog/common-node8-mistakes-in-lambda/)
330335

336+
**Special warning on `node-fetch`**: `node-fetch` and webpack [currently don't work well together](https://github.com/bitinn/node-fetch/issues/450). you will have to use the default export in your code:
337+
338+
```js
339+
cons fetch = require('node-fetch').default // not require('node-fetch')
340+
```
341+
331342
Don't forget to search our issues in case someone has run into a similar problem you have!
332343

333344
## Example functions and Tutorials
@@ -367,6 +378,7 @@ All of the above are community maintained and not officially supported by Netlif
367378
- v1.1: https://twitter.com/swyx/status/1069544181259849729 Typescript support
368379
- v1.2: https://twitter.com/swyx/status/1083446733374337024 Identity emulation (& others)
369380
- v1.3: https://github.com/netlify/netlify-lambda/releases/tag/v1.3.0
381+
- v1.4: New timeout feature https://github.com/netlify/netlify-lambda/pull/116
370382

371383
## License
372384

0 commit comments

Comments
 (0)