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
In this tutorial, you will create a React SPA that can be deployed as a Worker with Workers Assets.
12
+
This tutorial takes you through the steps needed to adapt a Vite project to use the Cloudflare Vite plugin.
13
+
Much of the content can also be applied to adapting existing Vite projects and to front-end frameworks other than React.
14
+
15
+
:::note
16
+
If you just 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
+
:::
18
+
19
+
### Introduction
20
+
21
+
In this tutorial, you will create a React SPA that can be deployed as a Worker with static assets.
13
22
You will then add an API Worker that can be accessed from the front-end code.
14
23
You will develop, build, and preview the application using Vite before finally deploying to Cloudflare.
15
24
@@ -66,7 +75,7 @@ Note that the [`directory`](/workers/static-assets/binding/#directory) field is
66
75
The `directory` in the output configuration will automatically point to the client build output.
67
76
68
77
:::note
69
-
When using the Cloudflare Vite plugin, the Worker config (for example, `wrangler.toml`) that you provide is the input configuration file.
78
+
When using the Cloudflare Vite plugin, the Worker config (for example, `wrangler.jsonc`) that you provide is the input configuration file.
70
79
A separate output `wrangler.json` file is created when you run `vite build`.
71
80
This output file is a snapshot of your configuration at the time of the build and is modified to reference your build artifacts.
72
81
It is the configuration that is used for preview and deployment.
@@ -128,14 +137,11 @@ However, this tutorial will show you how to go a step further and add an API Wor
The assets `binding` defined here will allow you to access the assets functionality from your Worker.
138
-
139
145
#### Add your API Worker
140
146
141
147
```ts
@@ -155,14 +161,16 @@ export default {
155
161
});
156
162
}
157
163
158
-
returnenv.ASSETS.fetch(request);
164
+
returnnewResponse(null, { status: 404 });
159
165
},
160
166
} satisfiesExportedHandler<Env>;
161
167
```
162
168
163
-
The Worker above will be invoked for any request not matching a static asset.
164
-
It returns a JSON response if the `pathname` starts with `/api/` and otherwise passes the incoming request through to the assets binding.
165
-
This means that for paths that do not start with `/api/`, the `not_found_handling` behavior defined in the Worker config will be evaluated and the `index.html` file will be returned, enabling SPA navigations.
169
+
The Worker above will be invoked for any non-navigation request that does not match a static asset.
170
+
It returns a JSON response if the `pathname` starts with `/api/` and otherwise return a `404` response.
171
+
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.
166
174
167
175
#### Call the API from the client
168
176
@@ -232,7 +240,8 @@ Now, if you click the button, it will display 'Name from API is: Cloudflare'.
232
240
Increment the counter to update the application state in the browser.
233
241
Next, edit `api/index.ts` by changing the `name` it returns to `'Cloudflare Workers'`.
234
242
If you click the button again, it will display the new `name` while preserving the previously set counter value.
235
-
With Vite and the Cloudflare plugin, you can iterate on the client and server parts of your app quickly without losing UI state between edits.
243
+
244
+
With Vite and the Cloudflare plugin, you can iterate on the client and server parts of your app together, without losing UI state between edits.
0 commit comments