|
| 1 | +# ADR-0029: Happy DOM for v0.18.3 DOM Simulation |
| 2 | + |
| 3 | +- Status: Proposed |
| 4 | +- Date: 2026-05-17 |
| 5 | +- Target: v0.18.3 |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +v0.18.3 adds an experimental DOM simulation path for client-only Web Components. |
| 10 | +The goal is to evaluate whether selected browser-dependent components can be |
| 11 | +server-rendered through an isolated environment without weakening the core |
| 12 | +admission model. |
| 13 | + |
| 14 | +The key challenge: Web Components often access browser-only DOM APIs during |
| 15 | +`connectedCallback()` — `childNodes`, `querySelector`, slot state, layout info. |
| 16 | +Without these APIs, components like Shoelace cannot render during SSR. |
| 17 | + |
| 18 | +Three implementation approaches were evaluated: |
| 19 | + |
| 20 | +1. **Self-implement** — write a minimal DOM shim covering only the APIs that |
| 21 | + LessJS components actually need (HTMLElement, connectedCallback, slot). |
| 22 | +2. **Happy DOM** — reuse an existing, maintained pure-JS DOM implementation |
| 23 | + with broad Web Component support. |
| 24 | +3. **JSDOM** — the oldest and most widely-known DOM-in-JS library. |
| 25 | + |
| 26 | +## Decision |
| 27 | + |
| 28 | +**Use Happy DOM as the underlying DOM environment for v0.18.3 DOM simulation.** |
| 29 | + |
| 30 | +Rationale: |
| 31 | + |
| 32 | +| Criterion | Self-implement | Happy DOM | JSDOM | |
| 33 | +| ---------------------- | ---------------------------- | -------------------------- | ------------------------------------- | |
| 34 | +| WC support maturity | Unknown (will discover bugs) | Good — Lit/Shoelace tested | Poor — customElements.define unstable | |
| 35 | +| Maintenance burden | High — we own every bug | Medium — active community | Low — but community nearly dead | |
| 36 | +| Bloat for our use case | Minimal (only needed APIs) | Moderate — treeshakable | High — full window object | |
| 37 | +| Startup cost | Lowest | Medium | Highest | |
| 38 | +| Long-term viability | N/A | Active (2024-2026 commits) | Stagnant (last release 2023) | |
| 39 | + |
| 40 | +**JSDOM** was ruled out because its `customElements.define` support requires |
| 41 | +explicit flags and remains unreliable — a showstopper for Web Component SSR. |
| 42 | + |
| 43 | +**Self-implement** is attractive for its minimal footprint but would require |
| 44 | +us to re-discover and fix the same edge cases that Happy DOM has already |
| 45 | +solved (e.g., slot assignment, composed paths, attribute reflection). The |
| 46 | +cost of reinventing a well-known wheel outweighs the bundle savings for an |
| 47 | +experimental feature. |
| 48 | + |
| 49 | +**Happy DOM** provides the best balance: |
| 50 | + |
| 51 | +- It supports `customElements.define`, `connectedCallback`, |
| 52 | + `attributeChangedCallback`, and Lit's rendering lifecycle out of the box. |
| 53 | +- It is treeshakable — we can import only `HTMLElement`, `customElements`, |
| 54 | + `Document`, and `window` without pulling in unused APIs like `fetch` or |
| 55 | + `Canvas`. |
| 56 | +- The community is active and responsive to Web Component issues. |
| 57 | +- The performance profile (5-20ms per render) is acceptable for an |
| 58 | + opt-in experimental feature with timeout guards. |
| 59 | + |
| 60 | +### Integration Architecture |
| 61 | + |
| 62 | +``` |
| 63 | +lessjs({ ssr: { domSimulation: 'explicit' } }) |
| 64 | + │ |
| 65 | + ▼ |
| 66 | + v0.18.3 creates isolated DOM environment |
| 67 | + │ |
| 68 | + ▼ |
| 69 | + Happy DOM provides: window, document, HTMLElement, |
| 70 | + customElements.define, connectedCallback, slots |
| 71 | + │ |
| 72 | + ▼ |
| 73 | + Admitted tags registered and rendered |
| 74 | + │ |
| 75 | + ▼ |
| 76 | + Timeout guard (500ms default) → success/failure |
| 77 | + │ |
| 78 | + ▼ |
| 79 | + Results written to dsd-report.json |
| 80 | + │ |
| 81 | + ▼ |
| 82 | + Failure → degrade to client-only, continue build |
| 83 | +``` |
| 84 | + |
| 85 | +## Consequences |
| 86 | + |
| 87 | +### Positive |
| 88 | + |
| 89 | +- Faster path to a working DOM simulation than self-implementing. |
| 90 | +- Happy DOM's existing WC support reduces the risk of missing edge cases. |
| 91 | +- We stay focused on the integration layer (config, isolation, timeout, |
| 92 | + reporting, fallback) rather than debugging DOM API quirks. |
| 93 | +- The decision is reversible — if Happy DOM proves unsuitable, we can |
| 94 | + swap it out without changing the public API. |
| 95 | + |
| 96 | +### Negative |
| 97 | + |
| 98 | +- Happy DOM adds a third-party dependency with its own release cadence and |
| 99 | + potential breaking changes. |
| 100 | +- Bundle size increases (~50KB gzipped) for the simulation path, though |
| 101 | + it is only loaded when `domSimulation !== 'off'`. |
| 102 | +- Happy DOM's behavior may diverge from real browsers in subtle ways |
| 103 | + (e.g., event dispatch timing, slot reassignment order). |
| 104 | + |
| 105 | +### Neutral |
| 106 | + |
| 107 | +- The v0.18.3 integration is designed to abstract the DOM environment |
| 108 | + behind an interface, so switching to a different implementation later |
| 109 | + is possible. |
| 110 | +- Happy DOM's treeshakable exports mean we can start with a minimal |
| 111 | + import set and expand only as needed. |
| 112 | + |
| 113 | +## References |
| 114 | + |
| 115 | +- [SOP v0.18.3](../sop/v0.18.3-dom-simulation-experiment.md) |
| 116 | +- [ADR-0028](./0028-universal-ssr-via-dom-simulation.md) — Conservative |
| 117 | + Third-Party WC SSR Admission (proposes the overall DOM simulation |
| 118 | + strategy) |
| 119 | +- [Happy DOM](https://github.com/capricorn86/happy-dom) — GitHub |
| 120 | + repository |
0 commit comments