close
logologo
Guide
Config
Plugin
API
Community
Version
Changelog
Rsbuild 0.x Doc
English
简体中文
Guide
Config
Plugin
API
Community
Changelog
Rsbuild 0.x Doc
English
简体中文
logologo
Overview
root
mode
plugins
logLevel
environments

dev

dev.assetPrefix
dev.browserLogs
dev.cliShortcuts
dev.client
dev.hmr
dev.lazyCompilation
dev.liveReload
dev.progressBar
dev.setupMiddlewares
dev.watchFiles
dev.writeToDisk

resolve

resolve.aliasStrategy
resolve.alias
resolve.conditionNames
resolve.dedupe
resolve.extensions
resolve.mainFields

source

source.assetsInclude
source.decorators
source.define
source.entry
source.exclude
source.include
source.preEntry
source.transformImport
source.tsconfigPath

output

output.assetPrefix
output.charset
output.cleanDistPath
output.copy
output.cssModules
output.dataUriLimit
output.distPath
output.emitAssets
output.emitCss
output.externals
output.filenameHash
output.filename
output.injectStyles
output.inlineScripts
output.inlineStyles
output.legalComments
output.manifest
output.minify
output.module
output.overrideBrowserslist
output.polyfill
output.sourceMap
output.target

html

html.appIcon
html.crossorigin
html.favicon
html.inject
html.meta
html.mountId
html.outputStructure
html.scriptLoading
html.tags
html.templateParameters
html.template
html.title

server

server.base
server.compress
server.cors
server.headers
server.historyApiFallback
server.host
server.htmlFallback
server.https
server.middlewareMode
server.open
server.port
server.printUrls
server.proxy
server.publicDir
server.strictPort

security

security.nonce
security.sri

tools

tools.bundlerChain
tools.cssExtract
tools.cssLoader
tools.htmlPlugin
tools.lightningcssLoader
tools.postcss
tools.rspack
tools.styleLoader
tools.swc

performance

performance.buildCache
performance.bundleAnalyze
performance.chunkSplit
performance.dnsPrefetch
performance.preconnect
performance.prefetch
performance.preload
performance.printFileSize
performance.profile
performance.removeConsole
performance.removeMomentLocale

moduleFederation

moduleFederation.options
📝 Edit this page on GitHub
Previous Pagesource.tsconfigPath
Next Pageoutput.charset

#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:
rsbuild.config.ts
export default {
  output: {
    assetPrefix: 'https://cdn.example.com/assets/',
  },
};

After the build, the URL of the JS bundle in the HTML file will be:

<script
  defer
  src="https://cdn.example.com/assets/static/js/index.ebc4ff4f.js"
></script>
  • Setting it to a relative path:
rsbuild.config.ts
export default {
  output: {
    assetPrefix: './',
  },
};

After the build, the URL of the JS bundle in the HTML file will be:

<script defer src="./static/js/index.ebc4ff4f.js"></script>

#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:

export default {
  output: {
    assetPrefix: '/foo/bar/',
  },
  server: {
    base: '/foo',
  },
};

#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 like https://cdn.example.com/assets/.
  • relative path: For example, ./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 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.assetPrefix only takes effect in production mode.
  • output.assetPrefix default value is the same as server.base.
  • output.assetPrefix automatically appends a trailing / by default.
  • The value of output.assetPrefix is 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.

See Rspack - Dynamically set publicPath.