output.assetPrefix
- Type:
string | 'auto' - Default: server.base
In production mode, use this option to configure the URL prefix for static assets, such as a CDN URL.
assetPrefix affects the URLs of most 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 configuration is only used in production mode or none mode. In development mode, use dev.assetPrefix to configure the URL prefix.
Example
Setting output.assetPrefix will add the value as a prefix to the URLs of all static assets like JavaScript, CSS, images, etc.
- For example, setting it to a CDN address:
After the build, the URL of the JS bundle in the HTML file will be:
- Setting it to a relative path:
After the build, the URL of the JS bundle in the HTML file will be:
Default value
The default value of output.assetPrefix is the same as server.base.
When server.base is /foo, index.html and static assets can be accessed through http://localhost:3000/foo/.
It should be noted that when customizing the output.assetPrefix option, if you want static assets to be accessible normally during Rsbuild preview, output.assetPrefix should contain the same URL prefix as server.base. For example:
Path types
assetPrefix can be set to the following types of paths:
- absolute path: This is the most common practice, which can be specific server paths like
/assets/, or CDN paths likehttps://cdn.example.com/assets/. - relative path: For example,
./assets/. - 'auto': Rspack will automatically calculate the path and generate relative paths based on file location.
It's not recommended to set assetPrefix as a relative path like './assets/'. This is because when assets are at different path depths, using relative paths may cause assets to load incorrectly.
Compare with publicPath
The functionality of output.assetPrefix is basically the same as the output.publicPath config in Rspack.
Key differences from the native configuration:
output.assetPrefixonly takes effect in production mode.output.assetPrefixdefault value is the same as server.base.output.assetPrefixautomatically appends a trailing/by default.- The value of
output.assetPrefixis added to the process.env.ASSET_PREFIX environment variable (can only be accessed in client code).
Dynamic asset prefix
Use the __webpack_public_path__ variable provided by Rspack to dynamically set the URL prefix of static assets in JavaScript code.

