Static assets
Let’s add a file the browser can request on its own. We’ll put it under static/ and build its URL in a BSX view.
Use a file path for each public URL
Start in the static/ directory under the application’s working directory. Bwf serves files from this tree. If you give a file a new name for a release, update the URL in the view and ship the renamed file with the application.
Open a file under /static/
Start the server in the directory containing static/. If you put the stylesheet in a subdirectory, keep that part of its path when you request it. One easy detail to miss is that the browser resolves url("./fonts/inter.woff2") relative to the URL of the stylesheet itself.
| File | Browser URL |
|---|---|
static/site.css | /static/site.css |
static/app.mjs | /static/app.mjs |
static/images/logo.svg | /static/images/logo.svg |
static/fonts/inter.woff2 | /static/fonts/inter.woff2 |
Try a small stylesheet from the project root and request it from the running app.
mkdir -p static
printf '%s\n' 'body { color: #25322e; }' > static/guide-example.css
curl -fsS http://127.0.0.1:18088/static/guide-example.cssLink the file from a BSX view
Once the browser can fetch the file, link to it from the view. Import Static and Html. Put the stylesheet in the layout if every page needs it. A script used by only one view can live with that view.
<link rel="stylesheet" href={Static.asset_path("site.css")} />
{Html.script_module(Static.asset_path("app.mjs"))}
<img src={Static.asset_path("images/logo.svg")} alt="Project logo" />See where the encoded URL comes from
Static.asset_path adds /static/ and encodes characters such as spaces without escaping the slashes between folders. The server looks up the file on request. For application routes, use the OpenAPI builder to check the operation and parameter types.
This example keeps the image’s folder and encodes the space in the filename.
logo_url = Static.asset_path("images/site logo.svg")Check how the server found a file
Requests under /static/ are handled before controllers. Any file you put there is public. Serve protected files from an application route that checks access.
The server accepts GET and HEAD and chooses the media type from the extension. Unknown extensions receive application/octet-stream. For a 404, check the working directory, the filename and whether the file was built.
curl -I http://127.0.0.1:18088/static/app.cssEdit the source stylesheet
Put hand-written CSS in static/. For the Journal’s Tailwind stylesheet, edit tailwind/app.css. Its @source directives select the views to scan. After npm ci, the build writes static/app.css and prepares the browser modules.
Keep these directives in the Tailwind entry point.
@import "tailwindcss";
@source "../examples/canonical/View";Use a fresh filename for a changed file
The built-in server sends Cache-Control: max-age=0. If a CDN keeps files for longer, use a different name when the contents change and link to it from the new deployment.
The version is part of the filename below. Make sure that file ships under static/.
<link rel="stylesheet" href={Static.asset_path("site.v2.css")} />