Static assets
Edit this pageWithin SolidStart there are two ways to import static assets into your project: using the public directory and using imports.
Public directory
Rich web applications use assets to create visuals.
In SolidStart, the /public
directory can be used to store static assets.
These assets are served at the exact path they are in, relative to the public directory:
If you would like to reference an asset in the public directory, you can use the absolute path to the asset:
This is ideal when you want to have human-readable, stable references to static assets. This can be useful for assets such as:
- documents
- service workers
- images, audio, and video
- manifest files
- metadata files (e.g.,
robots.txt
, sitemaps) - favicon
Importing assets
Vite provides a way to import assets directly into your Solid components:
When you use imports, Vite will create a hashed filename.
For example, solid.png
will become solid.2d8efhg.png
.
Public directory versus imports
The public directory and imports are both valid ways to include static assets in your project. The driver to using one over the other is based on your use case.
For dynamic updates to your assets, using the public directory is the best choice. It allows you to maintain full control over the asset URL paths, ensuring that the links remain consistent even when the assets are updated.
When using imports, the filename is hashed and therefore will not be predictable over time. This can be beneficial for cache busting but detrimental if you want to send someone a link to the asset.