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 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
+
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`.
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
+
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).
Copy file name to clipboardExpand all lines: src/content/docs/workers/vite-plugin/tutorial.mdx
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -147,12 +147,8 @@ The `main` field specifies the entry file for your Worker code.
147
147
### Add your API Worker
148
148
149
149
```ts title="worker/index.ts"
150
-
interfaceEnv {
151
-
ASSETS:Fetcher;
152
-
}
153
-
154
150
exportdefault {
155
-
fetch(request, env) {
151
+
fetch(request) {
156
152
const url =newURL(request.url);
157
153
158
154
if (url.pathname.startsWith("/api/")) {
@@ -163,7 +159,7 @@ export default {
163
159
164
160
returnnewResponse(null, { status: 404 });
165
161
},
166
-
} satisfiesExportedHandler<Env>;
162
+
} satisfiesExportedHandler;
167
163
```
168
164
169
165
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
172
168
:::note
173
169
For top-level navigation requests, browsers send a `Sec-Fetch-Mode: navigate` header.
174
170
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.
0 commit comments