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/reference/static-assets.mdx
+36-9Lines changed: 36 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,14 +8,17 @@ description: Static assets and the Vite plugin
8
8
9
9
import { WranglerConfig } from"~/components";
10
10
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/).
13
13
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)
19
22
20
23
On running `vite build`, an output `wrangler.json` configuration file is generated as part of the build output.
21
24
The `assets.directory` field in this file is automatically populated with the path to your `client` build output.
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`.
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
+
35
64
## Headers and redirects
36
65
37
66
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).
38
67
The paths in these files should reflect the structure of your client build output.
39
68
For example, generated assets are typically located in an [assets subdirectory](https://vite.dev/config/build-options#build-assetsdir).
0 commit comments