boolean | string | 'auto'
Set the URL prefix of static assets in development mode.
assetPrefix
will affect the URLs of most of the static assets, including JavaScript files, CSS files, images, videos, etc. If an incorrect value is specified, you'll receive 404 errors while loading these resources.
This config is only used in development mode. In the production mode, please use the output.assetPrefix to set the URL prefix.
The default value of dev.assetPrefix
is the same as server.base.
When server.base
is /foo
, index.html
and other static assets can be accessed through http://localhost:3000/foo/
.
It should be noted that when customizing the dev.assetPrefix
option, if you want static assets to be normally accessible through the Rsbuild dev server, dev.assetPrefix
should contain the same URL prefix as server.base
, such as:
If assetPrefix
is set to true
, the URL prefix will be http://localhost:<port>/
:
The resource URL loaded in the browser is as follows:
If assetPrefix
is set to false
or not set, /
is used as the default value.
When the value of assetPrefix
is a string
type, the string will be used as a prefix and automatically appended to the static resource URL.
The resource URL loaded in the browser is as follows:
The resource URL loaded in the browser is as follows:
The port number that Rsbuild server listens on may change. For example, if the port is in use, Rsbuild will automatically increment the port number until it finds an available port.
To avoid dev.assetPrefix
becoming invalid due to port changes, you can use one of the following methods:
<port>
placeholder to refer to the current port number. Rsbuild will replace the placeholder with the actual port number it is listening on.assetPrefix can be set to the following types of paths:
/assets/
.It's not recommended to set assetPrefix as a relative path, such as './assets/'
. This is because when assets are at different path depths, using relative paths may cause assets to load incorrectly.
publicPath
The functionality of dev.assetPrefix
is basically the same as the output.publicPath config in Rspack.
The differences from the native configuration are as follows:
dev.assetPrefix
only takes effect in development mode.dev.assetPrefix
default value is the same as server.base.dev.assetPrefix
automatically appends a trailing /
by default.dev.assetPrefix
is written to the process.env.ASSET_PREFIX environment variable (can only be accessed in client code).