Skip to content

Commit 1ff0bc5

Browse files
MattieTKCopilotOxyjun
authored
feat: add automatic resource provisioning documentation (#26374)
* feat: add automatic resource provisioning documentation and config option - Added documentation for automatic provisioning of KV, R2, and D1 bindings without pre-creating resources - Implemented removeSchema prop in WranglerConfig component to conditionally exclude JSON schema reference - Reformatted redirected configuration section for improved readability * fix file tree * add remove schema docs * update e.g. to style guide Co-authored-by: Copilot <[email protected]> * remove stray semicolon Co-authored-by: Copilot <[email protected]> * Apply suggestions from code review * Move beta heading --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Jun Lee <[email protected]>
1 parent f825abe commit 1ff0bc5

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed

src/components/WranglerConfig.astro

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { parse } from "node-html-parser";
33
import { Code, Tabs, TabItem } from "@astrojs/starlight/components";
44
import TOML from "@iarna/toml";
55
import { parse as jsoncParse } from "jsonc-parser";
6+
import { z } from "astro:schema";
7+
8+
const props = z.object({
9+
removeSchema: z.boolean().optional().default(false),
10+
});
11+
12+
const { removeSchema } = props.parse(Astro.props);
613
714
const slot = await Astro.slots.render("default");
815
@@ -12,15 +19,15 @@ const copy = html.querySelector("div.copy > button");
1219
1320
if (!copy) {
1421
throw new Error(
15-
`[WranglerConfig] Unable to find copy button in rendered code block HTML.`,
22+
`[WranglerConfig] Unable to find copy button in rendered code block HTML.`
1623
);
1724
}
1825
1926
let code = copy.attributes["data-code"];
2027
2128
if (!code) {
2229
throw new Error(
23-
`[WranglerConfig] Unable to find data-code attribute on copy button.`,
30+
`[WranglerConfig] Unable to find data-code attribute on copy button.`
2431
);
2532
}
2633
@@ -38,10 +45,11 @@ let toml, json;
3845
3946
if (language === "toml") {
4047
toml = code;
41-
json = JSON.stringify({
42-
"$schema": "./node_modules/wrangler/config-schema.json",
43-
...TOML.parse(code),
44-
}, null, 2);
48+
const parsedToml = TOML.parse(code);
49+
const jsonObj = removeSchema
50+
? parsedToml
51+
: { $schema: "./node_modules/wrangler/config-schema.json", ...parsedToml };
52+
json = JSON.stringify(jsonObj, null, 2);
4553
} else {
4654
json = code;
4755
toml = TOML.stringify(jsoncParse(code));

src/content/docs/style-guide/components/wrangler-config.mdx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This component can be used to automatically generate a `jsonc` version of the `t
1010

1111
```mdx
1212
import { WranglerConfig } from "~/components";
13+
1314
```
1415

1516
## Usage
@@ -25,12 +26,10 @@ database_name = "prod-d1-tutorial"
2526
database_id = "<unique-ID-for-your-database>"
2627
```
2728
</WranglerConfig>
28-
2929
````
3030

3131
A `$today` value can be used in this component to automatically insert the today's date. This is useful for suggesting that users set the latest compatibility date.
3232

33-
3433
````mdx live
3534
import { WranglerConfig } from "~/components";
3635

@@ -42,5 +41,21 @@ import { WranglerConfig } from "~/components";
4241
}
4342
```
4443
</WranglerConfig>
44+
````
45+
46+
The `removeSchema` prop can be used to remove the `$schema` reference from the generated JSON file. This can be useful if you want to add snippets of configuration files that are easier to copy paste, and are providing toml as the source config format.
47+
48+
If you provide jsonc as the source config format, the `removeSchema` prop will be ignored.
49+
50+
````mdx live
51+
import { WranglerConfig } from "~/components";
4552

46-
````
53+
<WranglerConfig removeSchema>
54+
```toml
55+
[[d1_databases]]
56+
binding = "DB" # available in your Worker on env.DB
57+
database_name = "prod-d1-tutorial"
58+
database_id = "<unique-ID-for-your-database>"
59+
```
60+
</WranglerConfig>
61+
````

src/content/docs/workers/wrangler/configuration.mdx

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ description: Use a configuration file to customize the
99
Platform products.
1010
---
1111

12-
import { Render, Type, MetaInfo, WranglerConfig } from "~/components";
12+
import {
13+
Render,
14+
Type,
15+
MetaInfo,
16+
WranglerConfig,
17+
InlineBadge,
18+
} from "~/components";
1319
import { FileTree } from "@astrojs/starlight/components";
1420

1521
Wrangler optionally uses a configuration file to customize the development and deployment setup for a Worker.
@@ -71,6 +77,29 @@ Further, there are a few keys that can _only_ appear at the top-level.
7177

7278
<Render file="vite-environments" product="workers" />
7379

80+
## Automatic provisioning
81+
<a href="https://developers.cloudflare.com/changelog/2025-10-24-automatic-resource-provisioning/" target="_blank"><InlineBadge preset="beta" /></a>
82+
83+
Wrangler can automatically provision resources for you when you deploy your Worker without you having to create them ahead of time.
84+
85+
This currently works for KV, R2, and D1 bindings.
86+
87+
To use this feature, add bindings to your configuration file _without_ adding resource IDs, or in the case of R2, a bucket name. Resources will be created with the name of your worker as the prefix.
88+
89+
<WranglerConfig removeSchema>
90+
91+
```toml
92+
kv_namespaces = [
93+
{ binding = "<MY_KV_NAMESPACE>" }
94+
]
95+
```
96+
97+
</WranglerConfig>
98+
99+
When you run `wrangler dev`, local resources will automatically be created which persist between runs. When you run `wrangler deploy`, resources will be created for you, and their IDs will be written back to your configuration file.
100+
101+
If you deploy a worker with resources and no resource IDs from the dashboard (for example, via GitHub), resources will be created, but their IDs will only be accessible via the dashboard. Currently, these resource IDs will not be written back to your repository.
102+
74103
## Top-level only keys
75104

76105
Top-level keys apply to the Worker as a whole (and therefore all environments). They cannot be defined within named environments.
@@ -1422,35 +1451,34 @@ A common example of using a redirected configuration is where a custom build too
14221451
- `my-tool` generates a `dist` directory that contains both compiled code and a new generated deployment configuration file, containing only the settings for the given environment.
14231452
It also creates a `.wrangler/deploy/config.json` file that redirects Wrangler to the new, generated deployment configuration file:
14241453

1425-
<FileTree>
1426-
- dist
1427-
- index.js
1428-
- wrangler.jsonc
1429-
- .wrangler
1430-
- deploy
1431-
- config.json
1432-
1433-
</FileTree>
1434-
1435-
The generated `dist/wrangler.jsonc` might contain:
1436-
1437-
```json
1438-
{
1439-
"name": "my-worker",
1440-
"main": "./index.js",
1441-
"vars": {
1442-
"MY_VARIABLE": "staging variable"
1443-
}
1444-
}
1445-
```
1454+
<FileTree>
1455+
- dist
1456+
- index.js
1457+
- wrangler.jsonc
1458+
- .wrangler
1459+
- deploy
1460+
- config.json
1461+
</FileTree>
1462+
1463+
The generated `dist/wrangler.jsonc` might contain:
1464+
1465+
```json
1466+
{
1467+
"name": "my-worker",
1468+
"main": "./index.js",
1469+
"vars": {
1470+
"MY_VARIABLE": "staging variable"
1471+
}
1472+
}
1473+
```
14461474

1447-
Now, the `main` property points to the generated code entry-point, no environment is defined,
1448-
and the `MY_VARIABLE` variable is resolved to the staging environment value.
1475+
Now, the `main` property points to the generated code entry-point, no environment is defined,
1476+
and the `MY_VARIABLE` variable is resolved to the staging environment value.
14491477

1450-
And the `.wrangler/deploy/config.json` contains the path to the generated configuration file:
1478+
And the `.wrangler/deploy/config.json` contains the path to the generated configuration file:
14511479

1452-
```json
1453-
{
1454-
"configPath": "../../dist/wrangler.jsonc"
1455-
}
1456-
```
1480+
```json
1481+
{
1482+
"configPath": "../../dist/wrangler.jsonc"
1483+
}
1484+
```

0 commit comments

Comments
 (0)