Skip to content

Commit e62c8e6

Browse files
committed
updated
1 parent 8e90977 commit e62c8e6

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

README.md

+26-11
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ Cloudflare Workers is a great platform to deploy static sites: the application w
55

66
Note: This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
77

8-
See video tutorial: https://www.youtube.com/watch?v=6YC3MgVwCGA
8+
See video tutorial by Cloudflare: https://www.youtube.com/watch?v=6YC3MgVwCGA
99

1010

1111
# Steps to reproduce this project
1212

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`.
1414
Next install the wrangler CLI tool to be able to use Workers:
1515

1616
`npm i @cloudflare/wrangler -g`
1717

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.
1919

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)...
2125

2226
## Create a static site
2327

@@ -27,11 +31,11 @@ The create-react-app will create a new project (i.e. `cf-react`), and include al
2731

2832
## Generate a project
2933

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:
3135

3236
`wrangler init --site`
3337

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.
3539

3640
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.
3741

@@ -45,15 +49,15 @@ async function handleEvent(event) {
4549

4650
## Configure and publish
4751

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`:
5056

5157
```yaml
5258
wrangler.toml
5359
# ... previous wrangler config
54-
[site]
55-
bucket = "./build"
56-
entry-point = "workers-site"
60+
site = {bucket = "./build", entry-point = "workers-site"}
5761
```
5862

5963
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
6468
wrangler publish
6569
```
6670

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.
81+
6782

6883
# Getting Started with Create React App
6984

src/App.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ function App() {
77
<header className="App-header">
88
<img src={logo} className="App-logo" alt="logo" />
99
<p>
10-
Edit <code>src/App.js</code> and save to reload.
10+
Hello, World!
11+
12+
I am running on Cloudflare Workers
1113
</p>
1214
<a
1315
className="App-link"

workers-site/package-lock.json

+30-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wrangler.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "cf-react"
22
type = 'webpack'
3-
account_id = ''
3+
account_id = 'a111e576129b8a175d42798bbf8c0c56'
44
route = ''
55
zone_id = ''
66
usage_model = ''

0 commit comments

Comments
 (0)