本页还没有译文
本页 16 段还没译成日本語,下面显示的是英文原文。Static Assets
还没翻译译自英文(developers.cloudflare.com)本页 16 段都还没译
This guide focuses on the areas of working with static assets that are unique to the Vite plugin. For more general documentation, see Static Assets.
Configuration
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:
- There is an
index.htmlfile in the root of your project build.rollupOptions.inputorenvironments.client.build.rollupOptions.inputis specified in your Vite config- You have a non-empty
publicdirectory - Your Worker imports assets as URLs
On running vite build, an output wrangler.json configuration file is generated as part of the build output.
The assets.directory field in this file is automatically populated with the path to your client build output.
It is therefore not necessary to provide the assets.directory field in your input Worker configuration.
Cloudflare Access context
The Vite plugin can add Static Assets to the generated deployment configuration even when the input Wrangler configuration does not include assets. Workers with Static Assets do not receive ctx.access in the user Worker.
This behavior affects frameworks that use the Cloudflare Vite plugin, including TanStack Start. For more information, refer to ctx.access limitations.
The assets configuration should be used, however, if you wish to set routing configuration or enable the assets binding.
The following example configures the not_found_handling for a single-page application so that the fallback will always be the root index.html file.
wrangler.jsoncwrangler.toml
{
"assets": {
"not_found_handling": "single-page-application"
}
}[assets]
not_found_handling = "single-page-application"
Features
The Vite plugin ensures that all of Vite's static asset handling features are supported in your Worker as well as in your frontend.
These include importing assets as URLs, importing as strings and importing from the public directory as well as inlining assets.
Assets imported as URLs can be fetched via the assets binding.
As the binding's fetch method requires a full URL, we recommend using the request URL as the base.
This is demonstrated in the following example:
import myImage from "./my-image.png";
export default {
fetch(request, env) {
return env.ASSETS.fetch(new URL(myImage, request.url));
},
};Assets imported as URLs in your Worker will automatically be moved to the client build output.
When running vite build the paths of any moved assets will be displayed in the console.
Note
If you are developing a multi-Worker application, assets can only be accessed on the client and in your entry Worker.
Headers and redirects
Custom headers and redirects are supported at build, preview and deploy time by adding _headers and _redirects files to your public directory.
The paths in these files should reflect the structure of your client build output.
For example, generated assets are typically located in an assets subdirectory.