Skip to content

Commit 020cfe3

Browse files
authored
Wrangler config updates (#18313)
1 parent 1040f47 commit 020cfe3

File tree

154 files changed

+1772
-144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+1772
-144
lines changed

CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,46 @@ If we require more information to address your pull request, the `more-informati
3333
- [npm](https://nodejs.org/en/learn/getting-started/an-introduction-to-the-npm-package-manager#introduction-to-npm) is the recommended package manager that must be used in installing dependencies.
3434
- The generated `package-lock.json` file must be committed to git.
3535

36+
## Wrangler config
37+
38+
If you're adding a code snippet to the docs that represents a Wrangler config file (`wrangler.toml` or `wrangler.json`) make sure you wrap it with the `<WranglerConfig>` component, which ensure it's rendered as both JSON and TOML e.g.
39+
40+
````
41+
42+
import { WranglerConfig } from "~/components";
43+
44+
<WranglerConfig>
45+
46+
```toml
47+
# Top-level configuration
48+
name = "my-worker"
49+
main = "src/index.js"
50+
compatibility_date = "2022-07-12"
51+
52+
workers_dev = false
53+
route = { pattern = "example.org/*", zone_name = "example.org" }
54+
55+
kv_namespaces = [
56+
{ binding = "<MY_NAMESPACE>", id = "<KV_ID>" }
57+
]
58+
59+
[env.staging]
60+
name = "my-worker-staging"
61+
route = { pattern = "staging.example.org/*", zone_name = "example.org" }
62+
63+
kv_namespaces = [
64+
{ binding = "<MY_NAMESPACE>", id = "<STAGING_KV_ID>" }
65+
]
66+
```
67+
68+
</WranglerConfig>
69+
70+
````
3671

3772
## Workers Playground
73+
3874
If you are adding a code snippet to the docs that is:
75+
3976
1. A fully contained, valid Worker (i.e. it does not require external dependencies or specific bindings)
4077
2. Only JavaScript
4178

@@ -58,6 +95,7 @@ export default {
5895
};
5996
```
6097
````
98+
6199
would render as
62100

63101
<img width="870" alt="Screenshot 2024-02-20 at 14 29 22" src="https://github.com/cloudflare/cloudflare-docs/assets/28503158/56aa8016-b3b6-4d64-8213-b1a26f16534a">

package-lock.json

Lines changed: 17 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@cloudflare/workers-types": "^4.20241112.0",
4040
"@codingheads/sticky-header": "^1.0.2",
4141
"@iconify-json/material-symbols": "^1.2.8",
42+
"@iarna/toml": "^2.2.5",
4243
"@stoplight/json-schema-tree": "^4.0.0",
4344
"@types/dompurify": "^3.2.0",
4445
"@types/hast": "^3.0.4",
@@ -61,6 +62,7 @@
6162
"he": "^1.2.0",
6263
"instantsearch.css": "^8.5.1",
6364
"instantsearch.js": "^4.75.4",
65+
"jsonc-parser": "^3.3.1",
6466
"lz-string": "^1.5.0",
6567
"marked": "^15.0.2",
6668
"mdast-util-mdx-expression": "^2.0.1",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
import { parse } from "node-html-parser";
3+
import { Code, Tabs, TabItem } from "@astrojs/starlight/components";
4+
import TOML from "@iarna/toml";
5+
import { parse as jsoncParse } from "jsonc-parser";
6+
7+
const slot = await Astro.slots.render("default");
8+
9+
const html = parse(slot);
10+
11+
const copy = html.querySelector("div.copy > button");
12+
13+
if (!copy) {
14+
throw new Error(
15+
`[WranglerConfig] Unable to find copy button in rendered code block HTML.`,
16+
);
17+
}
18+
19+
let code = copy.attributes["data-code"];
20+
21+
if (!code) {
22+
throw new Error(
23+
`[WranglerConfig] Unable to find data-code attribute on copy button.`,
24+
);
25+
}
26+
27+
code = code.replace(/\u007f/g, "\n");
28+
29+
const language =
30+
html.querySelector("[data-language]")?.attributes["data-language"];
31+
32+
if (!language) {
33+
throw new Error(`[WranglerConfig] Unable to find data-language.`);
34+
}
35+
36+
let toml, json;
37+
38+
if (language === "toml") {
39+
toml = code;
40+
json = JSON.stringify(TOML.parse(code), null, 2);
41+
} else {
42+
json = code;
43+
toml = TOML.stringify(jsoncParse(code));
44+
}
45+
---
46+
47+
<Tabs syncKey="wranglerConfig">
48+
<TabItem label="wrangler.toml" icon="setting">
49+
<Code lang="toml" code={toml} />
50+
</TabItem>
51+
<TabItem label="wrangler.json" icon="seti:json">
52+
<Code lang="jsonc" code={json} />
53+
</TabItem>
54+
</Tabs>

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export { default as TroubleshootingList } from "./TroubleshootingList.astro";
5353
export { default as TunnelCalculator } from "./TunnelCalculator.astro";
5454
export { default as Type } from "./Type.astro";
5555
export { default as TypeScriptExample } from "./TypeScriptExample.astro";
56+
export { default as WranglerConfig } from "./WranglerConfig.astro";
5657
export { default as WorkersArchitectureDiagram } from "./WorkersArchitectureDiagram.astro";
5758
export { default as WorkersIsolateDiagram } from "./WorkersIsolateDiagram.astro";
5859
export { default as WorkerStarter } from "./WorkerStarter.astro";

src/content/docs/ai-gateway/integrations/aig-workers-ai-binding.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ You must create an AI binding for your Worker to connect to Workers AI. Bindings
4949

5050
To bind Workers AI to your Worker, add the following to the end of your `wrangler.toml` file:
5151

52+
import { WranglerConfig } from "~/components";
53+
54+
<WranglerConfig>
55+
5256
```toml title="wrangler.toml"
5357
[ai]
5458
binding = "AI"
5559
```
5660

61+
</WranglerConfig>
62+
5763
Your binding is [available in your Worker code](/workers/reference/migrate-to-module-workers/#bindings-in-es-modules-format) on [`env.AI`](/workers/runtime-apis/handlers/fetch/).
5864

5965
You will need to have your `gateway id` for the next step. You can learn [how to create an AI Gateway in this tutorial](/ai-gateway/get-started/).

src/content/docs/analytics/analytics-engine/get-started.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ import { DirectoryListing } from "~/components"
1515

1616
Add the following to your `wrangler.toml` file to create a [binding](/workers/runtime-apis/bindings/) to a Workers Analytics Engine dataset. A dataset is like a table in SQL: the rows and columns should have consistent meaning.
1717

18+
import { WranglerConfig } from "~/components";
19+
20+
<WranglerConfig>
21+
1822
```toml
1923
[[analytics_engine_datasets]]
2024
binding = "<BINDING_NAME>"
2125
dataset = "<DATASET_NAME>"
2226
```
2327

28+
</WranglerConfig>
29+
2430
## 2. Write data points from your Worker
2531

2632
You can write data points to your Worker by calling the `writeDataPoint()` method that is exposed on the binding that you just created.
@@ -38,7 +44,7 @@ async fetch(request, env) {
3844

3945
:::note
4046

41-
You do not need to await `writeDataPoint()` — it will return immediately, and the Workers runtime handles writing your data in the background.
47+
You do not need to await `writeDataPoint()` — it will return immediately, and the Workers runtime handles writing your data in the background.
4248
:::
4349

4450
A data point is a structured event that consists of:
@@ -81,7 +87,7 @@ LIMIT 10
8187

8288
:::note
8389

84-
We are using a custom averaging function to take [sampling](/analytics/analytics-engine/sql-api/#sampling) into account.
90+
We are using a custom averaging function to take [sampling](/analytics/analytics-engine/sql-api/#sampling) into account.
8591
:::
8692

8793
You can run this query by making an HTTP request to the SQL API:
@@ -117,6 +123,6 @@ Refer to [Querying Workers Analytics Engine from Grafana](/analytics/analytics-e
117123

118124
## Further reading
119125

120-
<DirectoryListing
126+
<DirectoryListing
121127
folder="analytics/analytics-engine"
122128
/>

src/content/docs/analytics/analytics-engine/worker-querying.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ First the environment variables are set up with the account ID and API token.
4848

4949
The account ID is set in `wrangler.toml`:
5050

51+
import { WranglerConfig } from "~/components";
52+
53+
<WranglerConfig>
54+
5155
```toml
5256
[vars]
5357
ACCOUNT_ID = "<account_id>"
5458
```
5559

60+
</WranglerConfig>
61+
5662
The API_TOKEN can be set as a secret, using the wrangler command line tool, by running the following and entering your token string:
5763

5864
```sh

src/content/docs/browser-rendering/get-started/browser-rendering-with-DO.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ Configure your `browser-worker` project's [`wrangler.toml`](/workers/wrangler/co
7575

7676
Update your `wrangler.toml` configuration file with the Browser Rendering API binding, the R2 bucket you created and a Durable Object:
7777

78+
import { WranglerConfig } from "~/components";
79+
80+
<WranglerConfig>
81+
7882
```toml
7983
name = "rendering-api-demo"
8084
main = "src/index.js"
8185
compatibility_date = "2023-09-04"
8286
compatibility_flags = [ "nodejs_compat"]
83-
account_id = <ACCOUNT_ID>
87+
account_id = "<ACCOUNT_ID>"
8488

8589

8690
# Browser Rendering API binding
@@ -103,6 +107,8 @@ new_classes = ["Browser"] # Array of new classes
103107

104108
```
105109

110+
</WranglerConfig>
111+
106112
## 6. Code
107113

108114
The code below uses Durable Object to instantiate a browser using Puppeteer. It then opens a series of web pages with different resolutions, takes a screenshot of each, and uploads it to R2.

src/content/docs/browser-rendering/get-started/screenshots.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ Configure your `browser-worker` project's [`wrangler.toml`](/workers/wrangler/co
6262

6363
Update your `wrangler.toml` configuration file with the Browser Rendering API binding and the KV namespaces you created:
6464

65+
import { WranglerConfig } from "~/components";
66+
67+
<WranglerConfig>
68+
6569
```toml
6670
name = "browser-worker"
6771
main = "src/index.js"
@@ -74,6 +78,8 @@ kv_namespaces = [
7478
]
7579
```
7680

81+
</WranglerConfig>
82+
7783
## 5. Code
7884

7985
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">

0 commit comments

Comments
 (0)