Skip to content

Commit 2f5b790

Browse files
committed
Updated tutorial
1 parent e8fee20 commit 2f5b790

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/content/docs/workers/vite-plugin/tutorial.mdx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
pcx_content_type: tutorial
3-
title: Tutorial
3+
title: Tutorial - React SPA
44
head: []
55
sidebar:
66
order: 2
@@ -9,7 +9,16 @@ description: Create a React SPA with an API Worker using the Vite plugin
99

1010
import { PackageManagers, WranglerConfig } from "~/components";
1111

12-
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.
1322
You will then add an API Worker that can be accessed from the front-end code.
1423
You will develop, build, and preview the application using Vite before finally deploying to Cloudflare.
1524

@@ -66,7 +75,7 @@ Note that the [`directory`](/workers/static-assets/binding/#directory) field is
6675
The `directory` in the output configuration will automatically point to the client build output.
6776

6877
:::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.
7079
A separate output `wrangler.json` file is created when you run `vite build`.
7180
This output file is a snapshot of your configuration at the time of the build and is modified to reference your build artifacts.
7281
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
128137
```toml
129138
name = "cloudflare-vite-tutorial"
130139
compatibility_date = "2024-12-30"
131-
assets = { not_found_handling = "single-page-application", binding = "ASSETS" }
140+
assets = { not_found_handling = "single-page-application" }
132141
main = "./api/index.ts"
133142
```
134143

135144
</WranglerConfig>
136-
137-
The assets `binding` defined here will allow you to access the assets functionality from your Worker.
138-
139145
#### Add your API Worker
140146

141147
```ts
@@ -155,14 +161,16 @@ export default {
155161
});
156162
}
157163

158-
return env.ASSETS.fetch(request);
164+
return new Response(null, { status: 404 });
159165
},
160166
} satisfies ExportedHandler<Env>;
161167
```
162168

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.
166174

167175
#### Call the API from the client
168176

@@ -232,7 +240,8 @@ Now, if you click the button, it will display 'Name from API is: Cloudflare'.
232240
Increment the counter to update the application state in the browser.
233241
Next, edit `api/index.ts` by changing the `name` it returns to `'Cloudflare Workers'`.
234242
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.
236245

237246
#### Build your application
238247

0 commit comments

Comments
 (0)