Skip to content

Commit 706f346

Browse files
deadlypants1973pedrosousalukeed
authored
[Workers] tutorials style guide revision (cloudflare#3199)
* [Workers] tutorials style guide revision * Apply suggestions from code review Pedro's suggestions Co-authored-by: Pedro Sousa <[email protected]> Co-authored-by: Luke Edwards <[email protected]> * Apply suggestions from code review Co-authored-by: Pedro Sousa <[email protected]> * Apply suggestions from code review Co-authored-by: Pedro Sousa <[email protected]> Co-authored-by: Luke Edwards <[email protected]> * Apply suggestions from code review our -> your * update 500 * Update products/workers/src/content/tutorials/github-sms-notifications-using-twilio/index.md Webhooks -> webhook Co-authored-by: Pedro Sousa <[email protected]> * Update products/workers/src/content/tutorials/authorize-users-with-auth0/index.md Co-authored-by: Pedro Sousa <[email protected]> * Update products/workers/src/content/tutorials/authorize-users-with-auth0/index.md pedro's suggestion Co-authored-by: Pedro Sousa <[email protected]> * Update products/workers/src/content/tutorials/authorize-users-with-auth0/index.md pedro's suggestion Co-authored-by: Pedro Sousa <[email protected]> * Update products/workers/src/content/tutorials/authorize-users-with-auth0/index.md * Apply suggestions from code review * related resources Co-authored-by: Pedro Sousa <[email protected]> Co-authored-by: Luke Edwards <[email protected]>
1 parent 09671a9 commit 706f346

File tree

15 files changed

+126
-114
lines changed

15 files changed

+126
-114
lines changed

products/workers/src/content/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function handleRequest(request) {
6969

7070
[Explore third-party packages](https://workers.cloudflare.com/works) that work on Workers, submitted by Cloudflare users.
7171

72-
[Connect with the Workers community on Discord](https://discord.gg/cloudflaredev) to ask questions, show off what you are building, and discuss the platform with other developers.
72+
[Connect with the Workers community on Discord](https://discord.gg/cloudflaredev) to ask questions, show off what you are building, and discuss the platform with other developers.
7373

7474
[Follow @CloudflareDev on Twitter](https://twitter.com/cloudflaredev) to learn about product announcements, new tutorials, and what is new in Cloudflare Workers.
7575

products/workers/src/content/tutorials/authorize-users-with-auth0/index.md

Lines changed: 41 additions & 37 deletions
Large diffs are not rendered by default.

products/workers/src/content/tutorials/build-a-jamstack-app/index.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you would like to see the finished code for this project, find the [project o
2828

2929
Cloudflare offers a collection of pre-built examples for getting started with Workers. These are often referred to as templates, each of which emphasizes a particular use case or language. Wrangler, Cloudflare’s command-line tool for managing Worker projects, integrates with all templates so that it is even easier to start writing Workers. In this tutorial, you will use the default JavaScript template to generate a Workers project.
3030

31-
In your terminal, generate a Worker project with your desired project name; for example, todos:
31+
In your terminal, generate a Worker project with your desired project name; for example, `todos`:
3232

3333
```sh
3434
---
@@ -61,7 +61,7 @@ async function handleRequest(request) {
6161
}
6262
```
6363

64-
In your default `index.js` file, you can see that request/response pattern in action. The `handleRequest` constructs a new `Response` with the body text Hello worker, as well as an explicit `200` status code. When a Worker receives a `fetch` event, the script must use `event.respondWith` to return the newly constructed response to the client. This means that your Cloudflare Worker script will serve new responses directly from [Cloudflare's edge network](https://www.cloudflare.com/network). If you compare this with more traditional architectures, where an origin server would accept requests and return responses, Cloudflare Workers allows you to do the same work without managing hardware and closer the client, resulting in reduced cost and latencies.
64+
In your default `index.js` file, you can see that request/response pattern in action. The `handleRequest` constructs a new `Response` with the body text `Hello worker`, as well as an explicit `200` status code. When a Worker receives a `fetch` event, the script must use `event.respondWith` to return the newly constructed response to the client. This means that your Cloudflare Worker script will serve new responses directly from [Cloudflare's edge network](https://www.cloudflare.com/network). If you compare this with more traditional architectures, where an origin server would accept requests and return responses, Cloudflare Workers allows you to do the same work without managing hardware and closer to the client, resulting in reduced cost and latencies.
6565

6666
## Build
6767

@@ -79,7 +79,7 @@ For the remainder of this tutorial you will complete each task, iterating on you
7979

8080
To begin, you need to understand how to populate your todo list with actual data. To do this, use Cloudflare Workers KV — a key-value store that you can access inside of your Worker script to read and write data.
8181

82-
To get started with KV, set up a namespace. All of your cached data will be stored inside that namespace and, with configuration, you can access that namespace inside the script with a predefined variable. Use Wrangler to create a new namespace and get the associated namespace ID by running the following command in your terminal:
82+
To get started with KV, set up a namespace. All of your cached data will be stored inside that namespace and, with configuration, you can access that namespace inside the script with a predefined variable. Use Wrangler to create a new namespace and get the associated namespace ID by running the following command in your terminal:
8383

8484
```sh
8585
---
@@ -125,7 +125,7 @@ async function handleRequest(request) {
125125

126126
Workers KV is an eventually consistent, global datastore. In other words, any writes within a region are immediately reflected within that same region, but will not be immediately available in other regions. However, those writes will eventually be available everywhere and, at that point, Workers KV guarantees that data within each region will be consistent.
127127

128-
Given the presence of data in the cache and the assumption that your cache is eventually consistent, this code needs a slight adjustment: the application should check the cache and use its value, if the key exists. If it does not, you will use `defaultData` as the data source for now (remember, it should be set in the future) and write it to the cache for future use. After breaking out the code into a few functions for simplicity, the result looks like this:
128+
Given the presence of data in the cache and the assumption that your cache is eventually consistent, this code needs a slight adjustment: the application should check the cache and use its value, if the key exists. If it does not, you will use `defaultData` as the data source for now (it should be set in the future) and write it to the cache for future use. After breaking out the code into a few functions for simplicity, the result looks like this:
129129

130130
```js
131131
---
@@ -323,7 +323,7 @@ async function handleRequest(request) {
323323

324324
The script is pretty straightforward – you check that the request is a `PUT` and wrap the remainder of the code in a `try/catch` block. First, you parse the body of the request coming in, ensuring that it is JSON, before you update the cache with the new data and return it to the user. If anything goes wrong, return a `500` status code. If the route is hit with an HTTP method other than `PUT` — for example, `POST` or `DELETE` — return a `404` error.
325325

326-
With this script, you can now add some dynamic functionality to your HTML page to actually hit this route. First, create an input for your todo name and a button for submitting the todo.
326+
With this script, you can now add some dynamic functionality to your HTML page to actually hit this route. First, create an input for your todo name and a button for submitting the todo.
327327

328328
```js
329329
---
@@ -459,9 +459,9 @@ const html = todos => `
459459
`
460460
```
461461

462-
So far, you have designed the client-side part of this code to handle an array of todos and render a list of simple HTML elements. There is a number of things that you have been doing that you have not quite had a use for yet – specifically, the inclusion of IDs and updating the todo's completed state. Luckily, these things work well together to actually support updating todos in the application UI.
462+
So far, you have designed the client-side part of this code to handle an array of todos and render a list of simple HTML elements. There is a number of things that you have been doing that you have not quite had a use for yet – specifically, the inclusion of IDs and updating the todo's completed state. These things work well together to actually support updating todos in the application UI.
463463

464-
To start, it would be useful to attach the ID of each todo in the HTML. By doing this, you can then refer to the element later in order to correspond it to the todo in the JavaScript part of our code. Data attributes and the corresponding `dataset` method in JavaScript are a perfect way to implement this. When you generate your `div` element for each todo, you can attach a data attribute called todo to each `div`:
464+
To start, it would be useful to attach the ID of each todo in the HTML. By doing this, you can then refer to the element later in order to correspond it to the todo in the JavaScript part of your code. Data attributes and the corresponding `dataset` method in JavaScript are a perfect way to implement this. When you generate your `div` element for each todo, you can attach a data attribute called todo to each `div`:
465465

466466
```js
467467
---
@@ -566,11 +566,11 @@ The final result of our code is a system that checks the todos variable, updates
566566

567567
## Conclusions and next steps
568568

569-
By completing this tutorial, you have created a pretty remarkable project. You have built a static HTML, CSS, and JavaScript application that is transparently powered by Workers and Workers KV, which take full advantage of Cloudflare's edge network.
569+
By completing this tutorial, you have built a static HTML, CSS, and JavaScript application that is transparently powered by Workers and Workers KV, which take full advantage of Cloudflare's edge network.
570570

571-
There is room for improvement, too, if you feel so inclined. For example, you may want to implement a better design (you can refer to a live version available at [todos.signalnerve.workers.dev](https://todos.signalnerve.workers.dev/)), or make additional improvements to security, speed, etc.
571+
If you would like to keep improving on your project, you may want to implement a better design (you can refer to a live version available at [todos.signalnerve.workers.dev](https://todos.signalnerve.workers.dev/)), or make additional improvements to security and speed.
572572

573-
You may also want to add user-specific caching. Right now, the cache key is always data – this means that any visitor to the site will share the same todo list with other visitors. Within your Worker, you could use values from the client request to create and maintain user-specific lists. For example, you may generate a cache key based on the requesting IP:
573+
You may also want to add user-specific caching. Right now, the cache key is always `data` – this means that any visitor to the site will share the same todo list with other visitors. Within your Worker, you could use values from the client request to create and maintain user-specific lists. For example, you may generate a cache key based on the requesting IP:
574574

575575
```js
576576
---

products/workers/src/content/tutorials/build-a-qr-code-generator/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you would like to skip straight to the code, the final version of the codebas
2323

2424
Cloudflare’s command-line tool for managing Worker projects, [Wrangler](https://github.com/cloudflare/wrangler), supports various templates — pre-built collections of code that make it easy to get started writing Workers. You will make use of the default JavaScript template to start building your project.
2525

26-
In the command line, run the `wrangler generate` command to create your Worker project using Wrangler’s [worker-template](https://github.com/cloudflare/worker-template). Pass the project name qr-code-generator:
26+
In the command line, run the `wrangler generate` command to create your Worker project using Wrangler’s [worker-template](https://github.com/cloudflare/worker-template). Pass the project name `qr-code-generator`:
2727

2828
```sh
2929
---
@@ -56,7 +56,7 @@ function handleRequest(request) {
5656
}
5757
```
5858

59-
In your default `index.js` file, you can see that request/response pattern in action. The `handleRequest` constructs a new `Response` with the body text Hello worker, as well as an explicit `200` status code.
59+
In your default `index.js` file, you can see that request/response pattern in action. The `handleRequest` constructs a new `Response` with the body text `Hello worker!`, as well as an explicit `200` status code.
6060

6161
When a Worker receives a `fetch` event, the script must use `event.respondWith` to return the newly constructed response to the client. Your Cloudflare Worker script will serve new responses directly from [Cloudflare's edge network](https://www.cloudflare.com/network) instead of continuing to your origin server. A standard server would accept requests and return responses. Cloudflare Workers allows you to respond quickly by constructing responses directly on the Cloudflare edge network.
6262

@@ -68,7 +68,7 @@ The QR code generator you will build in this tutorial will be a serverless funct
6868

6969
### Handling requests
7070

71-
At this point in the tutorial, your Worker function can receive requests and return a simple response with the text Hello worker!. To handle data coming in to your serverless function, check if the incoming request is a `POST`:
71+
At this point in the tutorial, your Worker function can receive requests and return a simple response with the text `"Hello worker!"`. To handle data coming into your serverless function, check if the incoming request is a `POST`:
7272

7373
```js
7474
---
@@ -97,7 +97,7 @@ function handleRequest(request) {
9797
}
9898
```
9999

100-
At this point, you have established the basic flow of `handleRequest`. You will now set up a response to incoming valid requests. If a `POST` request comes in, the function should generate a QR code. To start, move the Hello worker! response into a new function, `generate`, which will ultimately contain the bulk of our function’s logic:
100+
At this point, you have established the basic flow of `handleRequest`. You will now set up a response to incoming valid requests. If a `POST` request comes in, the function should generate a QR code. To start, move the `"Hello worker!"` response into a new function, `generate`, which will ultimately contain the bulk of your function’s logic:
101101

102102
```js
103103
---

products/workers/src/content/tutorials/build-a-slackbot/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export const constructGhIssueSlackMessage = (
634634
}
635635
```
636636

637-
Back in `src/handlers/webhook.js`, the `blocks` that are returned from `constructGhIssueSlackMessage` become the body in a new `fetch` request, an HTTP POST request to a Slack webhook URL. Once that request completes, return a simple response with status code 200, and the body text “OK”:
637+
Back in `src/handlers/webhook.js`, the `blocks` that are returned from `constructGhIssueSlackMessage` become the body in a new `fetch` request, an HTTP POST request to a Slack webhook URL. Once that request completes, return a response with status code `200`, and the body text `"OK"`:
638638

639639
```js
640640
---
@@ -668,7 +668,7 @@ Since this webhook allows developers to post directly to your Slack channel, kee
668668

669669
</Aside>
670670

671-
To use this constant inside of your codebase, you can use Wrangler’s [Secrets](/cli-wrangler/commands#secret) feature:
671+
To use this constant inside of your codebase, use the [`wrangler secret`](/cli-wrangler/commands#secret) command:
672672

673673
```sh
674674
---

0 commit comments

Comments
 (0)