Skip to content

Commit 501562e

Browse files
committed
Update Vite plugin static assets page
1 parent bcefaea commit 501562e

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

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 entry 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+
This includes importing assets as URLs, inlining assets, importing assets as strings and importing assets from the `public` directory.
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 assetPath from "./my-image.svg";
49+
50+
export default {
51+
async fetch(request, env) {
52+
return env.ASSETS.fetch(new URL(assetPath, request.url));
53+
},
54+
};
55+
```
56+
57+
Assets imported as URLs in your Worker will automatically be moved from the Worker 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 />

0 commit comments

Comments
 (0)