{asset}
The address of a file of www/<app>/ with a version of its contents, safe behind a far-future cache header.
Syntax
{asset 'file'}
{asset 'file', tag}
Parameters
-
file -
A file relative to
www/<app>/-'charts.css','js/app.js'. It never leaves that directory.
Options
-
tag -
Bare option: write the element instead of the address -
<link rel="stylesheet">for.css,<script defer>for.js,<script type="module">for.mjs.
Description
{asset 'charts.css'} answers the address of www/<app>/charts.css with ?v= and the first ten hex digits of a hash of the file's contents: /charts/charts.css?v=1f3a9c0b2e. The hash is made once per request. A changed file gets a new address, so the server can send its assets with a cache lifetime of a year and a browser still fetches the new version the moment it changes - no build step, no manifest.
With the bare tag option it writes the element itself: a <link rel="stylesheet"> for a .css file, a <script defer> for .js and a <script type="module"> for .mjs, carrying the request's nonce when $padCsp asks for one. From PHP, padAsset ( 'charts.css' ) and padAssetTag ( 'app.js' ) give the same.
A name never leaves www/<app>/. A file that is not there is written as the plain address and, under the strict check, reported as an error. The hash follows the file, so the examples are shown rather than run. For the bundles of a Vite build, use {vite}.
Examples
A versioned address
The stylesheet's address carries a hash of its contents - /tags/tags.css?v=9c41e07a2b.
<link rel="stylesheet" href="{asset 'tags.css'}">
<img src="{asset 'img/logo.svg'}" alt="Logo">
The element written for you
tag writes a link for a stylesheet, a deferred script for .js and a module for .mjs.
{asset 'tags.css', tag}
{asset 'tags.js', tag}
{asset 'islands.mjs', tag}
See also
- {vite} Links the scripts and stylesheets of a Vite build by the name of their source - or the dev server while it runs.
- {nonce} This request's Content-Security-Policy nonce, for a script or style the policy lets run.
- {flush} Sends the page rendered so far to the browser at once, so it can load the stylesheets while a slow part below renders.