stave.dev

Asset Management

CSS, JavaScript, and components auto-loaded by naming convention. No build step. No manifest file.

Directory Structure

Each module keeps its assets in an assets/ directory alongside its controller. For a module named HelloWorld with a sayHello() endpoint, a full layout looks like this:

HelloWorld.php
assets/
├── css/
│   ├── hello-world.css    → every HelloWorld endpoint
│   └── say-hello.css      → /hello-world/say-hello only
├── js/
│   ├── hello-world.js     → every HelloWorld endpoint
│   └── say-hello.js       → /hello-world/say-hello only
└── component/
    └── greeting-card.html → included on every HelloWorld page
view/

Automatic Symlinking

Module assets aren't served from the modules/ directory. Module::__construct() (core/Module.php) links the module's assets/ folder into the public web root each time the module is built:

modules/HelloWorld/assets/
→
{WEB_ROOT}/assets/helloworld/

The link name is the class's path below its module namespace (Modules::classRest()), lowercased: no hyphens, no PascalCase. A site's Zero\Module\HelloWorld is /assets/helloworld/, and so is the bundled Zero\Core\Module\HelloWorld in core/modules/.

Module::linkAssets() decides what to do with whatever is already at that path:

At the link path Result
Nothing The link is created, when the module has an assets/ folder
A link whose target still exists Left alone
A link whose target no longer exists (the module moved folders) Removed and created again, pointing at the new folder
A real directory Left alone
Anything, and the module is off No link is created
A module that is off

A module switched off in app/modules.ini gets no asset link, and every existing link into its folder is removed on the first request after the change. See Bundled Modules.

Auto-Loading by Convention

A file in assets/css/ or assets/js/ is loaded when its name matches the module or the endpoint of the request (Response::loadAssetTypeFromDir() in core/Response.php):

File Name Pattern Loaded When Example
{module}.css / {module}.js Every endpoint in the module hello-world.css loads for all HelloWorld pages
{endpoint}.css / {endpoint}.js That specific endpoint only say-hello.css loads only for /hello-world/say-hello

Names are kebab-case, as the URL spells them. The converted forms match too: the lowercased class name (helloworld.css) and the camelCase method name (sayHello.css). Any other file in the folder is not loaded.

A request for /hello-world/say-hello loads all four files of the tree above. A request for /hello-world has the endpoint index, so only hello-world.css and hello-world.js load.

The Version on Each Tag

Each tag carries the file's modification time:

<link rel="stylesheet" type="text/css" href="/assets/helloworld/css/hello-world.css?v=1794503893">
<script src="/assets/helloworld/js/say-hello.js?v=1794503893"></script>

A changed file has a new URL, so browsers fetch it. Without the version, and with a server that sends no Cache-Control, a browser can keep an old copy long after a deploy and run it against new markup. An asset a frame includes by hand needs the same ?v=filemtime; the skeleton's head.php does it for main.css:

<link rel="stylesheet" href="/assets/css/main.css?v=<?= @filemtime(ZERO_ROOT . 'app/frontend/www/assets/css/main.css') ?>">
<?php $this->getStylesheets() ?>

Auto-Load Helper Methods

Three protected methods on Response write the tags. A frame calls them as $this->...:

Method Output Called by the skeleton frame in
$this->getStylesheets() <link> tags for the matching files in css/ head.php
$this->getScripts() <script> tags for the matching files in js/ footer.php
$this->getComponents() The contents of the component files, included into the page Not called. The skeleton uses no components

Components

getComponents() includes every .html file from two folders, in this order:

  1. Global components: app/frontend/www/shadow-component/components/, on every page.
  2. The module's components: modules/{Module}/assets/component/, on that module's pages.

What goes in a component file is covered under ShadowComponent.

Submodule Assets

A submodule has its own assets/ folder, linked one level down:

modules/Ttrpg/submodule/Characters/assets/
→
{WEB_ROOT}/assets/ttrpg/characters/

Standalone Pages

A standalone page such as /about belongs to the Index module, so its assets are Index's: /about loads index.css and about.css from modules/Index/assets/css/.

Frame Overrides

If a module provides its own frame files (for example modules/MyModule/frame/head.php), you must call the auto-load helpers manually to preserve automatic asset loading:

<head>
    <meta charset="UTF-8">
    <title><?php echo $this->title; ?></title>

    <!-- Your custom head content -->
    <link rel="stylesheet" href="/assets/mymodule/css/custom-base.css">

    <!-- Preserve auto-loading -->
    <?php $this->getStylesheets(); ?>
    <?php $this->getScripts(); ?>
    <?php $this->getComponents(); ?>
</head>
Don't Forget the Helpers

If you override frame files without calling $this->getStylesheets() and $this->getScripts(), your module's convention-based CSS and JS files won't load. The framework has no other mechanism to inject them.

Non-Convention Assets

Files that don't match the {module} or {endpoint} naming pattern won't be auto-loaded. To include them, reference them directly in your frame or view:

<!-- Manually include a non-convention asset -->
<link rel="stylesheet" href="/assets/mymodule/css/vendor-lib.css">
<script src="/assets/mymodule/js/chart-library.js"></script>

<!-- Then auto-load convention-based assets -->
<?php $this->getStylesheets(); ?>
<?php $this->getScripts(); ?>

This is useful for third-party libraries, shared utility files, or any CSS/JS that doesn't follow the naming convention.

Admin Assets

Admin screens are the exception: AdminResponse overrides getStylesheets() and getScripts() to write the files inline, so admin CSS and JS are never web-accessible. See Admin.