output.assetPrefix

  • Type: string | 'auto'
  • Default: '/'

When deploying to the production environment, you can use this option to set the URL prefix for static assets, such as setting it to a CDN URL.

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 the production build. During development, please use the dev.assetPrefix to set the URL prefix.

Example

After setting, the URLs of JavaScript, CSS and other static files will be prefixed with output.assetPrefix:

export default {
  output: {
    assetPrefix: 'https://cdn.example.com/assets/',
  },
};

After building, you can see that the JS files are loaded from:

<script
  defer
  src="https://cdn.example.com/assets/static/js/main.ebc4ff4f.js"
></script>

Path Types

assetPrefix can be set to the following types of paths:

  • absolute path: This is the most common practice, can be specific server paths, like /assets/, or setting to CDN paths, like https://cdn.example.com/assets/.
  • 'auto': Rspack will automatically calculate the path and generate relative paths based on file location.
TIP

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.

Compare with publicPath

The functionality of output.assetPrefix is basically the same as the output.publicPath config in Rspack.

The differences from the native configuration are as follows:

  • output.assetPrefix only takes effect in the production build.
  • output.assetPrefix automatically appends a trailing / by default.
  • The value of output.assetPrefix is written to the process.env.ASSET_PREFIX environment variable (can only be accessed in client code).