@streetui/renderer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +141 -0
- package/dist/index.cjs +972 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +373 -0
- package/dist/index.d.ts +373 -0
- package/dist/index.js +918 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 StreetUI contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# @streetui/renderer
|
|
2
|
+
|
|
3
|
+
StreetUI's own DOM renderer. It mounts a compiled semantic graph into real DOM,
|
|
4
|
+
patches individual nodes when signals change, reconciles keyed lists, renders
|
|
5
|
+
the *same* graph to an HTML string on the server, and hydrates server HTML into
|
|
6
|
+
a live app in the browser. There is no virtual DOM and no third-party SSR or
|
|
7
|
+
hydration framework — browser and server share one pipeline
|
|
8
|
+
(DSL → Compiler → Graph → Runtime → Renderer).
|
|
9
|
+
|
|
10
|
+
## Browser mount
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createRenderer } from '@streetui/renderer';
|
|
14
|
+
|
|
15
|
+
const renderer = createRenderer(); // BrowserDOMAdapter by default
|
|
16
|
+
const handle = renderer.mount(compile(app), document.getElementById('app')!);
|
|
17
|
+
// …later
|
|
18
|
+
handle.unmount(); // disposes subscriptions + listeners
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`mount` creates the full DOM tree, wires events, and opens signal subscriptions.
|
|
22
|
+
When a signal changes, its subscriber patches only the affected node
|
|
23
|
+
(`setTextContent` / `setAttribute` / `setProperty`) — no re-render, no diffing.
|
|
24
|
+
|
|
25
|
+
## Server rendering — `renderToString`
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { renderToString } from '@streetui/renderer';
|
|
29
|
+
|
|
30
|
+
const html = renderToString(compile(app)); // "<h1>…</h1><section>…"
|
|
31
|
+
// or with an explicit adapter:
|
|
32
|
+
renderToString(compile(app), { domAdapter: new ServerDOMAdapter() });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`renderToString` runs the exact same `mountGraph` pipeline used in the browser,
|
|
36
|
+
but against a `ServerDOMAdapter` — a lightweight in-memory node model that
|
|
37
|
+
serializes to HTML. It emits only the application's own elements (the synthetic
|
|
38
|
+
container is not serialized), so you embed the result wherever the client will
|
|
39
|
+
mount, e.g. `<div id="app">${html}</div>`.
|
|
40
|
+
|
|
41
|
+
Void elements are emitted HTML5-style — `<img src="…">` and `<input …>` with no
|
|
42
|
+
self-closing slash and no closing tag. Text and attribute values are escaped, so
|
|
43
|
+
markup is XSS-safe by construction.
|
|
44
|
+
|
|
45
|
+
**Lifecycle (no leaked subscriptions).** The synchronous mount opens signal
|
|
46
|
+
subscriptions; on the server those would live forever. `renderToString`
|
|
47
|
+
therefore disposes the root instance immediately after serializing — SSR has a
|
|
48
|
+
*render* lifecycle only. Mutating a signal after `renderToString` returns is a
|
|
49
|
+
safe no-op; the live *runtime* lifecycle is established later on the client by
|
|
50
|
+
`hydrate`.
|
|
51
|
+
|
|
52
|
+
## State transfer — `serializeState` / `readState`
|
|
53
|
+
|
|
54
|
+
Server-resolved state (e.g. a fetched list) crosses to the client as an inert
|
|
55
|
+
JSON island rather than being re-fetched:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { serializeState, readState } from '@streetui/renderer';
|
|
59
|
+
import { BrowserDOMAdapter } from '@streetui/dom';
|
|
60
|
+
|
|
61
|
+
// server:
|
|
62
|
+
const island = serializeState({ products: data }); // <script type="application/json" data-streetui-state>…</script>
|
|
63
|
+
// ship `html + island`
|
|
64
|
+
|
|
65
|
+
// client:
|
|
66
|
+
const state = readState(new BrowserDOMAdapter(), document);
|
|
67
|
+
const products = state['products'];
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`serializeState` returns `''` for an empty map, and escapes `<`, `>`, `&`,
|
|
71
|
+
`U+2028`, and `U+2029` as `\uXXXX` code points so the payload can never break out
|
|
72
|
+
of the `<script>` (a literal `</script>` in the data is neutralized).
|
|
73
|
+
`readState` is routed through a `DOMAdapter`, so it never assumes a global
|
|
74
|
+
`document` and returns `{}` when the island is absent or unparseable — hydration
|
|
75
|
+
then proceeds as a cold client render.
|
|
76
|
+
|
|
77
|
+
## Hydration — `hydrate`
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
const renderer = createRenderer();
|
|
81
|
+
const handle = renderer.hydrate(compile(app), document.getElementById('app')!);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`hydrate` walks the graph top-down against the server DOM, matching each graph
|
|
85
|
+
node positionally to one element by tag. Matching subtrees are **adopted in
|
|
86
|
+
place** — the same element objects are reused, never deleted and recreated. A
|
|
87
|
+
local mismatch (e.g. server emitted `<h1>` where the client graph expects `<p>`)
|
|
88
|
+
repairs only that subtree via a fresh mount at that position; the rest of the app
|
|
89
|
+
is untouched. Surplus server DOM beyond the graph is removed.
|
|
90
|
+
|
|
91
|
+
After hydration the app is fully live and behaves exactly like a browser mount:
|
|
92
|
+
|
|
93
|
+
- **Events** are wired once (single listener per node — no duplicates).
|
|
94
|
+
- **Reactive text/attributes** patch the *same* server node on signal change.
|
|
95
|
+
- **`when()`** toggles its branch in place, even when the branch was absent from
|
|
96
|
+
the server HTML.
|
|
97
|
+
- **Keyed lists** (`listOf`) reorder by reusing the existing server `<li>`
|
|
98
|
+
nodes rather than rebuilding them.
|
|
99
|
+
- **Controlled inputs** adopt their server value and are two-way bound —
|
|
100
|
+
typing updates the signal, and signal changes update the input.
|
|
101
|
+
|
|
102
|
+
`runtime.hydrate` uses `this._renderer.hydrate ?? this._renderer.mount`, so a
|
|
103
|
+
renderer without hydration support degrades gracefully to a cold mount.
|
|
104
|
+
|
|
105
|
+
## Router & resource hydration
|
|
106
|
+
|
|
107
|
+
`mountRouter(router, { hydrate: true, container, shell })` hydrates the
|
|
108
|
+
persistent shell and the initial route from the server HTML, resolving dynamic
|
|
109
|
+
params and query the same way on both sides, then takes over client-side
|
|
110
|
+
navigation (subsequent route changes mount fresh; the shell persists). See
|
|
111
|
+
`packages/router/README.md`.
|
|
112
|
+
|
|
113
|
+
`resource()` accepts `initialData` / `initialError` / `initialStatus` so a
|
|
114
|
+
server-resolved fetch seeds the client and is *not* repeated on hydration (pass
|
|
115
|
+
`immediate: true` only if you deliberately want a client refetch). See
|
|
116
|
+
`packages/state/README.md`.
|
|
117
|
+
|
|
118
|
+
## Server environment & error handling
|
|
119
|
+
|
|
120
|
+
`renderToString` runs under plain Node.js with no `window`/`document`/`location`
|
|
121
|
+
/`localStorage` assumptions (all DOM access goes through the adapter), so it is
|
|
122
|
+
framework-neutral and drops into any Node HTTP layer (including StreetJS). Errors
|
|
123
|
+
thrown while building the graph surface normally out of `renderToString`; the
|
|
124
|
+
renderer does not swallow them. In the app itself, failures stay observable and
|
|
125
|
+
contained via the `errorBoundary` DSL block (see the root README), which works
|
|
126
|
+
identically after hydration.
|
|
127
|
+
|
|
128
|
+
## Streaming (future)
|
|
129
|
+
|
|
130
|
+
Streaming SSR (flushing HTML in chunks / progressive hydration) is intentionally
|
|
131
|
+
**not** implemented in v0.4. The current model renders a complete document and
|
|
132
|
+
hydrates it in one pass. The `ServerDOMAdapter` + `mountGraph` split leaves room
|
|
133
|
+
to add a streaming serializer later without a second renderer; it is tracked as
|
|
134
|
+
future work.
|
|
135
|
+
|
|
136
|
+
## Public API
|
|
137
|
+
|
|
138
|
+
`renderToString`, `RenderToStringOptions`, `serializeState`, `readState`,
|
|
139
|
+
`STATE_MARKER_ATTR`, `createRenderer` (with `mount` / `hydrate`), and the
|
|
140
|
+
`RenderHandle` returned by both. All rendering paths consume a
|
|
141
|
+
`CompiledApplication` from `@streetui/compiler`.
|