Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7977ddc

Browse files
author
sw-yx
committed
update docs for CRAL with netlify dev
1 parent eb77fe8 commit 7977ddc

File tree

5 files changed

+231
-83
lines changed

5 files changed

+231
-83
lines changed

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
> ⚠️You may not need `netlify-lambda`. [Netlify Dev](https://github.com/netlify/netlify-dev-plugin) works with `create-react-app` out of the box, give it a try! Only use `netlify-lambda` if you need a build step for your functions. [See its README for details](https://github.com/netlify/netlify-lambda/blob/master/README.md#netlify-lambda).
1+
## Create-React-App-Lambda
22

3-
This project is based on latest versions of [Create React App v3](https://github.com/facebookincubator/create-react-app) and [netlify-lambda v1](https://github.com/netlify/netlify-lambda).
3+
This project is a reference demo showing you how to use [Create React App v3](https://github.com/facebookincubator/create-react-app) and [netlify-lambda v1](https://github.com/netlify/netlify-lambda) together in a [Netlify Dev](https://github.com/netlify/netlify-dev-plugin) workflow.
44

5-
The main addition to base Create-React-App is a new folder: `src/lambda`. Each JavaScript file in there will be built for Lambda function deployment in `/built-lambda`, specified in [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/).
5+
⚠️NOTE: You may not need `netlify-lambda`. [Netlify Dev](https://github.com/netlify/netlify-dev-plugin) already works with `create-react-app` out of the box, give it a try! **Only use `netlify-lambda` if you need a build step for your functions.** [See its README for details](https://github.com/netlify/netlify-lambda/blob/master/README.md#netlify-lambda).
66

7-
As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. We've also included an async lambda example using async/await syntax in `async-chuck-norris.js`.
7+
## Project Setup
8+
9+
Source: The main addition to base Create-React-App is a new folder: `src/lambda`. This folder is specified and can be changed in the `package.json` script: `"build:lambda": "netlify-lambda build src/lambda"`.
10+
11+
Dist: Each JavaScript file in there will be built for Lambda function deployment in `/built-lambda`, specified in [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/).
12+
13+
As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. We've also included an async lambda example using async/await syntax in `async-dadjoke.js`.
814

915
[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda)
1016

@@ -18,50 +24,43 @@ All functions are compiled with webpack using the Babel Loader, so you can use m
1824

1925
## Local Development
2026

21-
Before developing, clone the repository and run `yarn` from the root of the repo to install all dependencies.
22-
23-
### Start each server individually
27+
```bash
28+
## prep steps for first time users
29+
npm i -g netlify-cli # Make sure you have the [Netlify CLI](https://github.com/netlify/cli) installed
30+
git clone https://github.com/netlify/create-react-app-lambda ## clone this repo
31+
cd create-react-app-lambda ## change into this repo
32+
yarn # install all dependencies
2433

25-
**Run the functions dev server**
26-
27-
From inside the project folder, run:
28-
29-
```
30-
yarn start:lambda
34+
## done every time you start up this project
35+
ntl dev ## nice shortcut for `neltify dev`
3136
```
3237

33-
This will open a local server running at `http://localhost:9000` serving your Lambda functions, updating as you make changes in the `src/lambda` folder.
38+
This fires up [Netlify Dev](https://github.com/netlify/netlify-dev-plugin/), which:
3439

35-
You can then access your functions directly at `http://localhost:9000/{function_name}`, but to access them with the app, you'll need to start the app dev server. Under the hood, this uses `react-scripts`' [advanced proxy feature](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#configuring-the-proxy-manually) with the `setupProxy.js` file.
40+
- Detects that you are running a `create-react-app` project and runs the npm script that contains `react-scripts start`, which in this project is the `start` script
41+
- Detects that you use `netlify-lambda` as a [function builder](https://github.com/netlify/netlify-dev-plugin/#function-builders-function-builder-detection-and-relationship-with-netlify-lambda), and runs the npm script that contains `netlify-lambda build`, which in this project is the `build:lambda` script
3642

37-
**Run the app dev server**
43+
## Deployment
3844

39-
While the functions server is still running, open a new terminal tab and run:
45+
During deployment, this project is configured, inside `netlify.toml` to run the build `command`: `yarn build`.
4046

41-
```
42-
yarn start
43-
```
44-
45-
This will start the normal create-react-app dev server and open your app at `http://localhost:3000`.
46-
47-
Local in-app requests to the relative path `/.netlify/functions/*` will automatically be proxied to the local functions dev server.
48-
49-
> Note: You can also use [npm-run-all](https://github.com/mysticatea/npm-run-all#readme) to run the functions dev server and app dev server concurrently. Note that you don't need this if you use [`netlify dev`](https://github.com/netlify/netlify-dev-plugin/) as [function builder detection](https://www.netlify.com/blog/2019/04/24/zero-config-yet-technology-agnostic-how-netlify-dev-detectors-work/) does that for you.
47+
`yarn build` corresponds to the npm script `build`, which uses `npm-run-all` (aka `run-p`) to concurrently run `"build:app"` (aka `react-scripts build`) and `build:lambda` (aka `netlify-lambda build src/lambda`).
5048

5149
## Typescript
5250

5351
<details>
5452
<summary>
5553
<b id="typescript">Click for instructions</b>
5654
</summary>
57-
You can use Typescript in both your React code (with `react-scripts` v2.1+) and your lambda functions )with `netlify-lambda` v1.1+). Follow these instructions:
55+
56+
You can use Typescript in both your frontend React code (with `react-scripts` v2.1+) and your serverless functions (with `netlify-lambda` v1.1+). Follow these instructions:
5857

5958
1. `yarn add -D typescript @types/node @types/react @types/react-dom @babel/preset-typescript @types/aws-lambda`
6059
2. convert `src/lambda/hello.js` to `src/lambda/hello.ts`
6160
3. use types in your event handler:
6261

6362
```ts
64-
import { Handler, Context, Callback, APIGatewayEvent } from "aws-lambda"
63+
import { Handler, Context, Callback, APIGatewayEvent } from 'aws-lambda'
6564

6665
interface HelloResponse {
6766
statusCode: number
@@ -74,8 +73,8 @@ const handler: Handler = (event: APIGatewayEvent, context: Context, callback: Ca
7473
statusCode: 200,
7574
body: JSON.stringify({
7675
msg: `Hello world ${Math.floor(Math.random() * 10)}`,
77-
params
78-
})
76+
params,
77+
}),
7978
}
8079

8180
callback(undefined, response)
@@ -98,4 +97,4 @@ For a full demo of routing and authentication, check this branch: https://github
9897

9998
## Service Worker
10099

101-
The service worker does not work with lambda functions out of the box. It prevents calling the function and returns the app itself instead ([Read more](https://github.com/facebook/create-react-app/issues/2237#issuecomment-302693219)). To solve this you have to eject and enhance the service worker configuration in the webpack config. Whitelist the path of your lambda function and you are good to go.
100+
`create-react-app`'s default service worker (in `src/index.js`) does not work with lambda functions out of the box. It prevents calling the function and returns the app itself instead ([Read more](https://github.com/facebook/create-react-app/issues/2237#issuecomment-302693219)). To solve this you have to eject and enhance the service worker configuration in the webpack config. Whitelist the path of your lambda function and you are good to go.

netlify.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[build]
2-
command = "yarn build"
3-
functions = "built-lambda"
4-
publish = "build"
2+
command = "yarn build" # the command you run to build this file
3+
functions = "built-lambda" # netlify-lambda builds to this folder AND Netlify reads functions from here
4+
publish = "build" # create-react-app builds to this folder, Netlify should serve all these files statically

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
2-
"name": "react-lambda",
3-
"version": "0.4.0",
2+
"name": "create-react-app-lambda",
3+
"version": "0.5.0",
44
"private": true,
55
"dependencies": {
66
"axios": "^0.19.0",
77
"react": "^16.8.6",
88
"react-dom": "^16.8.6",
9-
"react-scripts": "^3.0.0"
9+
"react-scripts": "^3.0.1"
1010
},
1111
"scripts": {
1212
"start": "react-scripts start",
13-
"start:lambda": "netlify-lambda serve src/lambda",
1413
"build": "run-p build:**",
1514
"build:app": "react-scripts build",
1615
"build:lambda": "netlify-lambda build src/lambda",
@@ -27,9 +26,6 @@
2726
"not op_mini all"
2827
],
2928
"devDependencies": {
30-
"@babel/plugin-transform-object-assign": "^7.0.0",
31-
"babel-loader": "8.0.5",
32-
"http-proxy-middleware": "^0.19.0",
3329
"netlify-lambda": "^1.4.5",
3430
"npm-run-all": "^4.1.5"
3531
}

src/lambda/hello.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
// this uses the callback syntax, however, we encourage you to try the async/await syntax shown in async-dadjoke.js
12
export function handler(event, context, callback) {
2-
console.log("queryStringParameters", event.queryStringParameters)
3+
console.log('queryStringParameters', event.queryStringParameters)
34
callback(null, {
45
statusCode: 200,
5-
body: JSON.stringify({ msg: "Hello, World!" })
6+
body: JSON.stringify({ msg: 'Hello, World!' }),
67
})
78
}

0 commit comments

Comments
 (0)