Powered by Rsbuild, Rspeedy supports using static assets, including images, fonts, audios and videos.
Static assets are files that are part of a Lynx application and do not change, even when the application is being used.
Examples of static assets include images, fonts, medias, stylesheets, and JavaScript files. These assets are typically stored on a web server or CDN, and delivered to the users when the Lynx application is accessed.
Because they do not change, static assets can be cached by the App, which helps to improve the performance of the Lynx application.
You can directly import static assets in JavaScript:
The result of the import will be a URL string that represent the static asset. And the asset will be emitted to the output folder.
You can also use static assets in CSS:
The URL returned after importing a asset will automatically include the path prefix:
dev.assetPrefix
to set the path prefix.output.assetPrefix
to set the path prefix.For example, you can set output.assetPrefix
to https://example.com
:
The public
folder at the project root can be used to place some static assets. These assets will not be bundled by Rspeedy.
/
.In JavaScript code, you can splice the URL via process.env.ASSET_PREFIX
:
dist
). Please be careful to avoid name conflicts with the output files. When files in the public
folder have the same name as outputs, the outputs have higher priority and will overwrite the files with the same name in the public
folder.When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
To fix this, you need to add a type declaration file for the static assets, please create a src/rspeedy-env.d.ts
file, and add the corresponding type declaration.
Inline static assets refer to the practice of including the content of a static asset directly in the JS output, instead of linking to an external file. This can improve the performance of a Lynx application by reducing the number of HTTP requests that Lynx has to make to load the application.
However, static assets inlining also has some disadvantages, such as increasing the size of a single file, which may lead to slower loading. Therefore, in the actual scenario, it is necessary to decide whether to use static assets inlining according to the specific situation.
Rspeedy will inline assets when the file size of is less than a threshold (the default is 2KiB). When inlined, the asset will be converted to a base64-encoded string and will no longer send a separate HTTP request. When the file size is greater than this threshold, it is loaded as a separate file with a separate HTTP request.
The threshold can be modified with the output.dataUriLimit
config.
For example, set the threshold of images to 5000 bytes, and set media assets not to be inlined:
You can force an asset to be inlined by adding the inline
query when importing the asset, regardless of whether the asset's size is smaller than the size threshold.
In the above example, the foo.png
image will always be inlined, regardless of whether the size of the image is larger than the threshold.
When you want to always treat some assets as separate files, no matter how small the asset is, you can add the url
query to force the asset not to be inlined.
In the above example, the foo.png
image will always be loaded as a separate file, even if the size of the image is smaller than the threshold.
If the built-in asset types in Rsbuild cannot meet your requirements, you can:
source.assetsInclude
configuration option to specify additional file types to be treated as static assets.tools.rspack
.For example, if you want to treat *.pdf
files as assets and directly output them to the dist directory, you can add the following configuration:
After adding the above configuration, you can import *.pdf
files in your code, for example:
For more information about asset modules, please refer to Rspack - Asset modules.