Building a site
Assets and import map
JavaScript and CSS reach the browser through an import map, without Node and without a build step. Your own files live in assets/, and npm packages are downloaded into assets/vendor, so nothing comes from a CDN while the site runs.
Every file gets a URL with a hash of its content, such as /assets/css/app-3f9a1c2e.css. Browsers may keep it for as long as they like, and a changed file gets a new URL.
The import map
importmap.php names the modules and the files behind them:
<?php // importmap.php return [ 'app' => ['path' => 'js/app.js', 'entrypoint' => true], 'app.css' => ['path' => 'css/app.css'], 'htmx.org' => ['version' => '2.0.11'], // assets/vendor/htmx.org.js ];
Put <x-importmap /> into the <head> of your layout. It writes the import map, the CSS links and the modulepreload links of the entry point. Pass entrypoint="admin" for another entry point, or :entrypoints="['app', 'gallery']" for several at once.
Relative imports such as import './nav.js' work as they are, because the import map maps the plain URLs to the hashed ones. url() references in CSS get hashed URLs too. In a template, $cms->asset('images/hero.jpg') returns the hashed URL of a file.
Packages from npm
Packages come from jsDelivr, with their dependencies:
./lapis importmap:require htmx.org ./lapis importmap:require swiper --css=swiper-bundle.min.css ./lapis importmap:remove htmx.org ./lapis importmap:install # after a fresh checkout
Every downloaded file's hash is recorded in importmap.php as its integrity, much like a lock file. importmap:install refuses a download that differs from it.
In production
In development, PHP serves the assets. For production, ./lapis assets:compile writes them into public/assets with a manifest, and the web server serves them from there. ./lapis deploy runs it for you, and ./lapis assets:clear goes back to serving them with PHP.
htmx
Lapis works well with htmx. Mark a part of a template with data-lapis-fragment, and an htmx request that targets it gets only that part. The page renders as usual, with what its page function returned, and the rest of it stays on the server:
<ul id="posts" data-lapis-fragment>…</ul> <button hx-get="?offset=10" hx-target="#posts" hx-swap="outerHTML">More posts</button>
For anything else, $cms->htmx('target-id') is true when htmx sent the request for that target, so a template can answer with a fragment of its own.
Fragments never go into the page cache, and the answer gets a Vary: HX-Request header.