@alepha/react 0.7.6 → 0.7.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/dist/index.browser.d.ts +26 -81
- package/dist/index.browser.js +11 -61
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +53 -131
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -95
- package/dist/index.d.ts +54 -103
- package/dist/index.js +52 -130
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
- package/src/descriptors/$page.ts +2 -47
- package/src/hooks/useClient.ts +3 -3
- package/src/index.ts +16 -4
- package/src/providers/BrowserRouterProvider.ts +3 -4
- package/src/providers/PageDescriptorProvider.ts +10 -57
- package/src/providers/ReactBrowserProvider.ts +3 -14
- package/src/providers/ReactServerProvider.ts +66 -58
- package/src/providers/BrowserHeadProvider.ts +0 -43
- package/src/providers/ServerHeadProvider.ts +0 -91
package/src/index.ts
CHANGED
|
@@ -22,21 +22,33 @@ export * from "./providers/ReactServerProvider.ts";
|
|
|
22
22
|
|
|
23
23
|
declare module "@alepha/core" {
|
|
24
24
|
interface Hooks {
|
|
25
|
+
"react:router:createLayers": {
|
|
26
|
+
request: ServerRequest;
|
|
27
|
+
context: PageRequest;
|
|
28
|
+
layers: PageRequest[];
|
|
29
|
+
};
|
|
30
|
+
"react:server:render:begin": {
|
|
31
|
+
request?: ServerRequest;
|
|
32
|
+
context: PageRequest;
|
|
33
|
+
};
|
|
34
|
+
"react:server:render:end": {
|
|
35
|
+
request?: ServerRequest;
|
|
36
|
+
context: PageRequest;
|
|
37
|
+
state: RouterState;
|
|
38
|
+
html: string;
|
|
39
|
+
};
|
|
25
40
|
"react:browser:render": {
|
|
26
41
|
state: RouterState;
|
|
27
42
|
context: PageReactContext;
|
|
28
43
|
hydration?: ReactHydrationState;
|
|
29
44
|
};
|
|
30
|
-
"react:server:render": {
|
|
31
|
-
request: ServerRequest;
|
|
32
|
-
pageRequest: PageRequest;
|
|
33
|
-
};
|
|
34
45
|
"react:transition:begin": {
|
|
35
46
|
state: RouterState;
|
|
36
47
|
context: PageReactContext;
|
|
37
48
|
};
|
|
38
49
|
"react:transition:success": {
|
|
39
50
|
state: RouterState;
|
|
51
|
+
context: PageReactContext;
|
|
40
52
|
};
|
|
41
53
|
"react:transition:error": {
|
|
42
54
|
error: Error;
|
|
@@ -53,14 +53,13 @@ export class BrowserRouterProvider extends RouterProvider<BrowserRoute> {
|
|
|
53
53
|
layers: [],
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
-
const context
|
|
56
|
+
const context = {
|
|
57
57
|
url,
|
|
58
58
|
query: {},
|
|
59
59
|
params: {},
|
|
60
|
-
head: {},
|
|
61
60
|
onError: () => null,
|
|
62
61
|
...(options.context ?? {}),
|
|
63
|
-
};
|
|
62
|
+
} as PageRequest;
|
|
64
63
|
|
|
65
64
|
await this.alepha.emit("react:transition:begin", { state, context });
|
|
66
65
|
|
|
@@ -105,7 +104,7 @@ export class BrowserRouterProvider extends RouterProvider<BrowserRoute> {
|
|
|
105
104
|
});
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
await this.alepha.emit("react:transition:success", { state });
|
|
107
|
+
await this.alepha.emit("react:transition:success", { state, context });
|
|
109
108
|
} catch (e) {
|
|
110
109
|
this.log.error(e);
|
|
111
110
|
state.layers = [
|
|
@@ -8,11 +8,7 @@ import NestedView from "../components/NestedView.tsx";
|
|
|
8
8
|
import NotFoundPage from "../components/NotFound.tsx";
|
|
9
9
|
import { RouterContext } from "../contexts/RouterContext.ts";
|
|
10
10
|
import { RouterLayerContext } from "../contexts/RouterLayerContext.ts";
|
|
11
|
-
import {
|
|
12
|
-
$page,
|
|
13
|
-
type Head,
|
|
14
|
-
type PageDescriptorOptions,
|
|
15
|
-
} from "../descriptors/$page.ts";
|
|
11
|
+
import { $page, type PageDescriptorOptions } from "../descriptors/$page.ts";
|
|
16
12
|
import { RedirectionError } from "../errors/RedirectionError.ts";
|
|
17
13
|
|
|
18
14
|
const envSchema = t.object({
|
|
@@ -213,12 +209,12 @@ export class PageDescriptorProvider {
|
|
|
213
209
|
params[key] = String(params[key]);
|
|
214
210
|
}
|
|
215
211
|
|
|
216
|
-
if (it.route.head && !it.error) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
212
|
+
// if (it.route.head && !it.error) {
|
|
213
|
+
// this.fillHead(it.route, request, {
|
|
214
|
+
// ...props,
|
|
215
|
+
// ...context,
|
|
216
|
+
// });
|
|
217
|
+
// }
|
|
222
218
|
|
|
223
219
|
acc += "/";
|
|
224
220
|
acc += it.route.path ? this.compile(it.route.path, params) : "";
|
|
@@ -244,6 +240,7 @@ export class PageDescriptorProvider {
|
|
|
244
240
|
element: this.renderView(i + 1, path, element, it.route),
|
|
245
241
|
index: i + 1,
|
|
246
242
|
path,
|
|
243
|
+
route,
|
|
247
244
|
});
|
|
248
245
|
break;
|
|
249
246
|
}
|
|
@@ -263,6 +260,7 @@ export class PageDescriptorProvider {
|
|
|
263
260
|
element: this.renderView(i + 1, path, element, it.route),
|
|
264
261
|
index: i + 1,
|
|
265
262
|
path,
|
|
263
|
+
route,
|
|
266
264
|
});
|
|
267
265
|
}
|
|
268
266
|
|
|
@@ -294,51 +292,6 @@ export class PageDescriptorProvider {
|
|
|
294
292
|
return undefined;
|
|
295
293
|
}
|
|
296
294
|
|
|
297
|
-
protected fillHead(
|
|
298
|
-
page: PageRoute,
|
|
299
|
-
ctx: PageRequest,
|
|
300
|
-
props: Record<string, any>,
|
|
301
|
-
): void {
|
|
302
|
-
if (!page.head) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
ctx.head ??= {};
|
|
307
|
-
|
|
308
|
-
const head =
|
|
309
|
-
typeof page.head === "function" ? page.head(props, ctx.head) : page.head;
|
|
310
|
-
|
|
311
|
-
if (head.title) {
|
|
312
|
-
ctx.head ??= {};
|
|
313
|
-
|
|
314
|
-
if (ctx.head.titleSeparator) {
|
|
315
|
-
ctx.head.title = `${head.title}${ctx.head.titleSeparator}${ctx.head.title}`;
|
|
316
|
-
} else {
|
|
317
|
-
ctx.head.title = head.title;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
ctx.head.titleSeparator = head.titleSeparator;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (head.htmlAttributes) {
|
|
324
|
-
ctx.head.htmlAttributes = {
|
|
325
|
-
...ctx.head.htmlAttributes,
|
|
326
|
-
...head.htmlAttributes,
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (head.bodyAttributes) {
|
|
331
|
-
ctx.head.bodyAttributes = {
|
|
332
|
-
...ctx.head.bodyAttributes,
|
|
333
|
-
...head.bodyAttributes,
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (head.meta) {
|
|
338
|
-
ctx.head.meta = [...(ctx.head.meta ?? []), ...(head.meta ?? [])];
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
295
|
public renderError(error: Error): ReactNode {
|
|
343
296
|
return createElement(ErrorViewer, { error });
|
|
344
297
|
}
|
|
@@ -534,6 +487,7 @@ export interface Layer {
|
|
|
534
487
|
element: ReactNode;
|
|
535
488
|
index: number;
|
|
536
489
|
path: string;
|
|
490
|
+
route?: PageRoute;
|
|
537
491
|
}
|
|
538
492
|
|
|
539
493
|
export type PreviousLayerData = Omit<Layer, "element" | "index" | "path">;
|
|
@@ -586,7 +540,6 @@ export interface CreateLayersResult extends RouterState {
|
|
|
586
540
|
*/
|
|
587
541
|
export interface PageReactContext {
|
|
588
542
|
url: URL;
|
|
589
|
-
head: Head;
|
|
590
543
|
onError: (error: Error) => ReactNode;
|
|
591
544
|
links?: ApiLinksResponse;
|
|
592
545
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { $hook, $inject, $logger, Alepha } from "@alepha/core";
|
|
2
|
-
import {
|
|
2
|
+
import type { ApiLinksResponse } from "@alepha/server";
|
|
3
|
+
import { LinkProvider } from "@alepha/server-links";
|
|
3
4
|
import type { Root } from "react-dom/client";
|
|
4
|
-
import { BrowserHeadProvider } from "./BrowserHeadProvider.ts";
|
|
5
5
|
import { BrowserRouterProvider } from "./BrowserRouterProvider.ts";
|
|
6
6
|
import type {
|
|
7
7
|
PreviousLayerData,
|
|
@@ -12,10 +12,9 @@ import type {
|
|
|
12
12
|
|
|
13
13
|
export class ReactBrowserProvider {
|
|
14
14
|
protected readonly log = $logger();
|
|
15
|
-
protected readonly client = $inject(
|
|
15
|
+
protected readonly client = $inject(LinkProvider);
|
|
16
16
|
protected readonly alepha = $inject(Alepha);
|
|
17
17
|
protected readonly router = $inject(BrowserRouterProvider);
|
|
18
|
-
protected readonly headProvider = $inject(BrowserHeadProvider);
|
|
19
18
|
protected root!: Root;
|
|
20
19
|
|
|
21
20
|
public transitioning?: {
|
|
@@ -138,9 +137,6 @@ export class ReactBrowserProvider {
|
|
|
138
137
|
}
|
|
139
138
|
|
|
140
139
|
const { context } = await this.render({ previous });
|
|
141
|
-
if (context.head) {
|
|
142
|
-
this.headProvider.renderHead(this.document, context.head);
|
|
143
|
-
}
|
|
144
140
|
|
|
145
141
|
await this.alepha.emit("react:browser:render", {
|
|
146
142
|
state: this.state,
|
|
@@ -153,13 +149,6 @@ export class ReactBrowserProvider {
|
|
|
153
149
|
});
|
|
154
150
|
},
|
|
155
151
|
});
|
|
156
|
-
|
|
157
|
-
public readonly onTransitionEnd = $hook({
|
|
158
|
-
name: "react:transition:end",
|
|
159
|
-
handler: async ({ context }) => {
|
|
160
|
-
this.headProvider.renderHead(this.document, context.head);
|
|
161
|
-
},
|
|
162
|
-
});
|
|
163
152
|
}
|
|
164
153
|
|
|
165
154
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
@@ -12,13 +12,16 @@ import {
|
|
|
12
12
|
import {
|
|
13
13
|
apiLinksResponseSchema,
|
|
14
14
|
type ServerHandler,
|
|
15
|
-
ServerLinksProvider,
|
|
16
15
|
ServerRouterProvider,
|
|
17
16
|
ServerTimingProvider,
|
|
18
17
|
} from "@alepha/server";
|
|
18
|
+
import { ServerLinksProvider } from "@alepha/server-links";
|
|
19
19
|
import { ServerStaticProvider } from "@alepha/server-static";
|
|
20
20
|
import { renderToString } from "react-dom/server";
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
$page,
|
|
23
|
+
type PageDescriptorRenderOptions,
|
|
24
|
+
} from "../descriptors/$page.ts";
|
|
22
25
|
import {
|
|
23
26
|
PageDescriptorProvider,
|
|
24
27
|
type PageReactContext,
|
|
@@ -27,7 +30,6 @@ import {
|
|
|
27
30
|
type RouterState,
|
|
28
31
|
} from "./PageDescriptorProvider.ts";
|
|
29
32
|
import type { ReactHydrationState } from "./ReactBrowserProvider.ts";
|
|
30
|
-
import { ServerHeadProvider } from "./ServerHeadProvider.ts";
|
|
31
33
|
|
|
32
34
|
const envSchema = t.object({
|
|
33
35
|
REACT_SERVER_DIST: t.string({ default: "public" }),
|
|
@@ -50,7 +52,6 @@ export class ReactServerProvider {
|
|
|
50
52
|
protected readonly pageDescriptorProvider = $inject(PageDescriptorProvider);
|
|
51
53
|
protected readonly serverStaticProvider = $inject(ServerStaticProvider);
|
|
52
54
|
protected readonly serverRouterProvider = $inject(ServerRouterProvider);
|
|
53
|
-
protected readonly headProvider = $inject(ServerHeadProvider);
|
|
54
55
|
protected readonly serverTimingProvider = $inject(ServerTimingProvider);
|
|
55
56
|
protected readonly env = $inject(envSchema);
|
|
56
57
|
protected readonly ROOT_DIV_REGEX = new RegExp(
|
|
@@ -71,11 +72,7 @@ export class ReactServerProvider {
|
|
|
71
72
|
for (const { key, instance, value } of pages) {
|
|
72
73
|
const name = value[OPTIONS].name ?? key;
|
|
73
74
|
|
|
74
|
-
instance[key].
|
|
75
|
-
|
|
76
|
-
if (this.alepha.isTest()) {
|
|
77
|
-
instance[key].render = this.createRenderFunction(name);
|
|
78
|
-
}
|
|
75
|
+
instance[key].render = this.createRenderFunction(name);
|
|
79
76
|
}
|
|
80
77
|
|
|
81
78
|
// development mode
|
|
@@ -129,7 +126,10 @@ export class ReactServerProvider {
|
|
|
129
126
|
});
|
|
130
127
|
|
|
131
128
|
public get template() {
|
|
132
|
-
return
|
|
129
|
+
return (
|
|
130
|
+
this.alepha.state("ReactServerProvider.template") ??
|
|
131
|
+
"<!DOCTYPE html><html lang='en'><head></head><body></body></html>"
|
|
132
|
+
);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
protected async registerPages(templateLoader: TemplateLoader) {
|
|
@@ -193,12 +193,7 @@ export class ReactServerProvider {
|
|
|
193
193
|
* For testing purposes, creates a render function that can be used.
|
|
194
194
|
*/
|
|
195
195
|
protected createRenderFunction(name: string, withIndex = false) {
|
|
196
|
-
return async (
|
|
197
|
-
options: {
|
|
198
|
-
params?: Record<string, string>;
|
|
199
|
-
query?: Record<string, string>;
|
|
200
|
-
} = {},
|
|
201
|
-
) => {
|
|
196
|
+
return async (options: PageDescriptorRenderOptions = {}) => {
|
|
202
197
|
const page = this.pageDescriptorProvider.page(name);
|
|
203
198
|
const url = new URL(this.pageDescriptorProvider.url(name, options));
|
|
204
199
|
const context: PageRequest = {
|
|
@@ -209,12 +204,16 @@ export class ReactServerProvider {
|
|
|
209
204
|
onError: () => null,
|
|
210
205
|
};
|
|
211
206
|
|
|
207
|
+
await this.alepha.emit("react:server:render:begin", {
|
|
208
|
+
context,
|
|
209
|
+
});
|
|
210
|
+
|
|
212
211
|
const state = await this.pageDescriptorProvider.createLayers(
|
|
213
212
|
page,
|
|
214
213
|
context,
|
|
215
214
|
);
|
|
216
215
|
|
|
217
|
-
if (!withIndex) {
|
|
216
|
+
if (!withIndex && !options.html) {
|
|
218
217
|
return {
|
|
219
218
|
context,
|
|
220
219
|
html: renderToString(
|
|
@@ -223,10 +222,22 @@ export class ReactServerProvider {
|
|
|
223
222
|
};
|
|
224
223
|
}
|
|
225
224
|
|
|
226
|
-
|
|
225
|
+
const html = this.renderToHtml(
|
|
226
|
+
this.template ?? "",
|
|
227
|
+
state,
|
|
227
228
|
context,
|
|
228
|
-
|
|
229
|
+
options.hydration,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const result = {
|
|
233
|
+
context,
|
|
234
|
+
state,
|
|
235
|
+
html,
|
|
229
236
|
};
|
|
237
|
+
|
|
238
|
+
await this.alepha.emit("react:server:render:end", result);
|
|
239
|
+
|
|
240
|
+
return result;
|
|
230
241
|
};
|
|
231
242
|
}
|
|
232
243
|
|
|
@@ -287,16 +298,10 @@ export class ReactServerProvider {
|
|
|
287
298
|
// return;
|
|
288
299
|
// }
|
|
289
300
|
|
|
290
|
-
await this.alepha.emit(
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
pageRequest: context,
|
|
295
|
-
},
|
|
296
|
-
{
|
|
297
|
-
log: false,
|
|
298
|
-
},
|
|
299
|
-
);
|
|
301
|
+
await this.alepha.emit("react:server:render:begin", {
|
|
302
|
+
request: serverRequest,
|
|
303
|
+
context,
|
|
304
|
+
});
|
|
300
305
|
|
|
301
306
|
this.serverTimingProvider.beginTiming("createLayers");
|
|
302
307
|
|
|
@@ -327,6 +332,13 @@ export class ReactServerProvider {
|
|
|
327
332
|
|
|
328
333
|
const html = this.renderToHtml(template, state, context);
|
|
329
334
|
|
|
335
|
+
await this.alepha.emit("react:server:render:end", {
|
|
336
|
+
request: serverRequest,
|
|
337
|
+
context,
|
|
338
|
+
state,
|
|
339
|
+
html,
|
|
340
|
+
});
|
|
341
|
+
|
|
330
342
|
page.afterHandler?.(serverRequest);
|
|
331
343
|
|
|
332
344
|
return html;
|
|
@@ -337,6 +349,7 @@ export class ReactServerProvider {
|
|
|
337
349
|
template: string,
|
|
338
350
|
state: RouterState,
|
|
339
351
|
context: PageReactContext,
|
|
352
|
+
hydration = true,
|
|
340
353
|
) {
|
|
341
354
|
const element = this.pageDescriptorProvider.root(state, context);
|
|
342
355
|
|
|
@@ -352,41 +365,36 @@ export class ReactServerProvider {
|
|
|
352
365
|
|
|
353
366
|
this.serverTimingProvider.endTiming("renderToString");
|
|
354
367
|
|
|
355
|
-
const hydrationData: ReactHydrationState = {
|
|
356
|
-
links: context.links,
|
|
357
|
-
layers: state.layers.map((it) => ({
|
|
358
|
-
...it,
|
|
359
|
-
error: it.error
|
|
360
|
-
? {
|
|
361
|
-
...it.error,
|
|
362
|
-
name: it.error.name,
|
|
363
|
-
message: it.error.message,
|
|
364
|
-
stack: it.error.stack, // TODO: Hide stack in production ?
|
|
365
|
-
}
|
|
366
|
-
: undefined,
|
|
367
|
-
index: undefined,
|
|
368
|
-
path: undefined,
|
|
369
|
-
element: undefined,
|
|
370
|
-
})),
|
|
371
|
-
};
|
|
372
|
-
|
|
373
|
-
// create hydration data
|
|
374
|
-
const script = `<script>window.__ssr=${JSON.stringify(hydrationData)}</script>`;
|
|
375
|
-
|
|
376
368
|
const response = {
|
|
377
369
|
html: template,
|
|
378
370
|
};
|
|
379
371
|
|
|
380
|
-
|
|
381
|
-
|
|
372
|
+
if (hydration) {
|
|
373
|
+
const hydrationData: ReactHydrationState = {
|
|
374
|
+
links: context.links,
|
|
375
|
+
layers: state.layers.map((it) => ({
|
|
376
|
+
...it,
|
|
377
|
+
error: it.error
|
|
378
|
+
? {
|
|
379
|
+
...it.error,
|
|
380
|
+
name: it.error.name,
|
|
381
|
+
message: it.error.message,
|
|
382
|
+
stack: it.error.stack, // TODO: Hide stack in production ?
|
|
383
|
+
}
|
|
384
|
+
: undefined,
|
|
385
|
+
index: undefined,
|
|
386
|
+
path: undefined,
|
|
387
|
+
element: undefined,
|
|
388
|
+
route: undefined,
|
|
389
|
+
})),
|
|
390
|
+
};
|
|
382
391
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
response.html = this.headProvider.renderHead(response.html, context.head);
|
|
386
|
-
}
|
|
392
|
+
// create hydration data
|
|
393
|
+
const script = `<script>window.__ssr=${JSON.stringify(hydrationData)}</script>`;
|
|
387
394
|
|
|
388
|
-
|
|
389
|
-
|
|
395
|
+
// inject app into template
|
|
396
|
+
this.fillTemplate(response, app, script);
|
|
397
|
+
}
|
|
390
398
|
|
|
391
399
|
return response.html;
|
|
392
400
|
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { Head } from "./ServerHeadProvider.ts";
|
|
2
|
-
|
|
3
|
-
export class BrowserHeadProvider {
|
|
4
|
-
renderHead(document: Document, head: Head): void {
|
|
5
|
-
if (head.title) {
|
|
6
|
-
document.title = head.title;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
if (head.bodyAttributes) {
|
|
10
|
-
for (const [key, value] of Object.entries(head.bodyAttributes)) {
|
|
11
|
-
if (value) {
|
|
12
|
-
document.body.setAttribute(key, value);
|
|
13
|
-
} else {
|
|
14
|
-
document.body.removeAttribute(key);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (head.htmlAttributes) {
|
|
20
|
-
for (const [key, value] of Object.entries(head.htmlAttributes)) {
|
|
21
|
-
if (value) {
|
|
22
|
-
document.documentElement.setAttribute(key, value);
|
|
23
|
-
} else {
|
|
24
|
-
document.documentElement.removeAttribute(key);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (head.meta) {
|
|
30
|
-
for (const [key, value] of Object.entries(head.meta)) {
|
|
31
|
-
const meta = document.querySelector(`meta[name="${key}"]`);
|
|
32
|
-
if (meta) {
|
|
33
|
-
meta.setAttribute("content", value.content);
|
|
34
|
-
} else {
|
|
35
|
-
const newMeta = document.createElement("meta");
|
|
36
|
-
newMeta.setAttribute("name", key);
|
|
37
|
-
newMeta.setAttribute("content", value.content);
|
|
38
|
-
document.head.appendChild(newMeta);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
export interface Head {
|
|
2
|
-
title?: string;
|
|
3
|
-
htmlAttributes?: Record<string, string>;
|
|
4
|
-
bodyAttributes?: Record<string, string>;
|
|
5
|
-
meta?: Array<{ name: string; content: string }>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class ServerHeadProvider {
|
|
9
|
-
renderHead(template: string, head: Head): string {
|
|
10
|
-
let result = template;
|
|
11
|
-
|
|
12
|
-
// Inject htmlAttributes
|
|
13
|
-
const htmlAttributes = head.htmlAttributes;
|
|
14
|
-
if (htmlAttributes) {
|
|
15
|
-
result = result.replace(
|
|
16
|
-
/<html([^>]*)>/i,
|
|
17
|
-
(_, existingAttrs) =>
|
|
18
|
-
`<html${this.mergeAttributes(existingAttrs, htmlAttributes)}>`,
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Inject bodyAttributes
|
|
23
|
-
const bodyAttributes = head.bodyAttributes;
|
|
24
|
-
if (bodyAttributes) {
|
|
25
|
-
result = result.replace(
|
|
26
|
-
/<body([^>]*)>/i,
|
|
27
|
-
(_, existingAttrs) =>
|
|
28
|
-
`<body${this.mergeAttributes(existingAttrs, bodyAttributes)}>`,
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Build head content
|
|
33
|
-
let headContent = "";
|
|
34
|
-
const title = head.title;
|
|
35
|
-
if (title) {
|
|
36
|
-
if (template.includes("<title>")) {
|
|
37
|
-
result = result.replace(
|
|
38
|
-
/<title>(.*?)<\/title>/i,
|
|
39
|
-
() => `<title>${this.escapeHtml(title)}</title>`,
|
|
40
|
-
);
|
|
41
|
-
} else {
|
|
42
|
-
headContent += `<title>${this.escapeHtml(title)}</title>\n`;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (head.meta) {
|
|
47
|
-
for (const meta of head.meta) {
|
|
48
|
-
headContent += `<meta name="${this.escapeHtml(meta.name)}" content="${this.escapeHtml(meta.content)}">\n`;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Inject into <head>...</head>
|
|
53
|
-
result = result.replace(
|
|
54
|
-
/<head([^>]*)>(.*?)<\/head>/is,
|
|
55
|
-
(_, existingAttrs, existingHead) =>
|
|
56
|
-
`<head${existingAttrs}>${existingHead}${headContent}</head>`,
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
return result.trim();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
mergeAttributes(existing: string, attrs: Record<string, string>): string {
|
|
63
|
-
const existingAttrs = this.parseAttributes(existing);
|
|
64
|
-
const merged = { ...existingAttrs, ...attrs };
|
|
65
|
-
return Object.entries(merged)
|
|
66
|
-
.map(([k, v]) => ` ${k}="${this.escapeHtml(v)}"`)
|
|
67
|
-
.join("");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
parseAttributes(attrStr: string): Record<string, string> {
|
|
71
|
-
const attrs: Record<string, string> = {};
|
|
72
|
-
const attrRegex = /([^\s=]+)(?:="([^"]*)")?/g;
|
|
73
|
-
let match: RegExpExecArray | null = attrRegex.exec(attrStr);
|
|
74
|
-
|
|
75
|
-
while (match) {
|
|
76
|
-
attrs[match[1]] = match[2] ?? "";
|
|
77
|
-
match = attrRegex.exec(attrStr);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return attrs;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
escapeHtml(str: string): string {
|
|
84
|
-
return str
|
|
85
|
-
.replace(/&/g, "&")
|
|
86
|
-
.replace(/</g, "<")
|
|
87
|
-
.replace(/>/g, ">")
|
|
88
|
-
.replace(/"/g, """)
|
|
89
|
-
.replace(/'/g, "'");
|
|
90
|
-
}
|
|
91
|
-
}
|