@vertz/ui-server 0.2.31 → 0.2.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vertz/ui-server",
3
- "version": "0.2.31",
3
+ "version": "0.2.32",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Vertz UI server-side rendering runtime",
@@ -1,216 +0,0 @@
1
- import {
2
- encodeChunk,
3
- escapeAttr,
4
- injectIntoTemplate,
5
- precomputeHandlerState,
6
- resolveRouteModulepreload,
7
- resolveSession,
8
- safeSerialize,
9
- ssrRenderProgressive,
10
- ssrRenderSinglePass,
11
- ssrStreamNavQueries
12
- } from "./chunk-34fexgex.js";
13
-
14
- // src/ssr-progressive-response.ts
15
- function buildProgressiveResponse(options) {
16
- const { headChunk, renderStream, tailChunk, ssrData, nonce, headers } = options;
17
- const stream = new ReadableStream({
18
- async start(controller) {
19
- controller.enqueue(encodeChunk(headChunk));
20
- const reader = renderStream.getReader();
21
- let renderError;
22
- try {
23
- for (;; ) {
24
- const { done, value } = await reader.read();
25
- if (done)
26
- break;
27
- controller.enqueue(value);
28
- }
29
- } catch (err) {
30
- renderError = err instanceof Error ? err : new Error(String(err));
31
- }
32
- if (renderError) {
33
- console.error("[SSR] Render error after head sent:", renderError.message);
34
- const nonceAttr = nonce != null ? ` nonce="${escapeAttr(nonce)}"` : "";
35
- const errorScript = `<script${nonceAttr}>document.dispatchEvent(new CustomEvent('vertz:ssr-error',` + `{detail:{message:${safeSerialize(renderError.message)}}}))</script>`;
36
- controller.enqueue(encodeChunk(errorScript));
37
- }
38
- let tail = "";
39
- if (ssrData.length > 0) {
40
- const nonceAttr = nonce != null ? ` nonce="${escapeAttr(nonce)}"` : "";
41
- tail += `<script${nonceAttr}>window.__VERTZ_SSR_DATA__=${safeSerialize(ssrData)};</script>`;
42
- }
43
- tail += tailChunk;
44
- controller.enqueue(encodeChunk(tail));
45
- controller.close();
46
- }
47
- });
48
- const responseHeaders = {
49
- "Content-Type": "text/html; charset=utf-8",
50
- ...headers
51
- };
52
- return new Response(stream, { status: 200, headers: responseHeaders });
53
- }
54
-
55
- // src/ssr-handler.ts
56
- function createSSRHandler(options) {
57
- const {
58
- module,
59
- ssrTimeout,
60
- nonce,
61
- fallbackMetrics,
62
- routeChunkManifest,
63
- cacheControl,
64
- sessionResolver,
65
- manifest,
66
- progressiveHTML
67
- } = options;
68
- const { template, linkHeader, modulepreloadTags, splitResult } = precomputeHandlerState(options);
69
- return async (request) => {
70
- const url = new URL(request.url);
71
- const pathname = url.pathname;
72
- if (request.headers.get("x-vertz-nav") === "1") {
73
- return handleNavRequest(module, pathname, ssrTimeout);
74
- }
75
- let sessionScript = "";
76
- let ssrAuth;
77
- if (sessionResolver) {
78
- const result = await resolveSession(request, sessionResolver, nonce);
79
- sessionScript = result.sessionScript;
80
- ssrAuth = result.ssrAuth;
81
- }
82
- const useProgressive = progressiveHTML && splitResult && !(manifest?.routeEntries && Object.keys(manifest.routeEntries).length > 0);
83
- if (useProgressive) {
84
- return handleProgressiveHTMLRequest(module, splitResult, pathname + url.search, ssrTimeout, nonce, fallbackMetrics, linkHeader, modulepreloadTags, routeChunkManifest, cacheControl, sessionScript, ssrAuth, manifest);
85
- }
86
- return handleHTMLRequest(module, template, pathname + url.search, ssrTimeout, nonce, fallbackMetrics, linkHeader, modulepreloadTags, routeChunkManifest, cacheControl, sessionScript, ssrAuth, manifest);
87
- };
88
- }
89
- async function handleNavRequest(module, url, ssrTimeout) {
90
- try {
91
- const stream = await ssrStreamNavQueries(module, url, { ssrTimeout });
92
- return new Response(stream, {
93
- status: 200,
94
- headers: {
95
- "Content-Type": "text/event-stream",
96
- "Cache-Control": "no-cache"
97
- }
98
- });
99
- } catch {
100
- return new Response(`event: done
101
- data: {}
102
-
103
- `, {
104
- status: 200,
105
- headers: {
106
- "Content-Type": "text/event-stream",
107
- "Cache-Control": "no-cache"
108
- }
109
- });
110
- }
111
- }
112
- async function handleProgressiveHTMLRequest(module, split, url, ssrTimeout, nonce, fallbackMetrics, linkHeader, staticModulepreloadTags, routeChunkManifest, cacheControl, sessionScript, ssrAuth, manifest) {
113
- try {
114
- const result = await ssrRenderProgressive(module, url, {
115
- ssrTimeout,
116
- fallbackMetrics,
117
- ssrAuth,
118
- manifest
119
- });
120
- if (result.redirect) {
121
- return new Response(null, {
122
- status: 302,
123
- headers: { Location: result.redirect.to }
124
- });
125
- }
126
- const modulepreloadTags = resolveRouteModulepreload(routeChunkManifest, result.matchedRoutePatterns, staticModulepreloadTags);
127
- let headChunk = split.headTemplate;
128
- const headCloseIdx = headChunk.lastIndexOf("</head>");
129
- if (headCloseIdx !== -1) {
130
- const injections = [];
131
- if (result.css)
132
- injections.push(result.css);
133
- if (result.headTags)
134
- injections.push(result.headTags);
135
- if (modulepreloadTags)
136
- injections.push(modulepreloadTags);
137
- if (sessionScript)
138
- injections.push(sessionScript);
139
- if (injections.length > 0) {
140
- headChunk = headChunk.slice(0, headCloseIdx) + injections.join(`
141
- `) + `
142
- ` + headChunk.slice(headCloseIdx);
143
- }
144
- } else {
145
- if (result.css)
146
- headChunk += result.css;
147
- if (result.headTags)
148
- headChunk += result.headTags;
149
- if (modulepreloadTags)
150
- headChunk += modulepreloadTags;
151
- if (sessionScript)
152
- headChunk += sessionScript;
153
- }
154
- const headers = {};
155
- if (linkHeader)
156
- headers.Link = linkHeader;
157
- if (cacheControl)
158
- headers["Cache-Control"] = cacheControl;
159
- return buildProgressiveResponse({
160
- headChunk,
161
- renderStream: result.renderStream,
162
- tailChunk: split.tailTemplate,
163
- ssrData: result.ssrData,
164
- nonce,
165
- headers
166
- });
167
- } catch (err) {
168
- console.error("[SSR] Render failed:", err instanceof Error ? err.message : err);
169
- return new Response("Internal Server Error", {
170
- status: 500,
171
- headers: { "Content-Type": "text/plain" }
172
- });
173
- }
174
- }
175
- async function handleHTMLRequest(module, template, url, ssrTimeout, nonce, fallbackMetrics, linkHeader, staticModulepreloadTags, routeChunkManifest, cacheControl, sessionScript, ssrAuth, manifest) {
176
- try {
177
- const result = await ssrRenderSinglePass(module, url, {
178
- ssrTimeout,
179
- fallbackMetrics,
180
- ssrAuth,
181
- manifest
182
- });
183
- if (result.redirect) {
184
- return new Response(null, {
185
- status: 302,
186
- headers: { Location: result.redirect.to }
187
- });
188
- }
189
- const modulepreloadTags = resolveRouteModulepreload(routeChunkManifest, result.matchedRoutePatterns, staticModulepreloadTags);
190
- const allHeadTags = [result.headTags, modulepreloadTags].filter(Boolean).join(`
191
- `);
192
- const html = injectIntoTemplate({
193
- template,
194
- appHtml: result.html,
195
- appCss: result.css,
196
- ssrData: result.ssrData,
197
- nonce,
198
- headTags: allHeadTags || undefined,
199
- sessionScript
200
- });
201
- const headers = { "Content-Type": "text/html; charset=utf-8" };
202
- if (linkHeader)
203
- headers.Link = linkHeader;
204
- if (cacheControl)
205
- headers["Cache-Control"] = cacheControl;
206
- return new Response(html, { status: 200, headers });
207
- } catch (err) {
208
- console.error("[SSR] Render failed:", err instanceof Error ? err.message : err);
209
- return new Response("Internal Server Error", {
210
- status: 500,
211
- headers: { "Content-Type": "text/plain" }
212
- });
213
- }
214
- }
215
-
216
- export { createSSRHandler };