Configuration and Bindings
Configuring a Worker with assets requires specifying a directory and, optionally, an assets binding, in your Worker's wrangler.toml
file. The assets binding allows you to dynamically fetch assets from within your Worker script (e.g. env.ASSETS.fetch()
), similarly to how you might with a make a fetch()
call with a Service binding.
Only one collection of static assets can be configured in each Worker.
The folder of static assets to be served. For many frameworks, this is the ./public/
, ./dist/
, or ./build/
folder.
Sometime there are files in the asset directory that should not be uploaded.
In this case, create a .assetsignore
file in the root of the assets directory.
This file takes the same format as .gitignore
.
Wrangler will not upload asset files that match lines in this file.
Example
You are migrating from a Pages project where the assets directory is dist
.
You do not want to upload the server-side Worker code nor Pages configuration files as public client-side assets.
Add the following .assetsignore
file:
Now Wrangler will not upload these files as client-side assets when deploying the Worker.
Controls whether assets will be served first on a matching request. experimental_serve_directly = true
(default) will serve any static asset matching a request, while experimental_serve_directly = false
will unconditionally invoke your Worker script.
Configuring the optional binding gives you access to the collection of assets from within your Worker script.
In the example above, assets would be available through env.ASSETS
.
Parameters
request: Request
Pass a Request object, URL string, or URL object. Requests made through this method havehtml_handling
andnot_found_handling
configuration applied to them.
Response
Promise<Response>
Returns a static asset response for the given request.
Example
Your dynamic code can make new, or forward incoming, requests to your project's static assets using the assets binding.
Take the following example that configures a Worker script to return a response under all requests headed for /api/
. Otherwise, the Worker script will pass the incoming request through to the asset binding. In this case, because a Worker script is only invoked when the requested route has not matched any static assets, this will always evaluate not_found_handling
behavior.
For the various static asset routing configuration options, refer to Routing.
Smart Placement can be used to place a Worker's code close to your back-end infrastructure. Smart Placement will only have an effect if you specified a main
, pointing to your Worker code.
If you desire to run your Worker code ahead of assets by setting experimental_serve_directly=false
, all requests must first travel to your Smart-Placed Worker. As a result, you may experience increased latency for asset requests.
Use Smart Placement with experimental_serve_directly=false
when you need to integrate with other backend services, authenticate requests before serving any assets, or if your want to make modifications to your assets before serving them.
If you want some assets served as quickly as possible to the user, but others to be served behind a smart-placed Worker, considering splitting your app into multiple Workers and using service bindings to connect them.
Enabling Smart Placement with experimental_serve_directly=true
lets you serve assets from as close as possible to your users, but moves your Worker logic to run most efficiently (such as near a database).
Use Smart Placement with experimental_serve_directly=true
when prioritizing fast asset delivery.
This will not impact the default routing behavior.