GitHub

Bootloader

A module the page rarely needs, like an editor, does not have to be in its bundle. Name its files in the resource map and call it like any other module: the first response that calls it loads its CSS and JavaScript, then calls it.
  • Bootloader::setResourceMap()

    Names of the static files, e.g. from the manifest of the bundler.

  • Bootloader::enableBootload()

    The files a module needs; with a priority, they are preloaded while the network is idle.

  • registerModules()

    The loaded script makes the module available when it runs.

  • Pagelet::addCss(), addJs()

    Pagelets use the names of the map too.

Controller.php
Bootloader::setResourceMap([
    'editor.css' => ['type' => 'css', 'src' => '/bootloaded/editor.css'],
    'editor.js' => ['type' => 'js', 'src' => '/bootloaded/editor.js'],
]);
Bootloader::enableBootload(['Editor' => ['editor.css', 'editor.js']]);

return (new AsyncResponse())
    ->call('Editor', 'open', [TransportMarker::element('draft')])
    ->send();

// /bootloaded/editor.js, not in the bundle
registerModules({ Editor: class { open(textarea) { … } } });
Try it
Open the editor

Not in the bundle: public/bootloaded/editor.js

An editor loaded on demand

Open the editor: the response names editor.css and editor.js, and the browser loads them before it calls Editor. Open it again, or after a page transition: they are loaded already, nothing is requested.