Inlining static assets refers to embedding the content of a static asset directly in an HTML or JS file, instead of linking to an external file. This can improve website performance by reducing the number of HTTP requests the browser needs to make to load the page.
However, inlining static assets has some disadvantages, such as increasing the size of a single file, which may lead to slower loading. Therefore, you should decide whether to inline assets based on your specific situation.
Rsbuild automatically inlines static assets smaller than 4KiB. Sometimes you may need to manually control whether assets are inlined. This document explains how to precisely control the inlining behavior of static assets.
By default, Rsbuild inlines assets when the file size is less than a threshold (default is 4KiB). When inlined, the asset is converted to a Base64 encoded string and no longer requires a separate HTTP request. When the file size is greater than this threshold, it is loaded as a separate file with its own HTTP request.
You can modify the threshold with the output.dataUriLimit config. For example, set the threshold for images to 5000 bytes and disable inlining for media assets:
You can force an asset to be inlined by adding the inline
query parameter when importing it, regardless of the asset's size.
In the above example, the foo.png
image will always be inlined, regardless of the image size.
When you reference a static asset in your CSS file, you can also force inline it with the inline
query parameter.
Inlining large assets will significantly increase the first paint time or first contentful paint time of a page, degrading user experience. If you inline a static asset multiple times in a CSS file, the base64 content will be injected repeatedly, increasing bundle size. Use forced inlining with caution.
To prevent assets from being inlined and ensure they're always loaded as separate files (regardless of their size), add the url
query parameter.
In the above example, the foo.png
image will always be loaded as a separate file, even if it's smaller than the threshold.
When you reference a static asset in your CSS file, you can also prevent inlining with the url
query parameter.
Disabling asset inlining will increase the number of assets the web application needs to load. This will reduce loading efficiency in weak network environments or scenarios where HTTP/2 is not enabled. Use with caution.
In addition to inlining static assets into JS files, Rsbuild also supports inlining JS files into HTML files.
Just enable the output.inlineScripts config, and the generated JS files will not be written to the output directory, but will be directly inlined in the HTML file.
Inline JS files may cause the HTML file to become too large and will break HTTP caching. Use with caution.
You can also inline CSS files into HTML files.
Just enable the output.inlineStyles config, and the generated CSS file will not be written to the output directory, but will be directly inlined in the HTML file.
When you use URL queries such as ?inline
and ?url
in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, you can add type declarations for these URL queries. Create a src/env.d.ts
file and add the following type declarations:
@rsbuild/core
package is installed, you can reference the preset types provided by @rsbuild/core
: