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: src/content/docs/workers/vite-plugin/get-started.mdx
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,10 @@ export default defineConfig({
52
52
});
53
53
```
54
54
55
+
The Cloudflare Vite plugin doesn't require any configuration by default and will look for a `wrangler.jsonc`, `wrangler.json` or `wrangler.toml` in the root of your application.
56
+
57
+
Refer to the [API reference](/workers/vite-plugin/api/) for configuration options.
58
+
55
59
### Create your Worker config file
56
60
57
61
<WranglerConfig>
@@ -64,6 +68,12 @@ main = "./src/index.ts"
64
68
65
69
</WranglerConfig>
66
70
71
+
The `name` field specifies the name of your Worker.
72
+
By default, this is also used as the name of the Worker's Vite Environment (see [Vite Environments](/workers/vite-plugin/vite-environments/) for more information).
73
+
The `main` field specifies the entry file for your Worker code.
74
+
75
+
For more information about the Worker configuration, see [Configuration](/workers/wrangler/configuration/).
76
+
67
77
### Create your Worker entry file
68
78
69
79
```ts
@@ -76,4 +86,8 @@ export default {
76
86
};
77
87
```
78
88
79
-
You can now develop (`npm run dev`), build (`npm run build`), preview (`npm run preview`), and deploy (`npm run deploy`) your application.
89
+
A request to this Worker will return 'Running in Cloudflare-Workers!', demonstrating that the code is running inside the Workers runtime.
90
+
91
+
### Dev, build, preview and deploy
92
+
93
+
You can now start the Vite development server (`npm run dev`), build the application (`npm run build`), preview the built application (`npm run preview`), and deploy to Cloudflare (`npm run deploy`).
Copy file name to clipboardExpand all lines: src/content/docs/workers/vite-plugin/tutorial.mdx
+24-13Lines changed: 24 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Much of the content can also be applied to adapting existing Vite projects and t
14
14
15
15
:::note
16
16
If you want to start a new app with a template already set up with Vite, React and the Cloudflare Vite plugin, refer to the [React framework guide](/workers/frameworks/framework-guides/react/).
17
+
To create a standalone Worker, refer to [Get started](/workers/vite-plugin/get-started/).
The [`not_found_handling`](/workers/static-assets/routing/#not_found_handling--404-page--single-page-application--none) value has been set to `single-page-application`.
70
71
This means that all not found requests will serve the `index.html` file.
71
72
With the Cloudflare plugin, the `assets` routing configuration is used in place of Vite's default behavior.
72
-
This ensures that your application's routing works the same way while developing as it does when deployed to production.
73
+
This ensures that your application's [routing configuration](/workers/static-assets/routing/#routing-configuration) works the same way while developing as it does when deployed to production.
73
74
74
75
Note that the [`directory`](/workers/static-assets/binding/#directory) field is not used when configuring assets with Vite.
75
76
The `directory` in the output configuration will automatically point to the client build output.
77
+
See [Static Assets](/workers/vite-plugin/static-assets/) for more information.
76
78
77
79
:::note
78
80
When using the Cloudflare Vite plugin, the Worker config (for example, `wrangler.jsonc`) that you provide is the input configuration file.
@@ -93,10 +95,10 @@ Add the following lines to your `.gitignore` file:
93
95
94
96
#### Run the development server
95
97
96
-
Run `npm run dev` to verify that your application is working as expected.
98
+
Run `npm run dev` to start the Vite development server and verify that your application is working as expected.
97
99
98
100
For a purely front-end application, you could now build (`npm run build`), preview (`npm run preview`), and deploy (`npm exec wrangler deploy`) your application.
99
-
However, this tutorial will show you how to go a step further and add an API Worker.
101
+
This tutorial, however, will show you how to go a step further and add an API Worker.
100
102
101
103
### Add an API Worker
102
104
@@ -113,7 +115,7 @@ However, this tutorial will show you how to go a step further and add an API Wor
Setting the `not_found_handling` to `single-page-application` means that navigation requests that do not match a static asset will always return the `index.html` file.
149
+
145
150
#### Add your API Worker
146
151
147
152
```ts
148
-
//api/index.ts
153
+
//worker/index.ts
149
154
150
155
interfaceEnv {
151
156
ASSETS:Fetcher;
@@ -169,8 +174,10 @@ export default {
169
174
The Worker above will be invoked for any non-navigation request that does not match a static asset.
170
175
It returns a JSON response if the `pathname` starts with `/api/` and otherwise return a `404` response.
171
176
172
-
Browser navigations will invoke the `not_found_handling` behavior defined in the Worker config.
173
-
As we have set this to `single-page-application`, the `index.html` file is always returned.
177
+
:::note
178
+
For top-level navigation requests, browsers send a `Sec-Fetch-Mode: navigate` header.
179
+
If this is present and the URL does not match a static asset, the `not_found_handling` behavior will be invoked rather than the Worker.
180
+
:::
174
181
175
182
#### Call the API from the client
176
183
@@ -247,8 +254,10 @@ With Vite and the Cloudflare plugin, you can iterate on the client and server pa
247
254
248
255
Run `npm run build` to build your application.
249
256
250
-
If you inspect the `dist` directory, you will see that it contains two subdirectories: `client` and `cloudflare-vite-tutorial`.
251
-
The `cloudflare-vite-tutorial` directory contains your Worker code and the output `wrangler.json` configuration.
257
+
If you inspect the `dist` directory, you will see that it contains two subdirectories:
258
+
259
+
-`client` - the client code that runs in the browser
260
+
-`cloudflare-vite-tutorial` - the Worker code and the output `wrangler.json` Worker configuration
252
261
253
262
#### Preview your application
254
263
@@ -262,10 +271,12 @@ This command will automatically use the output `wrangler.json` that was included
262
271
263
272
### Next steps
264
273
265
-
In this tutorial, we created an SPA that could be deployed as a Worker with Workers Assets.
266
-
We then added an API Worker that could be accessed from the front-end code and deployed to Cloudflare.
274
+
In this tutorial, we created an SPA that could be deployed as a Worker with static assets.
275
+
We then added an API Worker that could be accessed from the front-end code.
276
+
Finally, we deployed both the client and server-side parts of the application to Cloudflare.
277
+
267
278
Possible next steps include:
268
279
269
280
- Adding a binding to another Cloudflare service such as a [KV namespace](/kv/) or [D1 database](/d1/)
270
281
- Expanding the API to include additional routes
271
-
- Using a library, such as [tRPC](https://trpc.io/) or [Hono](https://hono.dev/), in your API Worker
282
+
- Using a library, such as [Hono](https://hono.dev/) or [tRPC](https://trpc.io/), in your API Worker
0 commit comments