You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-11
Original file line number
Diff line number
Diff line change
@@ -5,19 +5,23 @@ Cloudflare Workers is a great platform to deploy static sites: the application w
5
5
6
6
Note: This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
7
7
8
-
See video tutorial: https://www.youtube.com/watch?v=6YC3MgVwCGA
8
+
See video tutorial by Cloudflare: https://www.youtube.com/watch?v=6YC3MgVwCGA
9
9
10
10
11
11
# Steps to reproduce this project
12
12
13
-
This assumes you have installed npm. It is recommended to install it with a Node version manager like `nvm`, which puts the global `node_modules` in your home directory to eliminate permissions issues with `npm install -g`.
13
+
This assumes you have installed `npm`. It is recommended to install it with a Node version manager like `nvm`, which puts the global `node_modules` in your home directory to eliminate permissions issues with `npm install -g`.
14
14
Next install the wrangler CLI tool to be able to use Workers:
15
15
16
16
`npm i @cloudflare/wrangler -g`
17
17
18
-
Then login:
18
+
Now that Wrangler is installed, you'll need to give it an API Token for your Cloudflare account. Run the command `wrangler login` and Wrangler will ask to automatically open your web browser to log into your Cloudflare account.
19
19
20
-
wrangler config
20
+
Alternatively, if you have already created a Cloudflare API token, use this command to authenticate Wrangler with that Cloudflare API Token:
21
+
22
+
`wrangler config`
23
+
24
+
This is an interactive command that will prompt you for your API token (Note: Do not use a global API key)...
21
25
22
26
## Create a static site
23
27
@@ -27,11 +31,11 @@ The create-react-app will create a new project (i.e. `cf-react`), and include al
27
31
28
32
## Generate a project
29
33
30
-
Enter the newly-created React project folder, and use the following command to generate a Workers Sites configuration for your project:
34
+
Enter the newly-created React project folder (i.e. `cd cf-react`), and use this command to generate a Workers Sites configuration:
31
35
32
36
`wrangler init --site`
33
37
34
-
The init --site command will provide the scaffolding necessary to deploy your React application. For the majority of static sites, you shouldn’t need to change the Workers script: by default, the script will look at an incoming request, and will serve a corresponding asset from [Workers KV](https://www.cloudflare.com/products/workers-kv/) based on that route. For instance, if my static site is deployed at mystaticsite.com, requesting mystaticsite.com/about.html will look for a file in KV called about.html, and serve it back to the client. In addition, if the asset being returned from KV is cacheable, it will automatically be cached with Cloudflare’s CDN, making subsequent requests even faster.
38
+
This will provide the scaffolding necessary to deploy the React application. For the majority of static sites, we do not need to change the Workers script: by default, the script will look at an incoming request, and will serve a corresponding asset from [Workers KV](https://www.cloudflare.com/products/workers-kv/) based on that route. For instance, if my static site is deployed at mystaticsite.com, requesting mystaticsite.com/about.html will look for a file in KV called about.html, and serve it back to the client. In addition, if the asset being returned from KV is cacheable, it will automatically be cached with Cloudflare’s CDN, making subsequent requests even faster.
35
39
36
40
To serve a single page application, update workers-site/index.js with the following code to so that all html requests are pointed at your root index.html file.
37
41
@@ -45,15 +49,15 @@ async function handleEvent(event) {
45
49
46
50
## Configure and publish
47
51
48
-
The bucket key in the wrangler.toml indicates the “build” folder that Sites will deploy to Workers.
49
-
While many front-end application and static site generators use the folder public, `create-react-app` uses the folder `build`. So, we need to change the `bucket` key in wrangler.toml to `build`:
52
+
Edit wrangler.toml file and add your cloudflare Account Id in the key `account_id`. You may find this id on you Account Home (the screen you see after you login to Cloudflare) and then clicking Workers. It’s on the right hand side.
53
+
It is ok to leave that Account ID in a public repo, as no action can be taken on your behalf without a corresponding API Token / Key, which should always be secret.
54
+
55
+
Also, there is a `bucket` key in the wrangler.toml that indicates the "build" folder that Sites will deploy to Workers. `create-react-app` uses a folder named `build`. So, we need to change the `bucket` key in wrangler.toml to `build`:
50
56
51
57
```yaml
52
58
wrangler.toml
53
59
# ... previous wrangler config
54
-
[site]
55
-
bucket = "./build"
56
-
entry-point = "workers-site"
60
+
site = {bucket = "./build", entry-point = "workers-site"}
57
61
```
58
62
59
63
With wrangler.toml configured, it’s time to build the project, and publish it to Workers. Run npm `run build` to tell create-react-app to build the site, and `wrangler publish` to deploy it to Workers:
@@ -64,6 +68,17 @@ npm run build
64
68
wrangler publish
65
69
```
66
70
71
+
Alternatively, to easily test your Worker while developing. you can run
72
+
73
+
`wrangler dev`
74
+
75
+
This command establishes a connection between `localhost` (your computer) and an edge server that operates your Worker in development. A cloudflared tunnel forwards all requests to the edge server, which continuously updates as your Worker code changes. This allows full access to Workers KV, Durable Objects, etc. You can see your app with your browser at:
76
+
77
+
`http://127.0.0.1:8787/`
78
+
79
+
80
+
From then on, you can start developing your React app. I.e. edit src/App.js and save to reload.
0 commit comments