@pyreon/server 0.5.5 → 0.5.7
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/lib/types/client.d.ts +41 -155
- package/lib/types/client.d.ts.map +1 -1
- package/lib/types/index.d.ts +184 -323
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +7 -7
package/lib/types/client.d.ts
CHANGED
|
@@ -1,160 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { hydrateRoot, mount } from "@pyreon/runtime-dom";
|
|
1
|
+
import { ComponentFn } from "@pyreon/core";
|
|
2
|
+
import { RouteRecord } from "@pyreon/router";
|
|
4
3
|
|
|
5
|
-
//#region src/client.ts
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
* Returns a cleanup function that unmounts the app.
|
|
15
|
-
*/
|
|
16
|
-
function startClient(options) {
|
|
17
|
-
const {
|
|
18
|
-
App,
|
|
19
|
-
routes,
|
|
20
|
-
container = "#app"
|
|
21
|
-
} = options;
|
|
22
|
-
const el = typeof container === "string" ? document.querySelector(container) : container;
|
|
23
|
-
if (!el) throw new Error(`[pyreon/client] Container "${container}" not found`);
|
|
24
|
-
const router = createRouter({
|
|
25
|
-
routes,
|
|
26
|
-
mode: "history"
|
|
27
|
-
});
|
|
28
|
-
const loaderData = window.__PYREON_LOADER_DATA__;
|
|
29
|
-
if (loaderData && typeof loaderData === "object") hydrateLoaderData(router, loaderData);
|
|
30
|
-
const app = h(RouterProvider, {
|
|
31
|
-
router
|
|
32
|
-
}, h(App, null));
|
|
33
|
-
if (el.childNodes.length > 0) return hydrateRoot(el, app);
|
|
34
|
-
return mount(app, el);
|
|
4
|
+
//#region src/client.d.ts
|
|
5
|
+
interface StartClientOptions {
|
|
6
|
+
/** Root application component */
|
|
7
|
+
App: ComponentFn;
|
|
8
|
+
/** Route definitions (same as server) */
|
|
9
|
+
routes: RouteRecord[];
|
|
10
|
+
/** CSS selector or element for the app container (default: "#app") */
|
|
11
|
+
container?: string | Element;
|
|
35
12
|
}
|
|
36
13
|
/**
|
|
37
|
-
* Hydrate
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
14
|
+
* Hydrate a server-rendered Pyreon app on the client.
|
|
15
|
+
*
|
|
16
|
+
* Handles:
|
|
17
|
+
* - Router creation (history mode)
|
|
18
|
+
* - Loader data hydration from `window.__PYREON_LOADER_DATA__`
|
|
19
|
+
* - Hydration if container has SSR content, fresh mount otherwise
|
|
20
|
+
*
|
|
21
|
+
* Returns a cleanup function that unmounts the app.
|
|
22
|
+
*/
|
|
23
|
+
declare function startClient(options: StartClientOptions): () => void;
|
|
24
|
+
type IslandLoader = () => Promise<{
|
|
25
|
+
default: ComponentFn;
|
|
26
|
+
} | ComponentFn>;
|
|
48
27
|
/**
|
|
49
|
-
* Hydrate all `<pyreon-island>` elements on the page.
|
|
50
|
-
*
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (cleanup) cleanups.push(cleanup);
|
|
66
|
-
}
|
|
67
|
-
return () => {
|
|
68
|
-
for (const fn of cleanups) fn();
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
function scheduleHydration(el, loader, propsJson, strategy) {
|
|
72
|
-
let cancelled = false;
|
|
73
|
-
const hydrate = () => {
|
|
74
|
-
if (!cancelled) hydrateIsland(el, loader, propsJson);
|
|
75
|
-
};
|
|
76
|
-
switch (strategy) {
|
|
77
|
-
case "load":
|
|
78
|
-
hydrate();
|
|
79
|
-
return null;
|
|
80
|
-
case "idle":
|
|
81
|
-
{
|
|
82
|
-
if ("requestIdleCallback" in window) {
|
|
83
|
-
const id = requestIdleCallback(hydrate);
|
|
84
|
-
return () => {
|
|
85
|
-
cancelled = true;
|
|
86
|
-
cancelIdleCallback(id);
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
const id = setTimeout(hydrate, 200);
|
|
90
|
-
return () => {
|
|
91
|
-
cancelled = true;
|
|
92
|
-
clearTimeout(id);
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
case "visible":
|
|
96
|
-
return observeVisibility(el, hydrate);
|
|
97
|
-
case "never":
|
|
98
|
-
return null;
|
|
99
|
-
default:
|
|
100
|
-
if (strategy.startsWith("media(")) {
|
|
101
|
-
const query = strategy.slice(6, -1);
|
|
102
|
-
const mql = window.matchMedia(query);
|
|
103
|
-
if (mql.matches) {
|
|
104
|
-
hydrate();
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
const onChange = e => {
|
|
108
|
-
if (e.matches) {
|
|
109
|
-
mql.removeEventListener("change", onChange);
|
|
110
|
-
hydrate();
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
mql.addEventListener("change", onChange);
|
|
114
|
-
return () => {
|
|
115
|
-
cancelled = true;
|
|
116
|
-
mql.removeEventListener("change", onChange);
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
hydrate();
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
async function hydrateIsland(el, loader, propsJson) {
|
|
124
|
-
const name = el.getAttribute("data-component") ?? "unknown";
|
|
125
|
-
try {
|
|
126
|
-
let props;
|
|
127
|
-
try {
|
|
128
|
-
props = JSON.parse(propsJson);
|
|
129
|
-
if (typeof props !== "object" || props === null || Array.isArray(props)) throw new TypeError("Expected object");
|
|
130
|
-
} catch (parseErr) {
|
|
131
|
-
console.error(`Invalid island props JSON for "${name}"`, parseErr);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const mod = await loader();
|
|
135
|
-
hydrateRoot(el, h(typeof mod === "function" ? mod : mod.default, props));
|
|
136
|
-
} catch (err) {
|
|
137
|
-
console.error(`Failed to hydrate island "${name}"`, err);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function observeVisibility(el, callback) {
|
|
141
|
-
if (!("IntersectionObserver" in window)) {
|
|
142
|
-
callback();
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
const observer = new IntersectionObserver(entries => {
|
|
146
|
-
for (const entry of entries) if (entry.isIntersecting) {
|
|
147
|
-
observer.disconnect();
|
|
148
|
-
callback();
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
}, {
|
|
152
|
-
rootMargin: "200px"
|
|
153
|
-
});
|
|
154
|
-
observer.observe(el);
|
|
155
|
-
return () => observer.disconnect();
|
|
156
|
-
}
|
|
157
|
-
|
|
28
|
+
* Hydrate all `<pyreon-island>` elements on the page.
|
|
29
|
+
*
|
|
30
|
+
* Only loads JavaScript for components that are actually present in the HTML.
|
|
31
|
+
* Respects hydration strategies (load, idle, visible, media, never).
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* hydrateIslands({
|
|
35
|
+
* Counter: () => import("./Counter"),
|
|
36
|
+
* Search: () => import("./Search"),
|
|
37
|
+
* })
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Hydrate all `<pyreon-island>` elements on the page.
|
|
41
|
+
* Returns a cleanup function that disconnects any pending observers/listeners.
|
|
42
|
+
*/
|
|
43
|
+
declare function hydrateIslands(registry: Record<string, IslandLoader>): () => void;
|
|
158
44
|
//#endregion
|
|
159
|
-
export { hydrateIslands, startClient };
|
|
160
|
-
//# sourceMappingURL=
|
|
45
|
+
export { StartClientOptions, hydrateIslands, startClient };
|
|
46
|
+
//# sourceMappingURL=client2.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"client2.d.ts","names":[],"sources":["../../../src/client.ts"],"mappings":";;;;UAmCiB,kBAAA;EAiD4C;EA/C3D,GAAA,EAAK,WAAA;EA+C0B;EA7C/B,MAAA,EAAQ,WAAA;EA6CgB;EA3CxB,SAAA,YAAqB,OAAA;AAAA;;;;AA6DvB;;;;;;;iBAhDgB,WAAA,CAAY,OAAA,EAAS,kBAAA;AAAA,KA8BhC,YAAA,SAAqB,OAAA;EAAU,OAAA,EAAS,WAAA;AAAA,IAAgB,WAAA;;;;;;;;;;;;;;;;;iBAkB7C,cAAA,CAAe,QAAA,EAAU,MAAA,SAAe,YAAA"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,340 +1,201 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { RouterProvider, createRouter, prefetchLoaderData, serializeLoaderData } from "@pyreon/router";
|
|
4
|
-
import { renderToStream, runWithRequestContext } from "@pyreon/runtime-server";
|
|
5
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
6
|
-
import { dirname, join, resolve } from "node:path";
|
|
1
|
+
import { ComponentFn, Props } from "@pyreon/core";
|
|
2
|
+
import { RouteRecord } from "@pyreon/router";
|
|
7
3
|
|
|
8
|
-
//#region src/
|
|
4
|
+
//#region src/middleware.d.ts
|
|
9
5
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
6
|
+
* SSR middleware — simple request processing pipeline.
|
|
7
|
+
*
|
|
8
|
+
* Middleware runs before rendering. Return a Response to short-circuit
|
|
9
|
+
* (e.g. for redirects, auth checks, or static file serving).
|
|
10
|
+
* Return void / undefined to continue to the next middleware or rendering.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const authMiddleware: Middleware = async (ctx) => {
|
|
14
|
+
* const token = ctx.req.headers.get("Authorization")
|
|
15
|
+
* if (!token) return new Response("Unauthorized", { status: 401 })
|
|
16
|
+
* ctx.locals.user = await verifyToken(token)
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* const handler = createHandler({
|
|
20
|
+
* App,
|
|
21
|
+
* routes,
|
|
22
|
+
* middleware: [authMiddleware],
|
|
23
|
+
* })
|
|
24
|
+
*/
|
|
25
|
+
interface MiddlewareContext {
|
|
26
|
+
/** The incoming request */
|
|
27
|
+
req: Request;
|
|
28
|
+
/** Parsed URL */
|
|
29
|
+
url: URL;
|
|
30
|
+
/** Pathname + search (passed to router) */
|
|
31
|
+
path: string;
|
|
32
|
+
/** Response headers — middleware can set custom headers */
|
|
33
|
+
headers: Headers;
|
|
34
|
+
/** Arbitrary per-request data shared between middleware and components */
|
|
35
|
+
locals: Record<string, unknown>;
|
|
39
36
|
}
|
|
40
37
|
/**
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
* 1. Inline script with serialized loader data (if any)
|
|
45
|
-
* 2. Module script tag pointing to the client entry
|
|
46
|
-
*/
|
|
47
|
-
function buildScripts(clientEntry, loaderData) {
|
|
48
|
-
const parts = [];
|
|
49
|
-
if (loaderData && Object.keys(loaderData).length > 0) {
|
|
50
|
-
const json = JSON.stringify(loaderData).replace(/<\//g, "<\\/");
|
|
51
|
-
parts.push(`<script>window.__PYREON_LOADER_DATA__=${json}<\/script>`);
|
|
52
|
-
}
|
|
53
|
-
parts.push(`<script type="module" src="${clientEntry}"><\/script>`);
|
|
54
|
-
return parts.join("\n ");
|
|
55
|
-
}
|
|
56
|
-
/** Pre-build the static client entry script tag (invariant across requests) */
|
|
57
|
-
function buildClientEntryTag(clientEntry) {
|
|
58
|
-
return `<script type="module" src="${clientEntry}"><\/script>`;
|
|
59
|
-
}
|
|
60
|
-
/** Fast path: build scripts with a pre-built client entry tag */
|
|
61
|
-
function buildScriptsFast(clientEntryTag, loaderData) {
|
|
62
|
-
if (loaderData && Object.keys(loaderData).length > 0) return `<script>window.__PYREON_LOADER_DATA__=${JSON.stringify(loaderData).replace(/<\//g, "<\\/")}<\/script>\n ${clientEntryTag}`;
|
|
63
|
-
return clientEntryTag;
|
|
64
|
-
}
|
|
65
|
-
|
|
38
|
+
* Middleware function. Return a Response to short-circuit, or void to continue.
|
|
39
|
+
*/
|
|
40
|
+
type Middleware = (ctx: MiddlewareContext) => Response | void | Promise<Response | void>;
|
|
66
41
|
//#endregion
|
|
67
|
-
//#region src/handler.ts
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const result = await mw(ctx);
|
|
93
|
-
if (result instanceof Response) return result;
|
|
94
|
-
}
|
|
95
|
-
const router = createRouter({
|
|
96
|
-
routes,
|
|
97
|
-
mode: "history",
|
|
98
|
-
url: path
|
|
99
|
-
});
|
|
100
|
-
return runWithRequestContext(async () => {
|
|
101
|
-
try {
|
|
102
|
-
await prefetchLoaderData(router, path);
|
|
103
|
-
const app = h(RouterProvider, {
|
|
104
|
-
router
|
|
105
|
-
}, h(App, null));
|
|
106
|
-
if (mode === "stream") return renderStreamResponse(app, router, compiled, clientEntryTag, ctx.headers);
|
|
107
|
-
const {
|
|
108
|
-
html: appHtml,
|
|
109
|
-
head
|
|
110
|
-
} = await renderWithHead(app);
|
|
111
|
-
const fullHtml = processCompiledTemplate(compiled, {
|
|
112
|
-
head,
|
|
113
|
-
app: appHtml,
|
|
114
|
-
scripts: buildScriptsFast(clientEntryTag, serializeLoaderData(router))
|
|
115
|
-
});
|
|
116
|
-
return new Response(fullHtml, {
|
|
117
|
-
status: 200,
|
|
118
|
-
headers: ctx.headers
|
|
119
|
-
});
|
|
120
|
-
} catch (_err) {
|
|
121
|
-
return new Response("Internal Server Error", {
|
|
122
|
-
status: 500,
|
|
123
|
-
headers: {
|
|
124
|
-
"Content-Type": "text/plain"
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Streaming mode: shell is emitted immediately, app content streams progressively.
|
|
133
|
-
*
|
|
134
|
-
* Head tags from the initial synchronous render are included in the shell.
|
|
135
|
-
* Suspense boundaries resolve out-of-order via inline <template> + swap scripts.
|
|
136
|
-
*/
|
|
137
|
-
async function renderStreamResponse(app, router, compiled, clientEntryTag, extraHeaders) {
|
|
138
|
-
const scripts = buildScriptsFast(clientEntryTag, serializeLoaderData(router));
|
|
139
|
-
const [p0, p1, p2, p3] = compiled.parts;
|
|
140
|
-
const shellHead = p0 + p1;
|
|
141
|
-
const shellTail = p2 + scripts + p3;
|
|
142
|
-
const reader = renderToStream(app).getReader();
|
|
143
|
-
const stream = new ReadableStream({
|
|
144
|
-
async start(controller) {
|
|
145
|
-
const encoder = new TextEncoder();
|
|
146
|
-
const push = s => controller.enqueue(encoder.encode(s));
|
|
147
|
-
try {
|
|
148
|
-
push(shellHead);
|
|
149
|
-
let done = false;
|
|
150
|
-
while (!done) {
|
|
151
|
-
const result = await reader.read();
|
|
152
|
-
done = result.done;
|
|
153
|
-
if (result.value) push(result.value);
|
|
154
|
-
}
|
|
155
|
-
push(shellTail);
|
|
156
|
-
} catch (_err) {
|
|
157
|
-
push(`<script>console.error("[pyreon/server] Stream render failed")<\/script>`);
|
|
158
|
-
push(shellTail);
|
|
159
|
-
} finally {
|
|
160
|
-
controller.close();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
return new Response(stream, {
|
|
165
|
-
status: 200,
|
|
166
|
-
headers: extraHeaders
|
|
167
|
-
});
|
|
42
|
+
//#region src/handler.d.ts
|
|
43
|
+
interface HandlerOptions {
|
|
44
|
+
/** Root application component */
|
|
45
|
+
App: ComponentFn;
|
|
46
|
+
/** Route definitions */
|
|
47
|
+
routes: RouteRecord[];
|
|
48
|
+
/**
|
|
49
|
+
* HTML template with placeholders:
|
|
50
|
+
* <!--pyreon-head--> — head tags (title, meta, link, etc.)
|
|
51
|
+
* <!--pyreon-app--> — rendered app HTML
|
|
52
|
+
* <!--pyreon-scripts--> — client entry + loader data
|
|
53
|
+
*
|
|
54
|
+
* Defaults to a minimal HTML5 template.
|
|
55
|
+
*/
|
|
56
|
+
template?: string;
|
|
57
|
+
/** Path to the client entry module (default: "/src/entry-client.ts") */
|
|
58
|
+
clientEntry?: string;
|
|
59
|
+
/** Middleware chain — runs before rendering */
|
|
60
|
+
middleware?: Middleware[];
|
|
61
|
+
/**
|
|
62
|
+
* Rendering mode:
|
|
63
|
+
* "string" (default) — full renderToString, complete HTML in one response
|
|
64
|
+
* "stream" — progressive streaming via renderToStream (Suspense out-of-order)
|
|
65
|
+
*/
|
|
66
|
+
mode?: "string" | "stream";
|
|
168
67
|
}
|
|
169
|
-
|
|
68
|
+
declare function createHandler(options: HandlerOptions): (req: Request) => Promise<Response>;
|
|
170
69
|
//#endregion
|
|
171
|
-
//#region src/
|
|
70
|
+
//#region src/html.d.ts
|
|
172
71
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const wrapper = async function IslandWrapper(props) {
|
|
186
|
-
const mod = await loader();
|
|
187
|
-
const Comp = typeof mod === "function" ? mod : mod.default;
|
|
188
|
-
const serializedProps = serializeIslandProps(props);
|
|
189
|
-
return h("pyreon-island", {
|
|
190
|
-
"data-component": name,
|
|
191
|
-
"data-props": serializedProps,
|
|
192
|
-
"data-hydrate": hydrate
|
|
193
|
-
}, h(Comp, props));
|
|
194
|
-
};
|
|
195
|
-
Object.defineProperties(wrapper, {
|
|
196
|
-
__island: {
|
|
197
|
-
value: true,
|
|
198
|
-
enumerable: true
|
|
199
|
-
},
|
|
200
|
-
name: {
|
|
201
|
-
value: name,
|
|
202
|
-
enumerable: true,
|
|
203
|
-
writable: false,
|
|
204
|
-
configurable: true
|
|
205
|
-
},
|
|
206
|
-
hydrate: {
|
|
207
|
-
value: hydrate,
|
|
208
|
-
enumerable: true
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
return wrapper;
|
|
72
|
+
* HTML template processing for SSR/SSG.
|
|
73
|
+
*
|
|
74
|
+
* Templates use comment placeholders:
|
|
75
|
+
* <!--pyreon-head--> — replaced with <head> tags (title, meta, link, etc.)
|
|
76
|
+
* <!--pyreon-app--> — replaced with rendered application HTML
|
|
77
|
+
* <!--pyreon-scripts--> — replaced with client entry script + inline loader data
|
|
78
|
+
*/
|
|
79
|
+
declare const DEFAULT_TEMPLATE = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <!--pyreon-head-->\n</head>\n<body>\n <div id=\"app\"><!--pyreon-app--></div>\n <!--pyreon-scripts-->\n</body>\n</html>";
|
|
80
|
+
interface TemplateData {
|
|
81
|
+
head: string;
|
|
82
|
+
app: string;
|
|
83
|
+
scripts: string;
|
|
212
84
|
}
|
|
213
85
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*/
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
if (key === "children") continue;
|
|
221
|
-
if (typeof value === "function") continue;
|
|
222
|
-
if (typeof value === "symbol") continue;
|
|
223
|
-
if (value === void 0) continue;
|
|
224
|
-
clean[key] = value;
|
|
225
|
-
}
|
|
226
|
-
return JSON.stringify(clean);
|
|
86
|
+
* Pre-compiled template — splits the template string once so that
|
|
87
|
+
* each request only concatenates 6 parts instead of scanning 3x with `.replace()`.
|
|
88
|
+
*/
|
|
89
|
+
interface CompiledTemplate {
|
|
90
|
+
/** [before-head, between-head-app, between-app-scripts, after-scripts] */
|
|
91
|
+
parts: [string, string, string, string];
|
|
227
92
|
}
|
|
228
|
-
|
|
93
|
+
declare function compileTemplate(template: string): CompiledTemplate;
|
|
94
|
+
declare function processTemplate(template: string, data: TemplateData): string;
|
|
95
|
+
/** Fast path using a pre-compiled template */
|
|
96
|
+
declare function processCompiledTemplate(compiled: CompiledTemplate, data: TemplateData): string;
|
|
97
|
+
/**
|
|
98
|
+
* Build the script tags for client hydration.
|
|
99
|
+
*
|
|
100
|
+
* Emits:
|
|
101
|
+
* 1. Inline script with serialized loader data (if any)
|
|
102
|
+
* 2. Module script tag pointing to the client entry
|
|
103
|
+
*/
|
|
104
|
+
declare function buildScripts(clientEntry: string, loaderData: Record<string, unknown> | null): string;
|
|
229
105
|
//#endregion
|
|
230
|
-
//#region src/
|
|
106
|
+
//#region src/island.d.ts
|
|
107
|
+
type HydrationStrategy = "load" | "idle" | "visible" | "never" | `media(${string})`;
|
|
108
|
+
interface IslandOptions {
|
|
109
|
+
/** Unique name — must match the key in the client-side hydrateIslands() registry */
|
|
110
|
+
name: string;
|
|
111
|
+
/** When to hydrate on the client (default: "load") */
|
|
112
|
+
hydrate?: HydrationStrategy;
|
|
113
|
+
}
|
|
114
|
+
interface IslandMeta {
|
|
115
|
+
readonly __island: true;
|
|
116
|
+
readonly name: string;
|
|
117
|
+
readonly hydrate: HydrationStrategy;
|
|
118
|
+
}
|
|
231
119
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
* handler,
|
|
245
|
-
* paths: ["/", "/about", "/blog", "/blog/hello-world"],
|
|
246
|
-
* outDir: "dist",
|
|
247
|
-
* })
|
|
248
|
-
*
|
|
249
|
-
* @example
|
|
250
|
-
* // Dynamic paths from a CMS or filesystem
|
|
251
|
-
* await prerender({
|
|
252
|
-
* handler,
|
|
253
|
-
* paths: async () => {
|
|
254
|
-
* const posts = await fetchAllPosts()
|
|
255
|
-
* return ["/", "/about", ...posts.map(p => `/blog/${p.slug}`)]
|
|
256
|
-
* },
|
|
257
|
-
* outDir: "dist",
|
|
258
|
-
* })
|
|
259
|
-
*/
|
|
120
|
+
* Create an island component.
|
|
121
|
+
*
|
|
122
|
+
* Returns an async ComponentFn that:
|
|
123
|
+
* 1. Resolves the dynamic import
|
|
124
|
+
* 2. Renders the component to VNodes
|
|
125
|
+
* 3. Wraps the output in `<pyreon-island>` with serialized props + hydration strategy
|
|
126
|
+
*/
|
|
127
|
+
declare function island<P extends Props = Props>(loader: () => Promise<{
|
|
128
|
+
default: ComponentFn<P>;
|
|
129
|
+
} | ComponentFn<P>>, options: IslandOptions): ComponentFn<P> & IslandMeta;
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/ssg.d.ts
|
|
260
132
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
});
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
await mkdir(dirname(filePath), {
|
|
307
|
-
recursive: true
|
|
308
|
-
});
|
|
309
|
-
await writeFile(filePath, html, "utf-8");
|
|
310
|
-
pages++;
|
|
311
|
-
}
|
|
312
|
-
const BATCH_SIZE = 10;
|
|
313
|
-
for (let i = 0; i < paths.length; i += BATCH_SIZE) {
|
|
314
|
-
const batch = paths.slice(i, i + BATCH_SIZE);
|
|
315
|
-
await Promise.all(batch.map(async path => {
|
|
316
|
-
try {
|
|
317
|
-
await renderPage(path);
|
|
318
|
-
} catch (error) {
|
|
319
|
-
errors.push({
|
|
320
|
-
path,
|
|
321
|
-
error
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
}));
|
|
325
|
-
}
|
|
326
|
-
return {
|
|
327
|
-
pages,
|
|
328
|
-
errors,
|
|
329
|
-
elapsed: Date.now() - start
|
|
330
|
-
};
|
|
133
|
+
* Static Site Generation — pre-render routes to HTML files at build time.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* // ssg.ts (run with: bun run ssg.ts)
|
|
137
|
+
* import { createHandler } from "@pyreon/server"
|
|
138
|
+
* import { prerender } from "@pyreon/server"
|
|
139
|
+
* import { App } from "./src/App"
|
|
140
|
+
* import { routes } from "./src/routes"
|
|
141
|
+
*
|
|
142
|
+
* const handler = createHandler({ App, routes })
|
|
143
|
+
*
|
|
144
|
+
* await prerender({
|
|
145
|
+
* handler,
|
|
146
|
+
* paths: ["/", "/about", "/blog", "/blog/hello-world"],
|
|
147
|
+
* outDir: "dist",
|
|
148
|
+
* })
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* // Dynamic paths from a CMS or filesystem
|
|
152
|
+
* await prerender({
|
|
153
|
+
* handler,
|
|
154
|
+
* paths: async () => {
|
|
155
|
+
* const posts = await fetchAllPosts()
|
|
156
|
+
* return ["/", "/about", ...posts.map(p => `/blog/${p.slug}`)]
|
|
157
|
+
* },
|
|
158
|
+
* outDir: "dist",
|
|
159
|
+
* })
|
|
160
|
+
*/
|
|
161
|
+
interface PrerenderOptions {
|
|
162
|
+
/** SSR handler created by createHandler() */
|
|
163
|
+
handler: (req: Request) => Promise<Response>;
|
|
164
|
+
/** Routes to pre-render — array of URL paths or async function that returns them */
|
|
165
|
+
paths: string[] | (() => string[] | Promise<string[]>);
|
|
166
|
+
/** Output directory for the generated HTML files */
|
|
167
|
+
outDir: string;
|
|
168
|
+
/** Origin for constructing full URLs (default: "http://localhost") */
|
|
169
|
+
origin?: string;
|
|
170
|
+
/**
|
|
171
|
+
* Called after each page is rendered — use for logging or progress tracking.
|
|
172
|
+
* Return false to skip writing this page.
|
|
173
|
+
*/
|
|
174
|
+
onPage?: (path: string, html: string) => void | boolean | Promise<void | boolean>;
|
|
331
175
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
176
|
+
interface PrerenderResult {
|
|
177
|
+
/** Number of pages generated */
|
|
178
|
+
pages: number;
|
|
179
|
+
/** Paths that failed to render */
|
|
180
|
+
errors: {
|
|
181
|
+
path: string;
|
|
182
|
+
error: unknown;
|
|
183
|
+
}[];
|
|
184
|
+
/** Total elapsed time in milliseconds */
|
|
185
|
+
elapsed: number;
|
|
336
186
|
}
|
|
337
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Pre-render a list of routes to static HTML files.
|
|
189
|
+
*
|
|
190
|
+
* For each path:
|
|
191
|
+
* 1. Constructs a Request for the path
|
|
192
|
+
* 2. Calls the SSR handler to render to HTML
|
|
193
|
+
* 3. Writes the HTML to `outDir/<path>/index.html`
|
|
194
|
+
*
|
|
195
|
+
* The root path "/" becomes `outDir/index.html`.
|
|
196
|
+
* Paths like "/about" become `outDir/about/index.html`.
|
|
197
|
+
*/
|
|
198
|
+
declare function prerender(options: PrerenderOptions): Promise<PrerenderResult>;
|
|
338
199
|
//#endregion
|
|
339
|
-
export { DEFAULT_TEMPLATE, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate };
|
|
340
|
-
//# sourceMappingURL=
|
|
200
|
+
export { type CompiledTemplate, DEFAULT_TEMPLATE, type HandlerOptions, type HydrationStrategy, type IslandMeta, type IslandOptions, type Middleware, type MiddlewareContext, type PrerenderOptions, type PrerenderResult, type TemplateData, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate };
|
|
201
|
+
//# sourceMappingURL=index2.d.ts.map
|
package/lib/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../../src/middleware.ts","../../../src/handler.ts","../../../src/html.ts","../../../src/island.ts","../../../src/ssg.ts"],"mappings":";;;;;;;;AAqBA;;;;;;;;;;;;;;;;UAAiB,iBAAA;EAUP;EARR,GAAA,EAAK,OAAA;EAQS;EANd,GAAA,EAAK,GAAA;EAae;EAXpB,IAAA;EAW6B;EAT7B,OAAA,EAAS,OAAA;EASoE;EAP7E,MAAA,EAAQ,MAAA;AAAA;;;;KAOE,UAAA,IAAc,GAAA,EAAK,iBAAA,KAAsB,QAAA,UAAkB,OAAA,CAAQ,QAAA;;;UCS9D,cAAA;EDT8D;ECW7E,GAAA,EAAK,WAAA;EDXuE;ECa5E,MAAA,EAAQ,WAAA;EDbqB;;;;;;;;ECsB7B,QAAA;;EAEA,WAAA;EAf6B;EAiB7B,UAAA,GAAa,UAAA;EAfR;;;;;EAqBL,IAAA;AAAA;AAAA,iBAGc,aAAA,CAAc,OAAA,EAAS,cAAA,IAAkB,GAAA,EAAK,OAAA,KAAY,OAAA,CAAQ,QAAA;;;;;;;ADpDlF;;;;cEZa,gBAAA;AAAA,UAaI,YAAA;EACf,IAAA;EACA,GAAA;EACA,OAAA;AAAA;;;;;UAOe,gBAAA;EFHN;EEKT,KAAA;AAAA;AAAA,iBAGc,eAAA,CAAgB,QAAA,WAAmB,gBAAA;AAAA,iBAgBnC,eAAA,CAAgB,QAAA,UAAkB,IAAA,EAAM,YAAA;AFfxD;AAAA,iBEuBgB,uBAAA,CAAwB,QAAA,EAAU,gBAAA,EAAkB,IAAA,EAAM,YAAA;;;;;;;;iBAY1D,YAAA,CACd,WAAA,UACA,UAAA,EAAY,MAAA;;;KChBF,iBAAA;AAAA,UAEK,aAAA;EFCf;EECA,IAAA;EFCa;EECb,OAAA,GAAU,iBAAA;AAAA;AAAA,UAGK,UAAA;EAAA,SACN,QAAA;EAAA,SACA,IAAA;EAAA,SACA,OAAA,EAAS,iBAAA;AAAA;;;;;;;;;iBAaJ,MAAA,WAAiB,KAAA,GAAQ,KAAA,CAAA,CACvC,MAAA,QAAc,OAAA;EAAU,OAAA,EAAS,WAAA,CAAY,CAAA;AAAA,IAAO,WAAA,CAAY,CAAA,IAChE,OAAA,EAAS,aAAA,GACR,WAAA,CAAY,CAAA,IAAK,UAAA;;;;;;;AHlEpB;;;;;;;;;;;;;;;;;;;;AAiBA;;;;;UILiB,gBAAA;EJKsD;EIHrE,OAAA,GAAU,GAAA,EAAK,OAAA,KAAY,OAAA,CAAQ,QAAA;EJGyC;EID5E,KAAA,+BAAoC,OAAA;EJCZ;EICxB,MAAA;EJDqE;EIGrE,MAAA;EJHqF;;;;EISrF,MAAA,IAAU,IAAA,UAAc,IAAA,8BAAkC,OAAA;AAAA;AAAA,UAG3C,eAAA;;EAEf,KAAA;EHDQ;EGGR,MAAA;IAAU,IAAA;IAAc,KAAA;EAAA;EHLnB;EGOL,OAAA;AAAA;;;;;;;;AHiBF;;;;iBGHsB,SAAA,CAAU,OAAA,EAAS,gBAAA,GAAmB,OAAA,CAAQ,eAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/server",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"description": "SSR handler, SSG prerender, and island architecture for Pyreon",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"prepublishOnly": "bun run build"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@pyreon/core": "^0.5.
|
|
48
|
-
"@pyreon/head": "^0.5.
|
|
49
|
-
"@pyreon/reactivity": "^0.5.
|
|
50
|
-
"@pyreon/router": "^0.5.
|
|
51
|
-
"@pyreon/runtime-dom": "^0.5.
|
|
52
|
-
"@pyreon/runtime-server": "^0.5.
|
|
47
|
+
"@pyreon/core": "^0.5.7",
|
|
48
|
+
"@pyreon/head": "^0.5.7",
|
|
49
|
+
"@pyreon/reactivity": "^0.5.7",
|
|
50
|
+
"@pyreon/router": "^0.5.7",
|
|
51
|
+
"@pyreon/runtime-dom": "^0.5.7",
|
|
52
|
+
"@pyreon/runtime-server": "^0.5.7"
|
|
53
53
|
},
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"access": "public"
|