|
| 1 | +# 0010 Lazily loaded pattern implementations |
| 2 | + |
| 3 | +Status: Accepted (2026-11-29) |
| 4 | + |
| 5 | +## Context |
| 6 | + |
| 7 | +All patterns imported in `src/patterns.js` end up in one eagerly loaded |
| 8 | +patterns chunk, which every page loads. Before this change it was about |
| 9 | +201 KB minified, of which about 148 KB was pattern code and about 10 KB were |
| 10 | +lookup maps of dynamic import contexts (ADR 0007). Heavy third party |
| 11 | +libraries were already imported dynamically, but the pattern code itself |
| 12 | +was not, even on pages which don't use the pattern. |
| 13 | + |
| 14 | +Mockup already splits patterns into a thin registration module and a lazily |
| 15 | +imported `--implementation` module. The convention started there with the |
| 16 | +controlpanel patterns (Mockup commit fe0380c46, 2021-11-25) and is used for |
| 17 | +several patterns since (e.g. Mockup's `pat/modal/modal.js`). Mockup's |
| 18 | +patterns are `Base` patterns though, while the patterns split here are class |
| 19 | +based (ADR 0009). See "Alternatives considered". |
| 20 | + |
| 21 | +Other code instantiates patterns from the registry directly, e.g. |
| 22 | +`new registry.patterns.markdown(el)` in pat-tooltip or |
| 23 | +`await new registry.patterns[name](el)` in Mockup. Registry code of other |
| 24 | +Module Federation bundles also instantiates registry entries with `new`. |
| 25 | + |
| 26 | +## Decision |
| 27 | + |
| 28 | +- `core/lazy-pattern.js` provides `create_lazy_pattern({name, trigger, |
| 29 | + order, load})`. It returns a small placeholder class which is registered |
| 30 | + instead of the real class. Instantiating it loads the implementation and |
| 31 | + returns a Promise of the real pattern instance. A class constructor may |
| 32 | + return an object, so the registry and other code using `new` keep working |
| 33 | + unchanged. Once loaded, the registry entry is replaced by the real class. |
| 34 | +- `events.await_pattern_init` also accepts such a Promise, so |
| 35 | + `await events.await_pattern_init(new Pattern(el))` keeps working. |
| 36 | +- A split pattern `pat/NAME/NAME.js` only contains the registration and |
| 37 | + module level code which must run on page load. The class lives in |
| 38 | + `pat/NAME/NAME--implementation.js`, which still registers itself, so |
| 39 | + importing it directly gives an eagerly registered pattern. Code which needs |
| 40 | + the class or the parser imports the `--implementation` module. |
| 41 | +- Split are patterns where it makes a difference (roughly 2 KB minified or |
| 42 | + more, including dynamic import lookup maps): calendar, navigation, |
| 43 | + scroll-marker, auto-suggest, date-picker, datetime-picker, display-time, |
| 44 | + sortable, modal, notification, bumper, image-crop, clone, masonry, |
| 45 | + gallery, carousel, auto-submit, markdown and syntax-highlight. |
| 46 | +- Kept eager, because lazy loading would break them or change what users |
| 47 | + see: |
| 48 | + - pat-inject and pat-ajax: shared services, used by many patterns and on |
| 49 | + almost every page anyway (ADR 0009). |
| 50 | + - pat-validation: must register its submit handlers before pat-inject. |
| 51 | + - pat-tooltip: imported as class and parser by Mockup and pat-tiptap. |
| 52 | + - pat-depends, pat-collapsible, pat-stacks, pat-toggle: they hide or show |
| 53 | + content on initialization. Loading them later would flash content in the |
| 54 | + wrong state. |
| 55 | + - pat-bind: renders bound values on initialization. |
| 56 | + - pat-tabs, pat-auto-scale, pat-equaliser: they adjust the layout on |
| 57 | + initialization. |
| 58 | + - Transform-only patterns (pat-legend, pat-focus) and small patterns, for |
| 59 | + which the registration module would eat most of the gain. |
| 60 | + |
| 61 | +## Alternatives considered |
| 62 | + |
| 63 | +- **Grafting the implementation's prototype onto the registration pattern** |
| 64 | + (Mockup's current approach, e.g. `pat/modal/modal.js`): the registration |
| 65 | + module is a `Base` pattern whose `init()` imports the implementation and |
| 66 | + copies its prototype onto the running instance with `$.extend`. This |
| 67 | + relies on `Base` specifics and does not work for `BasePattern` classes: |
| 68 | + class methods are not enumerable and are not copied, class fields are |
| 69 | + created by the implementation's constructor which never runs, private |
| 70 | + fields throw when accessed on an object not constructed by their class, |
| 71 | + and `BasePattern` parses the options with the implementation's static |
| 72 | + parser before `init()` runs. |
| 73 | +- **Wrapping a separate implementation instance** (Mockup's pat-tinymce |
| 74 | + since 2022): the registration pattern creates an implementation object in |
| 75 | + its `init()` and keeps it as `this.instance`. With a `BasePattern` class |
| 76 | + as implementation, there are two pattern instances per element. The |
| 77 | + registration pattern has already stored itself as `el["pattern-NAME"]`, so |
| 78 | + the real pattern refuses to initialize as already initialized. Destroying, |
| 79 | + lifecycle tracking and init events would have to be forwarded between the |
| 80 | + two instances. |
| 81 | +- **A placeholder which constructs the real pattern** (chosen): the real |
| 82 | + class is instantiated with its own constructor, so fields, private fields, |
| 83 | + the static parser, the initialization order and the re-initialization |
| 84 | + guard work unchanged, and the implementation modules are ordinary |
| 85 | + `BasePattern` classes. The logic lives in one helper, so each registration |
| 86 | + module is a few lines of configuration. |
| 87 | + |
| 88 | +## Consequences |
| 89 | + |
| 90 | +- The eagerly loaded patterns chunk went from 206.9 KB to 129.1 KB minified |
| 91 | + (60.7 KB to 38.8 KB gzipped), measured with production builds before and |
| 92 | + after splitting. The split pattern implementations and their dynamic |
| 93 | + import lookup maps are no longer part of it. The entry points grow by about |
| 94 | + 0.5 KB for the additional chunk map entries. |
| 95 | +- Instantiating a registration module's class with `new` returns a Promise |
| 96 | + of the pattern instance, not the instance. Code which uses the result |
| 97 | + directly has to `await new Pattern(el)`. |
| 98 | + `events.await_pattern_init(new Pattern(el))` accepts the Promise. Code |
| 99 | + which only registers patterns or lets the registry instantiate them is not |
| 100 | + affected. |
| 101 | +- A split pattern initializes after its implementation chunk is loaded, |
| 102 | + i.e. one network round trip later on first use. Patterns which already |
| 103 | + load a library dynamically now load two chunks in sequence. |
| 104 | +- Breaking change for JavaScript users of split patterns: the default export |
| 105 | + of `NAME.js` is the placeholder class and `parser` is no longer exported |
| 106 | + there. Import `NAME--implementation.js` instead. |
| 107 | +- Bundles importing `NAME.js` for registration (e.g. Mockup's |
| 108 | + `patterns.js`) get the lazy loading without changes. |
0 commit comments