Skip to content

Commit 2fff8d8

Browse files
authored
Update Vite plugin static assets docs and add changelog entry (#23283)
* Update Vite plugin static assets page * Reference run_worker_first in tutorial * Add changelog entry * PR feedback * Updated changelog date
1 parent fa0e0d8 commit 2fff8d8

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Enhanced support for static assets with the Cloudflare Vite plugin
3+
description: The Cloudflare Vite plugin now supports using all of Vite's static assets features in your Worker
4+
products:
5+
- workers
6+
date: 2025-07-01T01:00:00Z
7+
---
8+
9+
You can now use any of Vite's [static asset handling](https://vite.dev/guide/assets) features in your Worker as well as in your frontend.
10+
These include importing assets as URLs, importing as strings and importing from the `public` directory as well as inlining assets.
11+
12+
Additionally, assets imported as URLs in your Worker are now automatically moved to the client build output.
13+
14+
Here is an example that fetches an imported asset using the [assets binding](/workers/static-assets/binding/#binding) and modifies the response.
15+
16+
```ts
17+
// Import the asset URL
18+
// This returns the resolved path in development and production
19+
import myImage from "./my-image.png";
20+
21+
export default {
22+
async fetch(request, env) {
23+
// Fetch the asset using the binding
24+
const response = await env.ASSETS.fetch(new URL(myImage, request.url));
25+
// Create a new `Response` object that can be modified
26+
const modifiedResponse = new Response(response.body, response);
27+
// Add an additional header
28+
modifiedResponse.headers.append("my-header", "imported-asset");
29+
30+
// Return the modfied response
31+
return modifiedResponse;
32+
},
33+
};
34+
```
35+
36+
Refer to [Static Assets](/workers/vite-plugin/reference/static-assets/) in the Cloudflare Vite plugin docs for more info.

src/content/docs/workers/vite-plugin/reference/static-assets.mdx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ description: Static assets and the Vite plugin
88

99
import { WranglerConfig } from "~/components";
1010

11-
The Vite plugin does not require that you provide the `assets` field in order to enable assets and instead determines whether assets should be included based on whether the `client` environment has been built.
12-
By default, the `client` environment is built if there is an `index.html` file in the root of your project or if `build.rollupOptions.input` is specified in the Vite config.
11+
This guide focuses on the areas of working with static assets that are unique to the Vite plugin.
12+
For more general documentation, see [Static Assets](/workers/static-assets/).
1313

14-
:::note
15-
When using the Cloudflare Vite plugin, the `client` environment is deployed as your static assets.
16-
This typically includes files such as static HTML, front-end JavaScript, CSS, images and fonts.
17-
For more information about using static assets in Vite, refer to [Static Asset Handling](https://vite.dev/guide/assets).
18-
:::
14+
## Configuration
15+
16+
The Vite plugin does not require that you provide the `assets` field in order to enable assets and instead determines whether assets should be included based on whether the `client` environment has been built. By default, the `client` environment is built if any of the following conditions are met:
17+
18+
- There is an `index.html` file in the root of your project
19+
- `build.rollupOptions.input` or `environments.client.build.rollupOptions.input` is specified in your Vite config
20+
- You have a non-empty [`public` directory](https://vite.dev/guide/assets#the-public-directory)
21+
- Your Worker [imports assets as URLs](https://vite.dev/guide/assets#importing-asset-as-url)
1922

2023
On running `vite build`, an output `wrangler.json` configuration file is generated as part of the build output.
2124
The `assets.directory` field in this file is automatically populated with the path to your `client` build output.
@@ -32,10 +35,34 @@ assets = { not_found_handling = "single-page-application" }
3235

3336
</WranglerConfig>
3437

38+
## Features
39+
40+
The Vite plugin ensures that all of Vite's [static asset handling](https://vite.dev/guide/assets) features are supported in your Worker as well as in your frontend.
41+
These include importing assets as URLs, importing as strings and importing from the `public` directory as well as inlining assets.
42+
43+
Assets [imported as URLs](https://vite.dev/guide/assets#importing-asset-as-url) can be fetched via the [assets binding](/workers/static-assets/binding/#binding).
44+
As the binding's `fetch` method requires a full URL, we recommend using the request URL as the `base`.
45+
This is demonstrated in the following example:
46+
47+
```ts
48+
import myImage from "./my-image.png";
49+
50+
export default {
51+
fetch(request, env) {
52+
return env.ASSETS.fetch(new URL(myImage, request.url));
53+
},
54+
};
55+
```
56+
57+
Assets imported as URLs in your Worker will automatically be moved to the client build output.
58+
When running `vite build` the paths of any moved assets will be displayed in the console.
59+
60+
:::note
61+
If you are developing a multi-Worker application, assets can only be accessed on the client and in your entry Worker.
62+
:::
63+
3564
## Headers and redirects
3665

3766
Custom [headers](/workers/static-assets/headers/) and [redirects](/workers/static-assets/redirects/) are supported at build, preview and deploy time by adding `_headers` and `_redirects` files to your [`public` directory](https://vite.dev/guide/assets#the-public-directory).
3867
The paths in these files should reflect the structure of your client build output.
3968
For example, generated assets are typically located in an [assets subdirectory](https://vite.dev/config/build-options#build-assetsdir).
40-
41-
<br />

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,8 @@ The `main` field specifies the entry file for your Worker code.
147147
### Add your API Worker
148148

149149
```ts title="worker/index.ts"
150-
interface Env {
151-
ASSETS: Fetcher;
152-
}
153-
154150
export default {
155-
fetch(request, env) {
151+
fetch(request) {
156152
const url = new URL(request.url);
157153

158154
if (url.pathname.startsWith("/api/")) {
@@ -163,7 +159,7 @@ export default {
163159

164160
return new Response(null, { status: 404 });
165161
},
166-
} satisfies ExportedHandler<Env>;
162+
} satisfies ExportedHandler;
167163
```
168164

169165
The Worker above will be invoked for any non-navigation request that does not match a static asset.
@@ -172,6 +168,21 @@ It returns a JSON response if the `pathname` starts with `/api/` and otherwise r
172168
:::note
173169
For top-level navigation requests, browsers send a `Sec-Fetch-Mode: navigate` header.
174170
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.
171+
This implicit routing is the default behavior.
172+
173+
If you would instead like to define the routes that invoke your Worker explicitly, you can provide an array of route patterns to [`run_worker_first`](/workers/static-assets/binding/#run_worker_first).
174+
This opts out of interpreting the `Sec-Fetch-Mode` header.
175+
176+
<WranglerConfig>
177+
178+
```toml
179+
name = "cloudflare-vite-tutorial"
180+
compatibility_date = "2025-04-03"
181+
assets = { not_found_handling = "single-page-application", run_worker_first = ["/api/*"] }
182+
main = "./worker/index.ts"
183+
```
184+
185+
</WranglerConfig>
175186
:::
176187

177188
### Call the API from the client

0 commit comments

Comments
 (0)