@lab206/ssr 0.1.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 +114 -0
- package/README.md +6 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -0
- package/dist/islands.d.ts +61 -0
- package/dist/islands.d.ts.map +1 -0
- package/dist/islands.js +112 -0
- package/dist/islands.js.map +1 -0
- package/dist/registry.d.ts +39 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +72 -0
- package/dist/registry.js.map +1 -0
- package/package.json +58 -0
- package/src/index.ts +141 -0
- package/src/islands.ts +146 -0
- package/src/registry.ts +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
Parameters
|
|
4
|
+
|
|
5
|
+
Licensor: Scott Powers
|
|
6
|
+
|
|
7
|
+
Licensed Work: Powers
|
|
8
|
+
The Licensed Work is (c) 2026 Scott Powers
|
|
9
|
+
|
|
10
|
+
Additional Use Grant: You may make production use of the Licensed Work,
|
|
11
|
+
provided You do not use the Licensed Work for a
|
|
12
|
+
Competing Offering.
|
|
13
|
+
|
|
14
|
+
A “Competing Offering” is a product, hosted service,
|
|
15
|
+
library, framework, design system, or documentation
|
|
16
|
+
site whose primary purpose is to provide a
|
|
17
|
+
general-purpose UI runtime, component library, or
|
|
18
|
+
design-system kit that substitutes for the Licensed
|
|
19
|
+
Work (including rebranded or forked distributions
|
|
20
|
+
marketed as a competing UI kit or “Powers
|
|
21
|
+
alternative”).
|
|
22
|
+
|
|
23
|
+
Building end-user applications, internal tools, or
|
|
24
|
+
client project work *with* the Licensed Work is
|
|
25
|
+
allowed and is not a Competing Offering.
|
|
26
|
+
|
|
27
|
+
Change Date: Four years from the first public release of each
|
|
28
|
+
version of the Licensed Work, or 2030-08-15,
|
|
29
|
+
whichever is earlier for that version
|
|
30
|
+
|
|
31
|
+
Change License: Apache License, Version 2.0
|
|
32
|
+
|
|
33
|
+
For information about alternative licensing arrangements for the Software,
|
|
34
|
+
see LICENSE-COMMERCIAL.md or contact the Licensor.
|
|
35
|
+
|
|
36
|
+
Notice
|
|
37
|
+
|
|
38
|
+
The Business Source License (this document, or the “License”) is not an Open
|
|
39
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
40
|
+
under an Open Source License, as stated in this License.
|
|
41
|
+
|
|
42
|
+
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
43
|
+
“Business Source License” is a trademark of MariaDB Corporation Ab.
|
|
44
|
+
|
|
45
|
+
-----------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
Business Source License 1.1
|
|
48
|
+
|
|
49
|
+
Terms
|
|
50
|
+
|
|
51
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
52
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
53
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
54
|
+
production use.
|
|
55
|
+
|
|
56
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
57
|
+
available distribution of a specific version of the Licensed Work under this
|
|
58
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
59
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
60
|
+
above terminate.
|
|
61
|
+
|
|
62
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
63
|
+
currently in effect as described in this License, you must purchase a
|
|
64
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
65
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
66
|
+
|
|
67
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
68
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
69
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
70
|
+
for each version of the Licensed Work released by Licensor.
|
|
71
|
+
|
|
72
|
+
You must conspicuously display this License on each original or modified copy
|
|
73
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
74
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
75
|
+
License apply to your use of that work.
|
|
76
|
+
|
|
77
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
78
|
+
terminate your rights under this License for the current and all other
|
|
79
|
+
versions of the Licensed Work.
|
|
80
|
+
|
|
81
|
+
This License does not grant you any right in any trademark or logo of
|
|
82
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
83
|
+
Licensor as expressly required by this License).
|
|
84
|
+
|
|
85
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
86
|
+
AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
87
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
88
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
89
|
+
TITLE.
|
|
90
|
+
|
|
91
|
+
MariaDB hereby grants you permission to use this License’s text to license
|
|
92
|
+
your works, and to refer to it using the trademark “Business Source License”,
|
|
93
|
+
as long as you comply with the Covenants of Licensor below.
|
|
94
|
+
|
|
95
|
+
Covenants of Licensor
|
|
96
|
+
|
|
97
|
+
In consideration of the right to use this License’s text and the “Business
|
|
98
|
+
Source License” name and trademark, Licensor covenants to MariaDB, and to all
|
|
99
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
100
|
+
|
|
101
|
+
1. To specify as the Change License the GPL Version 2.0 or any later version,
|
|
102
|
+
or a license that is compatible with GPL Version 2.0 or a later version,
|
|
103
|
+
where “compatible” means that software provided under the Change License can
|
|
104
|
+
be included in a program with software provided under GPL Version 2.0 or a
|
|
105
|
+
later version. Licensor may specify additional Change Licenses without
|
|
106
|
+
limitation.
|
|
107
|
+
|
|
108
|
+
2. To either: (a) specify an additional grant of rights to use that does not
|
|
109
|
+
impose any additional restriction on the right granted in this License, as
|
|
110
|
+
the Additional Use Grant; or (b) insert the text “None”.
|
|
111
|
+
|
|
112
|
+
3. To specify a Change Date.
|
|
113
|
+
|
|
114
|
+
4. Not to modify this License in any other way.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type MountResult } from "@lab206/dom";
|
|
2
|
+
export interface RenderToStringOptions {
|
|
3
|
+
/** Wait for microtasks / flush reactive updates before serializing. Default true. */
|
|
4
|
+
flush?: boolean;
|
|
5
|
+
/** Optional URL for the virtual window (routing, absolute links). */
|
|
6
|
+
url?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Mount `app` in a virtual document and return `innerHTML` of the root.
|
|
10
|
+
* Disposes the app after serialization.
|
|
11
|
+
*/
|
|
12
|
+
export declare function renderToString(app: () => MountResult, options?: RenderToStringOptions): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* Wrap app HTML in a minimal document shell for full-page SSR responses.
|
|
15
|
+
*/
|
|
16
|
+
export declare function htmlDocument(body: string, options?: {
|
|
17
|
+
title?: string;
|
|
18
|
+
head?: string;
|
|
19
|
+
/** Extra attributes on <html>, e.g. data-pu-theme */
|
|
20
|
+
htmlAttrs?: string;
|
|
21
|
+
}): string;
|
|
22
|
+
export { island, markIsland, hydrateIsland, hydrateIslands, islandPlaceholder, ISLAND_ATTR, } from "./islands.js";
|
|
23
|
+
export type { IslandFactory, IslandRegistry, HydrateIslandsOptions, } from "./islands.js";
|
|
24
|
+
export { defineIslands, listIslandsInHtml } from "./registry.js";
|
|
25
|
+
export type { IslandRegistryApi } from "./registry.js";
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAS,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAGtD,MAAM,WAAW,qBAAqB;IACpC,qFAAqF;IACrF,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qEAAqE;IACrE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,MAAM,WAAW,EACtB,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,MAAM,CAAC,CA+CjB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;IACR,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,GACA,MAAM,CAgBR;AAUD,OAAO,EACL,MAAM,EACN,UAAU,EACV,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,WAAW,GACZ,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,aAAa,EACb,cAAc,EACd,qBAAqB,GACtB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACjE,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lab206/ssr
|
|
3
|
+
*
|
|
4
|
+
* - `renderToString` — full tree → HTML (happy-dom)
|
|
5
|
+
* - `htmlDocument` — wrap body in a document shell
|
|
6
|
+
* - Islands — `island` / `markIsland` / `hydrateIslands` for selective client interactivity
|
|
7
|
+
*
|
|
8
|
+
* @example SSR + island
|
|
9
|
+
* ```ts
|
|
10
|
+
* // server
|
|
11
|
+
* const body = await renderToString(() =>
|
|
12
|
+
* h("main", null,
|
|
13
|
+
* h("h1", { text: "Static" }),
|
|
14
|
+
* island("counter", () => Counter()),
|
|
15
|
+
* )
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* // client
|
|
19
|
+
* hydrateIslands({ counter: () => Counter() });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { Window } from "happy-dom";
|
|
23
|
+
import { mount } from "@lab206/dom";
|
|
24
|
+
import { flush } from "@lab206/core";
|
|
25
|
+
/**
|
|
26
|
+
* Mount `app` in a virtual document and return `innerHTML` of the root.
|
|
27
|
+
* Disposes the app after serialization.
|
|
28
|
+
*/
|
|
29
|
+
export async function renderToString(app, options = {}) {
|
|
30
|
+
const window = new Window({
|
|
31
|
+
url: options.url ?? "https://localhost/",
|
|
32
|
+
});
|
|
33
|
+
const document = window.document;
|
|
34
|
+
const g = globalThis;
|
|
35
|
+
const prev = {
|
|
36
|
+
window: g.window,
|
|
37
|
+
document: g.document,
|
|
38
|
+
Node: g.Node,
|
|
39
|
+
HTMLElement: g.HTMLElement,
|
|
40
|
+
Text: g.Text,
|
|
41
|
+
Element: g.Element,
|
|
42
|
+
};
|
|
43
|
+
g.window = window;
|
|
44
|
+
g.document = document;
|
|
45
|
+
g.Node = window.Node;
|
|
46
|
+
g.HTMLElement = window.HTMLElement;
|
|
47
|
+
g.Text = window.Text;
|
|
48
|
+
g.Element = window.Element;
|
|
49
|
+
const root = document.createElement("div");
|
|
50
|
+
document.body.appendChild(root);
|
|
51
|
+
let html = "";
|
|
52
|
+
try {
|
|
53
|
+
const dispose = mount(root, app);
|
|
54
|
+
if (options.flush !== false) {
|
|
55
|
+
await Promise.resolve();
|
|
56
|
+
await Promise.resolve();
|
|
57
|
+
flush();
|
|
58
|
+
}
|
|
59
|
+
html = root.innerHTML;
|
|
60
|
+
dispose();
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
g.window = prev.window;
|
|
64
|
+
g.document = prev.document;
|
|
65
|
+
g.Node = prev.Node;
|
|
66
|
+
g.HTMLElement = prev.HTMLElement;
|
|
67
|
+
g.Text = prev.Text;
|
|
68
|
+
g.Element = prev.Element;
|
|
69
|
+
window.close();
|
|
70
|
+
}
|
|
71
|
+
return html;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Wrap app HTML in a minimal document shell for full-page SSR responses.
|
|
75
|
+
*/
|
|
76
|
+
export function htmlDocument(body, options) {
|
|
77
|
+
const title = options?.title ?? "Powers";
|
|
78
|
+
const head = options?.head ?? "";
|
|
79
|
+
const htmlAttrs = options?.htmlAttrs ? ` ${options.htmlAttrs}` : "";
|
|
80
|
+
return `<!DOCTYPE html>
|
|
81
|
+
<html lang="en"${htmlAttrs}>
|
|
82
|
+
<head>
|
|
83
|
+
<meta charset="UTF-8" />
|
|
84
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
85
|
+
<title>${escapeHtml(title)}</title>
|
|
86
|
+
${head}
|
|
87
|
+
</head>
|
|
88
|
+
<body>
|
|
89
|
+
<div id="app">${body}</div>
|
|
90
|
+
</body>
|
|
91
|
+
</html>`;
|
|
92
|
+
}
|
|
93
|
+
function escapeHtml(s) {
|
|
94
|
+
return s
|
|
95
|
+
.replace(/&/g, "&")
|
|
96
|
+
.replace(/</g, "<")
|
|
97
|
+
.replace(/>/g, ">")
|
|
98
|
+
.replace(/"/g, """);
|
|
99
|
+
}
|
|
100
|
+
export { island, markIsland, hydrateIsland, hydrateIslands, islandPlaceholder, ISLAND_ATTR, } from "./islands.js";
|
|
101
|
+
export { defineIslands, listIslandsInHtml } from "./registry.js";
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,KAAK,EAAoB,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AASrC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,GAAsB,EACtB,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EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Dispose } from "@lab206/core";
|
|
2
|
+
import { type MountResult } from "@lab206/dom";
|
|
3
|
+
export type IslandFactory = () => MountResult;
|
|
4
|
+
export type IslandRegistry = Record<string, IslandFactory>;
|
|
5
|
+
export declare const ISLAND_ATTR = "data-pu-island";
|
|
6
|
+
/**
|
|
7
|
+
* Mark a host element as a client island (sets `data-pu-island`).
|
|
8
|
+
* During SSR the inner HTML is serialized; on the client call `hydrateIslands`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function markIsland(name: string, host?: HTMLElement): HTMLElement;
|
|
11
|
+
/**
|
|
12
|
+
* Create an island host and mount `app` into it (SSR + client create path).
|
|
13
|
+
* Prefer this when building trees that will be stringified, then hydrated.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // server
|
|
18
|
+
* const html = await renderToString(() =>
|
|
19
|
+
* island("counter", () => <Counter />)
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* // client
|
|
23
|
+
* hydrateIslands({ counter: () => <Counter /> });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function island(name: string, app: IslandFactory): HTMLElement;
|
|
27
|
+
export interface HydrateIslandsOptions {
|
|
28
|
+
/** Document or subtree to search (default: document) */
|
|
29
|
+
root?: ParentNode;
|
|
30
|
+
/**
|
|
31
|
+
* Called when a `data-pu-island` has no factory.
|
|
32
|
+
* Default: console.warn
|
|
33
|
+
*/
|
|
34
|
+
onMissing?: (name: string, el: HTMLElement) => void;
|
|
35
|
+
/**
|
|
36
|
+
* If true, leave server HTML when no factory is registered.
|
|
37
|
+
* Default: true
|
|
38
|
+
*/
|
|
39
|
+
preserveOnMissing?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Hydrate all `[data-pu-island]` nodes under `root` using a name → factory map.
|
|
43
|
+
* Clears server HTML inside each island and mounts the client app.
|
|
44
|
+
* Returns a dispose that tears down every island.
|
|
45
|
+
*/
|
|
46
|
+
export declare function hydrateIslands(registry: IslandRegistry, rootOrOptions?: ParentNode | HydrateIslandsOptions): Dispose;
|
|
47
|
+
/**
|
|
48
|
+
* Hydrate a single island by name (first match).
|
|
49
|
+
*/
|
|
50
|
+
export declare function hydrateIsland(name: string, factory: IslandFactory, root?: ParentNode): Dispose;
|
|
51
|
+
/**
|
|
52
|
+
* Static HTML placeholder for an island (no happy-dom needed).
|
|
53
|
+
* Useful when the shell is a template string.
|
|
54
|
+
*/
|
|
55
|
+
export declare function islandPlaceholder(name: string, fallbackHtml?: string): string;
|
|
56
|
+
/** @internal */
|
|
57
|
+
export declare function createIslandRoot(app: IslandFactory): {
|
|
58
|
+
host: HTMLElement;
|
|
59
|
+
dispose: Dispose;
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=islands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"islands.d.ts","sourceRoot":"","sources":["../src/islands.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAS,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,MAAM,aAAa,GAAG,MAAM,WAAW,CAAC;AAC9C,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAE3D,eAAO,MAAM,WAAW,mBAAmB,CAAC;AAE5C;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,WAAW,CAIxE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,aAAa,GAAG,WAAW,CAUpE;AAED,MAAM,WAAW,qBAAqB;IACpC,wDAAwD;IACxD,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB;;;OAGG;IACH,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,KAAK,IAAI,CAAC;IACpD;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,cAAc,EACxB,aAAa,GAAE,UAAU,GAAG,qBAAgC,GAC3D,OAAO,CAmCT;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,EACtB,IAAI,GAAE,UAAqB,GAC1B,OAAO,CAET;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,SAAK,GAAG,MAAM,CAEzE;AAMD,gBAAgB;AAChB,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,aAAa,GAAG;IACpD,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;CAClB,CAaA"}
|
package/dist/islands.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { createRoot } from "@lab206/core";
|
|
2
|
+
import { mount } from "@lab206/dom";
|
|
3
|
+
export const ISLAND_ATTR = "data-pu-island";
|
|
4
|
+
/**
|
|
5
|
+
* Mark a host element as a client island (sets `data-pu-island`).
|
|
6
|
+
* During SSR the inner HTML is serialized; on the client call `hydrateIslands`.
|
|
7
|
+
*/
|
|
8
|
+
export function markIsland(name, host) {
|
|
9
|
+
const el = host ?? document.createElement("div");
|
|
10
|
+
el.setAttribute(ISLAND_ATTR, name);
|
|
11
|
+
return el;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create an island host and mount `app` into it (SSR + client create path).
|
|
15
|
+
* Prefer this when building trees that will be stringified, then hydrated.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // server
|
|
20
|
+
* const html = await renderToString(() =>
|
|
21
|
+
* island("counter", () => <Counter />)
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* // client
|
|
25
|
+
* hydrateIslands({ counter: () => <Counter /> });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function island(name, app) {
|
|
29
|
+
const host = markIsland(name);
|
|
30
|
+
// Mount interactive tree into the island host
|
|
31
|
+
const result = app();
|
|
32
|
+
if (result == null)
|
|
33
|
+
return host;
|
|
34
|
+
const nodes = Array.isArray(result) ? result : [result];
|
|
35
|
+
for (const n of nodes) {
|
|
36
|
+
if (n)
|
|
37
|
+
host.appendChild(n);
|
|
38
|
+
}
|
|
39
|
+
return host;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Hydrate all `[data-pu-island]` nodes under `root` using a name → factory map.
|
|
43
|
+
* Clears server HTML inside each island and mounts the client app.
|
|
44
|
+
* Returns a dispose that tears down every island.
|
|
45
|
+
*/
|
|
46
|
+
export function hydrateIslands(registry, rootOrOptions = document) {
|
|
47
|
+
const options = rootOrOptions && typeof rootOrOptions === "object" && "nodeType" in rootOrOptions
|
|
48
|
+
? { root: rootOrOptions }
|
|
49
|
+
: rootOrOptions;
|
|
50
|
+
const root = options.root ?? document;
|
|
51
|
+
const preserveOnMissing = options.preserveOnMissing !== false;
|
|
52
|
+
const onMissing = options.onMissing ??
|
|
53
|
+
((name) => {
|
|
54
|
+
console.warn(`[powers/ssr] No island registered for "${name}"`);
|
|
55
|
+
});
|
|
56
|
+
const disposers = [];
|
|
57
|
+
const nodes = root.querySelectorAll?.(`[${ISLAND_ATTR}]`) ?? [];
|
|
58
|
+
nodes.forEach((node) => {
|
|
59
|
+
const el = node;
|
|
60
|
+
const name = el.getAttribute(ISLAND_ATTR);
|
|
61
|
+
if (!name)
|
|
62
|
+
return;
|
|
63
|
+
const factory = registry[name];
|
|
64
|
+
if (!factory) {
|
|
65
|
+
onMissing(name, el);
|
|
66
|
+
if (!preserveOnMissing)
|
|
67
|
+
el.textContent = "";
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// Replace server markup with live app
|
|
71
|
+
el.textContent = "";
|
|
72
|
+
disposers.push(mount(el, factory));
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
for (const d of disposers)
|
|
76
|
+
d();
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Hydrate a single island by name (first match).
|
|
81
|
+
*/
|
|
82
|
+
export function hydrateIsland(name, factory, root = document) {
|
|
83
|
+
return hydrateIslands({ [name]: factory }, { root });
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Static HTML placeholder for an island (no happy-dom needed).
|
|
87
|
+
* Useful when the shell is a template string.
|
|
88
|
+
*/
|
|
89
|
+
export function islandPlaceholder(name, fallbackHtml = "") {
|
|
90
|
+
return `<div ${ISLAND_ATTR}="${escapeAttr(name)}">${fallbackHtml}</div>`;
|
|
91
|
+
}
|
|
92
|
+
function escapeAttr(s) {
|
|
93
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
94
|
+
}
|
|
95
|
+
/** @internal */
|
|
96
|
+
export function createIslandRoot(app) {
|
|
97
|
+
const host = document.createElement("div");
|
|
98
|
+
let dispose;
|
|
99
|
+
createRoot((d) => {
|
|
100
|
+
dispose = d;
|
|
101
|
+
const result = app();
|
|
102
|
+
if (result == null)
|
|
103
|
+
return;
|
|
104
|
+
const nodes = Array.isArray(result) ? result : [result];
|
|
105
|
+
for (const n of nodes) {
|
|
106
|
+
if (n)
|
|
107
|
+
host.appendChild(n);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return { host, dispose };
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=islands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"islands.js","sourceRoot":"","sources":["../src/islands.ts"],"names":[],"mappings":"AAAA,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AAC;AACvD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY,EAAE,YAAY,GAAG,EAAE;IAC/D,OAAO,QAAQ,WAAW,KAAK,UAAU,CAAC,IAAI,CAAC,KAAK,YAAY,QAAQ,CAAC;AAC3E,CAAC;AAED,SAAS,UAAU,CAAC,CAAS;IAC3B,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAChF,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,gBAAgB,CAAC,GAAkB;IAIjD,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC3C,IAAI,OAAiB,CAAC;IACtB,UAAU,CAAC,CAAC,CAAC,EAAE,EAAE;QACf,OAAO,GAAG,CAAC,CAAC;QACZ,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC;QACrB,IAAI,MAAM,IAAI,IAAI;YAAE,OAAO;QAC3B,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;QACxD,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;YACtB,IAAI,CAAC;gBAAE,IAAI,CAAC,WAAW,CAAC,CAAS,CAAC,CAAC;QACrC,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Dispose } from "@lab206/core";
|
|
2
|
+
import type { IslandFactory, IslandRegistry } from "./islands.js";
|
|
3
|
+
export interface IslandRegistryApi {
|
|
4
|
+
/** Registered factories */
|
|
5
|
+
readonly entries: IslandRegistry;
|
|
6
|
+
/** Register or replace an island factory */
|
|
7
|
+
register(name: string, factory: IslandFactory): void;
|
|
8
|
+
/** Register many islands at once */
|
|
9
|
+
registerAll(map: IslandRegistry): void;
|
|
10
|
+
/** Hydrate all matching islands in the document (or root) */
|
|
11
|
+
hydrate(root?: ParentNode): Dispose;
|
|
12
|
+
/** List island names present under root (DOM) */
|
|
13
|
+
discover(root?: ParentNode): string[];
|
|
14
|
+
/** Names registered but not found in the document */
|
|
15
|
+
missingInDom(root?: ParentNode): string[];
|
|
16
|
+
/** Island names in the DOM that have no factory */
|
|
17
|
+
missingInRegistry(root?: ParentNode): string[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Typed-ish island registry for client entrypoints.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const islands = defineIslands({
|
|
25
|
+
* counter: () => Counter(),
|
|
26
|
+
* cart: () => CartWidget(),
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // after HTML is in the document:
|
|
30
|
+
* islands.hydrate();
|
|
31
|
+
* console.log(islands.missingInRegistry()); // unexpected SSR islands
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function defineIslands(initial?: IslandRegistry): IslandRegistryApi;
|
|
35
|
+
/**
|
|
36
|
+
* Parse island names from an HTML string (no DOM required).
|
|
37
|
+
*/
|
|
38
|
+
export declare function listIslandsInHtml(html: string): string[];
|
|
39
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGlE,MAAM,WAAW,iBAAiB;IAChC,2BAA2B;IAC3B,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,4CAA4C;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAAC;IACrD,oCAAoC;IACpC,WAAW,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAAC;IACvC,6DAA6D;IAC7D,OAAO,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC;IACpC,iDAAiD;IACjD,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,EAAE,CAAC;IACtC,qDAAqD;IACrD,YAAY,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,EAAE,CAAC;IAC1C,mDAAmD;IACnD,iBAAiB,CAAC,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,EAAE,CAAC;CAChD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAC3B,OAAO,GAAE,cAAmB,GAC3B,iBAAiB,CAiDnB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAQxD"}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { hydrateIslands, ISLAND_ATTR } from "./islands.js";
|
|
2
|
+
/**
|
|
3
|
+
* Typed-ish island registry for client entrypoints.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const islands = defineIslands({
|
|
8
|
+
* counter: () => Counter(),
|
|
9
|
+
* cart: () => CartWidget(),
|
|
10
|
+
* });
|
|
11
|
+
*
|
|
12
|
+
* // after HTML is in the document:
|
|
13
|
+
* islands.hydrate();
|
|
14
|
+
* console.log(islands.missingInRegistry()); // unexpected SSR islands
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export function defineIslands(initial = {}) {
|
|
18
|
+
const entries = { ...initial };
|
|
19
|
+
function register(name, factory) {
|
|
20
|
+
entries[name] = factory;
|
|
21
|
+
}
|
|
22
|
+
function registerAll(map) {
|
|
23
|
+
Object.assign(entries, map);
|
|
24
|
+
}
|
|
25
|
+
function discover(root = document) {
|
|
26
|
+
const nodes = root.querySelectorAll?.(`[${ISLAND_ATTR}]`) ?? [];
|
|
27
|
+
const names = [];
|
|
28
|
+
nodes.forEach((n) => {
|
|
29
|
+
const name = n.getAttribute(ISLAND_ATTR);
|
|
30
|
+
if (name)
|
|
31
|
+
names.push(name);
|
|
32
|
+
});
|
|
33
|
+
return names;
|
|
34
|
+
}
|
|
35
|
+
function missingInDom(root = document) {
|
|
36
|
+
const present = new Set(discover(root));
|
|
37
|
+
return Object.keys(entries).filter((k) => !present.has(k));
|
|
38
|
+
}
|
|
39
|
+
function missingInRegistry(root = document) {
|
|
40
|
+
return discover(root).filter((name) => !entries[name]);
|
|
41
|
+
}
|
|
42
|
+
function hydrate(root = document) {
|
|
43
|
+
const unknown = missingInRegistry(root);
|
|
44
|
+
if (unknown.length) {
|
|
45
|
+
console.warn(`[powers/ssr] DOM islands with no registry entry: ${unknown.join(", ")}`);
|
|
46
|
+
}
|
|
47
|
+
return hydrateIslands(entries, root);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
entries,
|
|
51
|
+
register,
|
|
52
|
+
registerAll,
|
|
53
|
+
hydrate,
|
|
54
|
+
discover,
|
|
55
|
+
missingInDom,
|
|
56
|
+
missingInRegistry,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Parse island names from an HTML string (no DOM required).
|
|
61
|
+
*/
|
|
62
|
+
export function listIslandsInHtml(html) {
|
|
63
|
+
const re = /data-pu-island=["']([^"']+)["']/g;
|
|
64
|
+
const names = [];
|
|
65
|
+
let m;
|
|
66
|
+
while ((m = re.exec(html))) {
|
|
67
|
+
if (m[1])
|
|
68
|
+
names.push(m[1]);
|
|
69
|
+
}
|
|
70
|
+
return names;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|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Q,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,CAAC;IAED,SAAS,OAAO,CAAC,OAAmB,QAAQ;QAC1C,MAAM,OAAO,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;QACxC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,OAAO,CAAC,IAAI,CACV,oDAAoD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACzE,CAAC;QACJ,CAAC;QACD,OAAO,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC;IAED,OAAO;QACL,OAAO;QACP,QAAQ;QACR,WAAW;QACX,OAAO;QACP,QAAQ;QACR,YAAY;QACZ,iBAAiB;KAClB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,MAAM,EAAE,GAAG,kCAAkC,CAAC;IAC9C,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,CAAyB,CAAC;IAC9B,OAAO,CAAC,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC,CAAC,CAAC,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lab206/ssr",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SSR foundation for Powers — render apps to HTML strings (happy-dom).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "BUSL-1.1",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"development": "./src/index.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"src/**/*.ts",
|
|
22
|
+
"src/**/*.tsx",
|
|
23
|
+
"src/**/*.css",
|
|
24
|
+
"!src/**/*.test.ts",
|
|
25
|
+
"!src/**/*.test.tsx"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"happy-dom": "^17.4.4",
|
|
29
|
+
"@lab206/core": "0.1.0",
|
|
30
|
+
"@lab206/dom": "0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsx": "^4.19.3",
|
|
34
|
+
"typescript": "^5.8.2"
|
|
35
|
+
},
|
|
36
|
+
"author": "Scott Powers",
|
|
37
|
+
"private": false,
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://lab206.com",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/spowers2/powers/issues"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/spowers2/powers.git",
|
|
51
|
+
"directory": "packages/ssr"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc -p tsconfig.json",
|
|
55
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
56
|
+
"test": "node --import tsx --test src/*.test.ts"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lab206/ssr
|
|
3
|
+
*
|
|
4
|
+
* - `renderToString` — full tree → HTML (happy-dom)
|
|
5
|
+
* - `htmlDocument` — wrap body in a document shell
|
|
6
|
+
* - Islands — `island` / `markIsland` / `hydrateIslands` for selective client interactivity
|
|
7
|
+
*
|
|
8
|
+
* @example SSR + island
|
|
9
|
+
* ```ts
|
|
10
|
+
* // server
|
|
11
|
+
* const body = await renderToString(() =>
|
|
12
|
+
* h("main", null,
|
|
13
|
+
* h("h1", { text: "Static" }),
|
|
14
|
+
* island("counter", () => Counter()),
|
|
15
|
+
* )
|
|
16
|
+
* );
|
|
17
|
+
*
|
|
18
|
+
* // client
|
|
19
|
+
* hydrateIslands({ counter: () => Counter() });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { Window } from "happy-dom";
|
|
23
|
+
import { mount, type MountResult } from "@lab206/dom";
|
|
24
|
+
import { flush } from "@lab206/core";
|
|
25
|
+
|
|
26
|
+
export interface RenderToStringOptions {
|
|
27
|
+
/** Wait for microtasks / flush reactive updates before serializing. Default true. */
|
|
28
|
+
flush?: boolean;
|
|
29
|
+
/** Optional URL for the virtual window (routing, absolute links). */
|
|
30
|
+
url?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Mount `app` in a virtual document and return `innerHTML` of the root.
|
|
35
|
+
* Disposes the app after serialization.
|
|
36
|
+
*/
|
|
37
|
+
export async function renderToString(
|
|
38
|
+
app: () => MountResult,
|
|
39
|
+
options: RenderToStringOptions = {},
|
|
40
|
+
): Promise<string> {
|
|
41
|
+
const window = new Window({
|
|
42
|
+
url: options.url ?? "https://localhost/",
|
|
43
|
+
});
|
|
44
|
+
const document = window.document;
|
|
45
|
+
|
|
46
|
+
const g = globalThis as unknown as Record<string, unknown>;
|
|
47
|
+
const prev = {
|
|
48
|
+
window: g.window,
|
|
49
|
+
document: g.document,
|
|
50
|
+
Node: g.Node,
|
|
51
|
+
HTMLElement: g.HTMLElement,
|
|
52
|
+
Text: g.Text,
|
|
53
|
+
Element: g.Element,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
g.window = window;
|
|
57
|
+
g.document = document;
|
|
58
|
+
g.Node = window.Node;
|
|
59
|
+
g.HTMLElement = window.HTMLElement;
|
|
60
|
+
g.Text = window.Text;
|
|
61
|
+
g.Element = window.Element;
|
|
62
|
+
|
|
63
|
+
const root = document.createElement("div");
|
|
64
|
+
document.body.appendChild(root);
|
|
65
|
+
|
|
66
|
+
let html = "";
|
|
67
|
+
try {
|
|
68
|
+
const dispose = mount(root as unknown as ParentNode, app);
|
|
69
|
+
if (options.flush !== false) {
|
|
70
|
+
await Promise.resolve();
|
|
71
|
+
await Promise.resolve();
|
|
72
|
+
flush();
|
|
73
|
+
}
|
|
74
|
+
html = root.innerHTML;
|
|
75
|
+
dispose();
|
|
76
|
+
} finally {
|
|
77
|
+
g.window = prev.window;
|
|
78
|
+
g.document = prev.document;
|
|
79
|
+
g.Node = prev.Node;
|
|
80
|
+
g.HTMLElement = prev.HTMLElement;
|
|
81
|
+
g.Text = prev.Text;
|
|
82
|
+
g.Element = prev.Element;
|
|
83
|
+
window.close();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return html;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Wrap app HTML in a minimal document shell for full-page SSR responses.
|
|
91
|
+
*/
|
|
92
|
+
export function htmlDocument(
|
|
93
|
+
body: string,
|
|
94
|
+
options?: {
|
|
95
|
+
title?: string;
|
|
96
|
+
head?: string;
|
|
97
|
+
/** Extra attributes on <html>, e.g. data-pu-theme */
|
|
98
|
+
htmlAttrs?: string;
|
|
99
|
+
},
|
|
100
|
+
): string {
|
|
101
|
+
const title = options?.title ?? "Powers";
|
|
102
|
+
const head = options?.head ?? "";
|
|
103
|
+
const htmlAttrs = options?.htmlAttrs ? ` ${options.htmlAttrs}` : "";
|
|
104
|
+
return `<!DOCTYPE html>
|
|
105
|
+
<html lang="en"${htmlAttrs}>
|
|
106
|
+
<head>
|
|
107
|
+
<meta charset="UTF-8" />
|
|
108
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
109
|
+
<title>${escapeHtml(title)}</title>
|
|
110
|
+
${head}
|
|
111
|
+
</head>
|
|
112
|
+
<body>
|
|
113
|
+
<div id="app">${body}</div>
|
|
114
|
+
</body>
|
|
115
|
+
</html>`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function escapeHtml(s: string): string {
|
|
119
|
+
return s
|
|
120
|
+
.replace(/&/g, "&")
|
|
121
|
+
.replace(/</g, "<")
|
|
122
|
+
.replace(/>/g, ">")
|
|
123
|
+
.replace(/"/g, """);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export {
|
|
127
|
+
island,
|
|
128
|
+
markIsland,
|
|
129
|
+
hydrateIsland,
|
|
130
|
+
hydrateIslands,
|
|
131
|
+
islandPlaceholder,
|
|
132
|
+
ISLAND_ATTR,
|
|
133
|
+
} from "./islands.js";
|
|
134
|
+
export type {
|
|
135
|
+
IslandFactory,
|
|
136
|
+
IslandRegistry,
|
|
137
|
+
HydrateIslandsOptions,
|
|
138
|
+
} from "./islands.js";
|
|
139
|
+
|
|
140
|
+
export { defineIslands, listIslandsInHtml } from "./registry.js";
|
|
141
|
+
export type { IslandRegistryApi } from "./registry.js";
|
package/src/islands.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { createRoot, type Dispose } from "@lab206/core";
|
|
2
|
+
import { mount, type MountResult } from "@lab206/dom";
|
|
3
|
+
|
|
4
|
+
export type IslandFactory = () => MountResult;
|
|
5
|
+
export type IslandRegistry = Record<string, IslandFactory>;
|
|
6
|
+
|
|
7
|
+
export const ISLAND_ATTR = "data-pu-island";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Mark a host element as a client island (sets `data-pu-island`).
|
|
11
|
+
* During SSR the inner HTML is serialized; on the client call `hydrateIslands`.
|
|
12
|
+
*/
|
|
13
|
+
export function markIsland(name: string, host?: HTMLElement): HTMLElement {
|
|
14
|
+
const el = host ?? document.createElement("div");
|
|
15
|
+
el.setAttribute(ISLAND_ATTR, name);
|
|
16
|
+
return el;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create an island host and mount `app` into it (SSR + client create path).
|
|
21
|
+
* Prefer this when building trees that will be stringified, then hydrated.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // server
|
|
26
|
+
* const html = await renderToString(() =>
|
|
27
|
+
* island("counter", () => <Counter />)
|
|
28
|
+
* );
|
|
29
|
+
*
|
|
30
|
+
* // client
|
|
31
|
+
* hydrateIslands({ counter: () => <Counter /> });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function island(name: string, app: IslandFactory): HTMLElement {
|
|
35
|
+
const host = markIsland(name);
|
|
36
|
+
// Mount interactive tree into the island host
|
|
37
|
+
const result = app();
|
|
38
|
+
if (result == null) return host;
|
|
39
|
+
const nodes = Array.isArray(result) ? result : [result];
|
|
40
|
+
for (const n of nodes) {
|
|
41
|
+
if (n) host.appendChild(n as Node);
|
|
42
|
+
}
|
|
43
|
+
return host;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface HydrateIslandsOptions {
|
|
47
|
+
/** Document or subtree to search (default: document) */
|
|
48
|
+
root?: ParentNode;
|
|
49
|
+
/**
|
|
50
|
+
* Called when a `data-pu-island` has no factory.
|
|
51
|
+
* Default: console.warn
|
|
52
|
+
*/
|
|
53
|
+
onMissing?: (name: string, el: HTMLElement) => void;
|
|
54
|
+
/**
|
|
55
|
+
* If true, leave server HTML when no factory is registered.
|
|
56
|
+
* Default: true
|
|
57
|
+
*/
|
|
58
|
+
preserveOnMissing?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Hydrate all `[data-pu-island]` nodes under `root` using a name → factory map.
|
|
63
|
+
* Clears server HTML inside each island and mounts the client app.
|
|
64
|
+
* Returns a dispose that tears down every island.
|
|
65
|
+
*/
|
|
66
|
+
export function hydrateIslands(
|
|
67
|
+
registry: IslandRegistry,
|
|
68
|
+
rootOrOptions: ParentNode | HydrateIslandsOptions = document,
|
|
69
|
+
): Dispose {
|
|
70
|
+
const options: HydrateIslandsOptions =
|
|
71
|
+
rootOrOptions && typeof rootOrOptions === "object" && "nodeType" in rootOrOptions
|
|
72
|
+
? { root: rootOrOptions as ParentNode }
|
|
73
|
+
: (rootOrOptions as HydrateIslandsOptions);
|
|
74
|
+
|
|
75
|
+
const root = options.root ?? document;
|
|
76
|
+
const preserveOnMissing = options.preserveOnMissing !== false;
|
|
77
|
+
const onMissing =
|
|
78
|
+
options.onMissing ??
|
|
79
|
+
((name: string) => {
|
|
80
|
+
console.warn(`[powers/ssr] No island registered for "${name}"`);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const disposers: Dispose[] = [];
|
|
84
|
+
const nodes = root.querySelectorAll?.(`[${ISLAND_ATTR}]`) ?? [];
|
|
85
|
+
|
|
86
|
+
nodes.forEach((node) => {
|
|
87
|
+
const el = node as HTMLElement;
|
|
88
|
+
const name = el.getAttribute(ISLAND_ATTR);
|
|
89
|
+
if (!name) return;
|
|
90
|
+
const factory = registry[name];
|
|
91
|
+
if (!factory) {
|
|
92
|
+
onMissing(name, el);
|
|
93
|
+
if (!preserveOnMissing) el.textContent = "";
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
// Replace server markup with live app
|
|
97
|
+
el.textContent = "";
|
|
98
|
+
disposers.push(mount(el, factory));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return () => {
|
|
102
|
+
for (const d of disposers) d();
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Hydrate a single island by name (first match).
|
|
108
|
+
*/
|
|
109
|
+
export function hydrateIsland(
|
|
110
|
+
name: string,
|
|
111
|
+
factory: IslandFactory,
|
|
112
|
+
root: ParentNode = document,
|
|
113
|
+
): Dispose {
|
|
114
|
+
return hydrateIslands({ [name]: factory }, { root });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Static HTML placeholder for an island (no happy-dom needed).
|
|
119
|
+
* Useful when the shell is a template string.
|
|
120
|
+
*/
|
|
121
|
+
export function islandPlaceholder(name: string, fallbackHtml = ""): string {
|
|
122
|
+
return `<div ${ISLAND_ATTR}="${escapeAttr(name)}">${fallbackHtml}</div>`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function escapeAttr(s: string): string {
|
|
126
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** @internal */
|
|
130
|
+
export function createIslandRoot(app: IslandFactory): {
|
|
131
|
+
host: HTMLElement;
|
|
132
|
+
dispose: Dispose;
|
|
133
|
+
} {
|
|
134
|
+
const host = document.createElement("div");
|
|
135
|
+
let dispose!: Dispose;
|
|
136
|
+
createRoot((d) => {
|
|
137
|
+
dispose = d;
|
|
138
|
+
const result = app();
|
|
139
|
+
if (result == null) return;
|
|
140
|
+
const nodes = Array.isArray(result) ? result : [result];
|
|
141
|
+
for (const n of nodes) {
|
|
142
|
+
if (n) host.appendChild(n as Node);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
return { host, dispose };
|
|
146
|
+
}
|
package/src/registry.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Dispose } from "@lab206/core";
|
|
2
|
+
import type { IslandFactory, IslandRegistry } from "./islands.js";
|
|
3
|
+
import { hydrateIslands, ISLAND_ATTR } from "./islands.js";
|
|
4
|
+
|
|
5
|
+
export interface IslandRegistryApi {
|
|
6
|
+
/** Registered factories */
|
|
7
|
+
readonly entries: IslandRegistry;
|
|
8
|
+
/** Register or replace an island factory */
|
|
9
|
+
register(name: string, factory: IslandFactory): void;
|
|
10
|
+
/** Register many islands at once */
|
|
11
|
+
registerAll(map: IslandRegistry): void;
|
|
12
|
+
/** Hydrate all matching islands in the document (or root) */
|
|
13
|
+
hydrate(root?: ParentNode): Dispose;
|
|
14
|
+
/** List island names present under root (DOM) */
|
|
15
|
+
discover(root?: ParentNode): string[];
|
|
16
|
+
/** Names registered but not found in the document */
|
|
17
|
+
missingInDom(root?: ParentNode): string[];
|
|
18
|
+
/** Island names in the DOM that have no factory */
|
|
19
|
+
missingInRegistry(root?: ParentNode): string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Typed-ish island registry for client entrypoints.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const islands = defineIslands({
|
|
28
|
+
* counter: () => Counter(),
|
|
29
|
+
* cart: () => CartWidget(),
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // after HTML is in the document:
|
|
33
|
+
* islands.hydrate();
|
|
34
|
+
* console.log(islands.missingInRegistry()); // unexpected SSR islands
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function defineIslands(
|
|
38
|
+
initial: IslandRegistry = {},
|
|
39
|
+
): IslandRegistryApi {
|
|
40
|
+
const entries: IslandRegistry = { ...initial };
|
|
41
|
+
|
|
42
|
+
function register(name: string, factory: IslandFactory) {
|
|
43
|
+
entries[name] = factory;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function registerAll(map: IslandRegistry) {
|
|
47
|
+
Object.assign(entries, map);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function discover(root: ParentNode = document): string[] {
|
|
51
|
+
const nodes = root.querySelectorAll?.(`[${ISLAND_ATTR}]`) ?? [];
|
|
52
|
+
const names: string[] = [];
|
|
53
|
+
nodes.forEach((n) => {
|
|
54
|
+
const name = (n as Element).getAttribute(ISLAND_ATTR);
|
|
55
|
+
if (name) names.push(name);
|
|
56
|
+
});
|
|
57
|
+
return names;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function missingInDom(root: ParentNode = document): string[] {
|
|
61
|
+
const present = new Set(discover(root));
|
|
62
|
+
return Object.keys(entries).filter((k) => !present.has(k));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function missingInRegistry(root: ParentNode = document): string[] {
|
|
66
|
+
return discover(root).filter((name) => !entries[name]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hydrate(root: ParentNode = document): Dispose {
|
|
70
|
+
const unknown = missingInRegistry(root);
|
|
71
|
+
if (unknown.length) {
|
|
72
|
+
console.warn(
|
|
73
|
+
`[powers/ssr] DOM islands with no registry entry: ${unknown.join(", ")}`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return hydrateIslands(entries, root);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
entries,
|
|
81
|
+
register,
|
|
82
|
+
registerAll,
|
|
83
|
+
hydrate,
|
|
84
|
+
discover,
|
|
85
|
+
missingInDom,
|
|
86
|
+
missingInRegistry,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Parse island names from an HTML string (no DOM required).
|
|
92
|
+
*/
|
|
93
|
+
export function listIslandsInHtml(html: string): string[] {
|
|
94
|
+
const re = /data-pu-island=["']([^"']+)["']/g;
|
|
95
|
+
const names: string[] = [];
|
|
96
|
+
let m: RegExpExecArray | null;
|
|
97
|
+
while ((m = re.exec(html))) {
|
|
98
|
+
if (m[1]) names.push(m[1]);
|
|
99
|
+
}
|
|
100
|
+
return names;
|
|
101
|
+
}
|