@magnet-js/ssr 0.1.0 → 0.1.1
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/README.md +89 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @magnet/ssr
|
|
2
|
+
|
|
3
|
+
Lightweight server-side rendering support for Magnet components.
|
|
4
|
+
|
|
5
|
+
`@magnet/ssr` exports `ssrWindow`, a minimal implementation of `MagnetWindow`
|
|
6
|
+
that renders Magnet components to plain HTML strings on the server. It provides
|
|
7
|
+
lightweight server-side DOM implementations (`SSRComment`, `SSRText`,
|
|
8
|
+
`SSRElement`) with just enough surface area for the rest of Magnet to operate.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
### JSR
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
deno add jsr:@magnet/ssr
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### npm via JSR
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npx jsr add @magnet/ssr
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### npm
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @magnet-js/ssr
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { ssrWindow } from "@magnet/ssr";
|
|
34
|
+
import { magnet } from "@magnet/ui";
|
|
35
|
+
|
|
36
|
+
const ctx = magnet({ window: ssrWindow /* signal API */ });
|
|
37
|
+
const element = ctx.html.div({ class: "greeting" }, "Hello");
|
|
38
|
+
console.log(element.toString());
|
|
39
|
+
// <div class="greeting">Hello</div>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
If you need a full document, prepend `<!DOCTYPE html>` yourself:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const html = "<!DOCTYPE html>" + element.toString();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Namespaced attributes
|
|
49
|
+
|
|
50
|
+
Consumers specify namespaced attributes by wrapping the value with a namespace
|
|
51
|
+
URI key from `@magnet/ui`:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { nsAttr } from "@magnet/ui";
|
|
55
|
+
|
|
56
|
+
const xlink = nsAttr("http://www.w3.org/1999/xlink");
|
|
57
|
+
|
|
58
|
+
const element = ctx.html.svg({
|
|
59
|
+
"x:href": xlink("/icon.svg"),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The consumer is responsible for choosing a prefixed key such as `"x:href"` in
|
|
64
|
+
the attributes object. `nsAttr` attaches the namespace URI to the value. Magnet
|
|
65
|
+
passes both the prefixed key and the namespace URI to `setAttributeNS`, so
|
|
66
|
+
`ssr.ts` stores the attribute under the full key name.
|
|
67
|
+
|
|
68
|
+
## Design trade-offs
|
|
69
|
+
|
|
70
|
+
The implementation is intentionally minimal and fast:
|
|
71
|
+
|
|
72
|
+
- **No attribute value escaping.** Consumers are expected to provide well-formed
|
|
73
|
+
attribute values.
|
|
74
|
+
- **Empty string attributes render as boolean attributes.** For example,
|
|
75
|
+
`disabled=""` is emitted as `disabled`.
|
|
76
|
+
- **Namespaced attributes are stored by their full key.** The consumer chooses a
|
|
77
|
+
prefixed key such as `"x:href"`; `nsAttr` attaches the namespace URI to the
|
|
78
|
+
value, and Magnet passes both the key and the URI to `setAttributeNS`.
|
|
79
|
+
- **Shadow DOM is not supported.** `attachShadow` is stubbed out. Magnet checks
|
|
80
|
+
`browser()` before attaching shadow roots, so this path is not exercised
|
|
81
|
+
during SSR.
|
|
82
|
+
- **No attribute name validation.** Invalid or unsafe attribute names are
|
|
83
|
+
emitted as-is.
|
|
84
|
+
- **Text nodes escape `&`, `<`, and `>` only.** This is sufficient for plain
|
|
85
|
+
text content; consumers handling richer text should preprocess it.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT © 2026 Fernando G. Vilar.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magnet-js/ssr",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Server-side rendering support for Magnet components.",
|
|
5
5
|
"homepage": "https://gitlab.com/magnetjs/monorepo/-/tree/main/packages/ssr",
|
|
6
6
|
"repository": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@magnet-js/common": "0.1.
|
|
24
|
-
"@magnet-js/ui": "0.1.
|
|
23
|
+
"@magnet-js/common": "0.1.1",
|
|
24
|
+
"@magnet-js/ui": "0.1.1"
|
|
25
25
|
},
|
|
26
26
|
"_generatedBy": "dnt@dev"
|
|
27
27
|
}
|