@alepha/react 0.7.4 → 0.7.6
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 +578 -0
- package/dist/index.browser.js +1091 -49
- package/dist/index.browser.js.map +1 -0
- package/dist/index.cjs +1333 -571
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +678 -0
- package/dist/index.d.ts +530 -570
- package/dist/index.js +1297 -557
- package/dist/index.js.map +1 -0
- package/package.json +11 -8
- package/src/components/NotFound.tsx +0 -10
- package/src/descriptors/$page.ts +5 -2
- package/src/index.browser.ts +1 -0
- package/src/index.shared.ts +0 -4
- package/src/index.ts +0 -2
- package/dist/ReactBrowserProvider-CXDElhnK.cjs +0 -960
- package/dist/ReactBrowserProvider-ufHSOTmv.js +0 -946
- package/dist/index.browser.cjs +0 -58
package/dist/index.js
CHANGED
|
@@ -1,591 +1,1331 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { AlephaServerCache } from
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import { existsSync } from
|
|
7
|
-
import { join } from
|
|
8
|
-
import { ServerStaticProvider } from
|
|
9
|
-
import { renderToString } from
|
|
10
|
-
import {
|
|
11
|
-
import React, { useContext, useMemo, useState, useEffect } from 'react';
|
|
12
|
-
import '@alepha/router';
|
|
1
|
+
import { $hook, $inject, $logger, Alepha, KIND, NotImplementedError, OPTIONS, __bind, __descriptor, t } from "@alepha/core";
|
|
2
|
+
import { AlephaServer, HttpClient, ServerLinksProvider, ServerRouterProvider, ServerTimingProvider, apiLinksResponseSchema } from "@alepha/server";
|
|
3
|
+
import { AlephaServerCache } from "@alepha/server-cache";
|
|
4
|
+
import React, { StrictMode, createContext, createElement, useContext, useEffect, useMemo, useState } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { ServerStaticProvider } from "@alepha/server-static";
|
|
9
|
+
import { renderToString } from "react-dom/server";
|
|
10
|
+
import { RouterProvider } from "@alepha/router";
|
|
13
11
|
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
12
|
+
//#region src/descriptors/$page.ts
|
|
13
|
+
const KEY = "PAGE";
|
|
14
|
+
/**
|
|
15
|
+
* Main descriptor for defining a React route in the application.
|
|
16
|
+
*/
|
|
17
|
+
const $page = (options) => {
|
|
18
|
+
__descriptor(KEY);
|
|
19
|
+
if (options.children) for (const child of options.children) child[OPTIONS].parent = { [OPTIONS]: options };
|
|
20
|
+
if (options.parent) {
|
|
21
|
+
options.parent[OPTIONS].children ??= [];
|
|
22
|
+
options.parent[OPTIONS].children.push({ [OPTIONS]: options });
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
[KIND]: KEY,
|
|
26
|
+
[OPTIONS]: options,
|
|
27
|
+
render: () => {
|
|
28
|
+
throw new NotImplementedError(KEY);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
$page[KIND] = KEY;
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/components/ClientOnly.tsx
|
|
36
|
+
/**
|
|
37
|
+
* A small utility component that renders its children only on the client side.
|
|
38
|
+
*
|
|
39
|
+
* Optionally, you can provide a fallback React node that will be rendered.
|
|
40
|
+
*
|
|
41
|
+
* You should use this component when
|
|
42
|
+
* - you have code that relies on browser-specific APIs
|
|
43
|
+
* - you want to avoid server-side rendering for a specific part of your application
|
|
44
|
+
* - you want to prevent pre-rendering of a component
|
|
45
|
+
*/
|
|
46
|
+
const ClientOnly = (props) => {
|
|
47
|
+
const [mounted, setMounted] = useState(false);
|
|
48
|
+
useEffect(() => setMounted(true), []);
|
|
49
|
+
if (props.disabled) return props.children;
|
|
50
|
+
return mounted ? props.children : props.fallback;
|
|
51
|
+
};
|
|
52
|
+
var ClientOnly_default = ClientOnly;
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/contexts/RouterContext.ts
|
|
56
|
+
const RouterContext = createContext(void 0);
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/hooks/useAlepha.ts
|
|
60
|
+
const useAlepha = () => {
|
|
61
|
+
const routerContext = useContext(RouterContext);
|
|
62
|
+
if (!routerContext) throw new Error("useAlepha must be used within a RouterProvider");
|
|
63
|
+
return routerContext.alepha;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/components/ErrorViewer.tsx
|
|
68
|
+
const ErrorViewer = ({ error }) => {
|
|
69
|
+
const [expanded, setExpanded] = useState(false);
|
|
70
|
+
const isProduction = useAlepha().isProduction();
|
|
71
|
+
if (isProduction) return /* @__PURE__ */ jsx(ErrorViewerProduction, {});
|
|
72
|
+
const stackLines = error.stack?.split("\n") ?? [];
|
|
73
|
+
const previewLines = stackLines.slice(0, 5);
|
|
74
|
+
const hiddenLineCount = stackLines.length - previewLines.length;
|
|
75
|
+
const copyToClipboard = (text) => {
|
|
76
|
+
navigator.clipboard.writeText(text).catch((err) => {
|
|
77
|
+
console.error("Clipboard error:", err);
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const styles = {
|
|
81
|
+
container: {
|
|
82
|
+
padding: "24px",
|
|
83
|
+
backgroundColor: "#FEF2F2",
|
|
84
|
+
color: "#7F1D1D",
|
|
85
|
+
border: "1px solid #FECACA",
|
|
86
|
+
borderRadius: "16px",
|
|
87
|
+
boxShadow: "0 8px 24px rgba(0,0,0,0.05)",
|
|
88
|
+
fontFamily: "monospace",
|
|
89
|
+
maxWidth: "768px",
|
|
90
|
+
margin: "40px auto"
|
|
91
|
+
},
|
|
92
|
+
heading: {
|
|
93
|
+
fontSize: "20px",
|
|
94
|
+
fontWeight: "bold",
|
|
95
|
+
marginBottom: "4px"
|
|
96
|
+
},
|
|
97
|
+
name: {
|
|
98
|
+
fontSize: "16px",
|
|
99
|
+
fontWeight: 600
|
|
100
|
+
},
|
|
101
|
+
message: {
|
|
102
|
+
fontSize: "14px",
|
|
103
|
+
marginBottom: "16px"
|
|
104
|
+
},
|
|
105
|
+
sectionHeader: {
|
|
106
|
+
display: "flex",
|
|
107
|
+
justifyContent: "space-between",
|
|
108
|
+
alignItems: "center",
|
|
109
|
+
fontSize: "12px",
|
|
110
|
+
marginBottom: "4px",
|
|
111
|
+
color: "#991B1B"
|
|
112
|
+
},
|
|
113
|
+
copyButton: {
|
|
114
|
+
fontSize: "12px",
|
|
115
|
+
color: "#DC2626",
|
|
116
|
+
background: "none",
|
|
117
|
+
border: "none",
|
|
118
|
+
cursor: "pointer",
|
|
119
|
+
textDecoration: "underline"
|
|
120
|
+
},
|
|
121
|
+
stackContainer: {
|
|
122
|
+
backgroundColor: "#FEE2E2",
|
|
123
|
+
padding: "12px",
|
|
124
|
+
borderRadius: "8px",
|
|
125
|
+
fontSize: "13px",
|
|
126
|
+
lineHeight: "1.4",
|
|
127
|
+
overflowX: "auto",
|
|
128
|
+
whiteSpace: "pre-wrap"
|
|
129
|
+
},
|
|
130
|
+
expandLine: {
|
|
131
|
+
color: "#F87171",
|
|
132
|
+
cursor: "pointer",
|
|
133
|
+
marginTop: "8px"
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
137
|
+
style: styles.container,
|
|
138
|
+
children: [/* @__PURE__ */ jsxs("div", { children: [
|
|
139
|
+
/* @__PURE__ */ jsx("div", {
|
|
140
|
+
style: styles.heading,
|
|
141
|
+
children: "🔥 Error"
|
|
142
|
+
}),
|
|
143
|
+
/* @__PURE__ */ jsx("div", {
|
|
144
|
+
style: styles.name,
|
|
145
|
+
children: error.name
|
|
146
|
+
}),
|
|
147
|
+
/* @__PURE__ */ jsx("div", {
|
|
148
|
+
style: styles.message,
|
|
149
|
+
children: error.message
|
|
150
|
+
})
|
|
151
|
+
] }), stackLines.length > 0 && /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsxs("div", {
|
|
152
|
+
style: styles.sectionHeader,
|
|
153
|
+
children: [/* @__PURE__ */ jsx("span", { children: "Stack trace" }), /* @__PURE__ */ jsx("button", {
|
|
154
|
+
onClick: () => copyToClipboard(error.stack),
|
|
155
|
+
style: styles.copyButton,
|
|
156
|
+
children: "Copy all"
|
|
157
|
+
})]
|
|
158
|
+
}), /* @__PURE__ */ jsxs("pre", {
|
|
159
|
+
style: styles.stackContainer,
|
|
160
|
+
children: [(expanded ? stackLines : previewLines).map((line, i) => /* @__PURE__ */ jsx("div", { children: line }, i)), !expanded && hiddenLineCount > 0 && /* @__PURE__ */ jsxs("div", {
|
|
161
|
+
style: styles.expandLine,
|
|
162
|
+
onClick: () => setExpanded(true),
|
|
163
|
+
children: [
|
|
164
|
+
"+ ",
|
|
165
|
+
hiddenLineCount,
|
|
166
|
+
" more lines..."
|
|
167
|
+
]
|
|
168
|
+
})]
|
|
169
|
+
})] })]
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
var ErrorViewer_default = ErrorViewer;
|
|
173
|
+
const ErrorViewerProduction = () => {
|
|
174
|
+
const styles = {
|
|
175
|
+
container: {
|
|
176
|
+
padding: "24px",
|
|
177
|
+
backgroundColor: "#FEF2F2",
|
|
178
|
+
color: "#7F1D1D",
|
|
179
|
+
border: "1px solid #FECACA",
|
|
180
|
+
borderRadius: "16px",
|
|
181
|
+
boxShadow: "0 8px 24px rgba(0,0,0,0.05)",
|
|
182
|
+
fontFamily: "monospace",
|
|
183
|
+
maxWidth: "768px",
|
|
184
|
+
margin: "40px auto",
|
|
185
|
+
textAlign: "center"
|
|
186
|
+
},
|
|
187
|
+
heading: {
|
|
188
|
+
fontSize: "20px",
|
|
189
|
+
fontWeight: "bold",
|
|
190
|
+
marginBottom: "8px"
|
|
191
|
+
},
|
|
192
|
+
name: {
|
|
193
|
+
fontSize: "16px",
|
|
194
|
+
fontWeight: 600,
|
|
195
|
+
marginBottom: "4px"
|
|
196
|
+
},
|
|
197
|
+
message: {
|
|
198
|
+
fontSize: "14px",
|
|
199
|
+
opacity: .85
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
203
|
+
style: styles.container,
|
|
204
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
205
|
+
style: styles.heading,
|
|
206
|
+
children: "🚨 An error occurred"
|
|
207
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
208
|
+
style: styles.message,
|
|
209
|
+
children: "Something went wrong. Please try again later."
|
|
210
|
+
})]
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/contexts/RouterLayerContext.ts
|
|
216
|
+
const RouterLayerContext = createContext(void 0);
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/hooks/useRouterEvents.ts
|
|
220
|
+
const useRouterEvents = (opts = {}, deps = []) => {
|
|
221
|
+
const ctx = useContext(RouterContext);
|
|
222
|
+
if (!ctx) throw new Error("useRouter must be used within a RouterProvider");
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
if (!ctx.alepha.isBrowser()) return;
|
|
225
|
+
const subs = [];
|
|
226
|
+
const onBegin = opts.onBegin;
|
|
227
|
+
const onEnd = opts.onEnd;
|
|
228
|
+
const onError = opts.onError;
|
|
229
|
+
if (onBegin) subs.push(ctx.alepha.on("react:transition:begin", { callback: onBegin }));
|
|
230
|
+
if (onEnd) subs.push(ctx.alepha.on("react:transition:end", { callback: onEnd }));
|
|
231
|
+
if (onError) subs.push(ctx.alepha.on("react:transition:error", { callback: onError }));
|
|
232
|
+
return () => {
|
|
233
|
+
for (const sub of subs) sub();
|
|
234
|
+
};
|
|
235
|
+
}, deps);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/components/ErrorBoundary.tsx
|
|
240
|
+
/**
|
|
241
|
+
* A reusable error boundary for catching rendering errors
|
|
242
|
+
* in any part of the React component tree.
|
|
243
|
+
*/
|
|
244
|
+
var ErrorBoundary = class extends React.Component {
|
|
245
|
+
constructor(props) {
|
|
246
|
+
super(props);
|
|
247
|
+
this.state = {};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Update state so the next render shows the fallback UI.
|
|
251
|
+
*/
|
|
252
|
+
static getDerivedStateFromError(error) {
|
|
253
|
+
return { error };
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Lifecycle method called when an error is caught.
|
|
257
|
+
* You can log the error or perform side effects here.
|
|
258
|
+
*/
|
|
259
|
+
componentDidCatch(error, info) {
|
|
260
|
+
if (this.props.onError) this.props.onError(error, info);
|
|
261
|
+
}
|
|
262
|
+
render() {
|
|
263
|
+
if (this.state.error) return this.props.fallback(this.state.error);
|
|
264
|
+
return this.props.children;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
var ErrorBoundary_default = ErrorBoundary;
|
|
268
|
+
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/components/NestedView.tsx
|
|
271
|
+
/**
|
|
272
|
+
* A component that renders the current view of the nested router layer.
|
|
273
|
+
*
|
|
274
|
+
* To be simple, it renders the `element` of the current child page of a parent page.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```tsx
|
|
278
|
+
* import { NestedView } from "@alepha/react";
|
|
279
|
+
*
|
|
280
|
+
* class App {
|
|
281
|
+
* parent = $page({
|
|
282
|
+
* component: () => <NestedView />,
|
|
283
|
+
* });
|
|
284
|
+
*
|
|
285
|
+
* child = $page({
|
|
286
|
+
* parent: this.root,
|
|
287
|
+
* component: () => <div>Child Page</div>,
|
|
288
|
+
* });
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
const NestedView = (props) => {
|
|
293
|
+
const app = useContext(RouterContext);
|
|
294
|
+
const layer = useContext(RouterLayerContext);
|
|
295
|
+
const index = layer?.index ?? 0;
|
|
296
|
+
const [view, setView] = useState(app?.state.layers[index]?.element);
|
|
297
|
+
useRouterEvents({ onEnd: ({ state }) => {
|
|
298
|
+
setView(state.layers[index]?.element);
|
|
299
|
+
} }, [app]);
|
|
300
|
+
if (!app) throw new Error("NestedView must be used within a RouterContext.");
|
|
301
|
+
const element = view ?? props.children ?? null;
|
|
302
|
+
return /* @__PURE__ */ jsx(ErrorBoundary_default, {
|
|
303
|
+
fallback: app.context.onError,
|
|
304
|
+
children: element
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
var NestedView_default = NestedView;
|
|
308
|
+
|
|
309
|
+
//#endregion
|
|
310
|
+
//#region src/components/NotFound.tsx
|
|
311
|
+
function NotFoundPage() {
|
|
312
|
+
return /* @__PURE__ */ jsx("div", {
|
|
313
|
+
style: {
|
|
314
|
+
height: "100vh",
|
|
315
|
+
display: "flex",
|
|
316
|
+
flexDirection: "column",
|
|
317
|
+
justifyContent: "center",
|
|
318
|
+
alignItems: "center",
|
|
319
|
+
textAlign: "center",
|
|
320
|
+
fontFamily: "sans-serif",
|
|
321
|
+
padding: "1rem"
|
|
322
|
+
},
|
|
323
|
+
children: /* @__PURE__ */ jsx("h1", {
|
|
324
|
+
style: {
|
|
325
|
+
fontSize: "1rem",
|
|
326
|
+
marginBottom: "0.5rem"
|
|
327
|
+
},
|
|
328
|
+
children: "This page does not exist"
|
|
329
|
+
})
|
|
330
|
+
});
|
|
74
331
|
}
|
|
75
332
|
|
|
333
|
+
//#endregion
|
|
334
|
+
//#region src/errors/RedirectionError.ts
|
|
335
|
+
var RedirectionError = class extends Error {
|
|
336
|
+
page;
|
|
337
|
+
constructor(page) {
|
|
338
|
+
super("Redirection");
|
|
339
|
+
this.page = page;
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
//#endregion
|
|
344
|
+
//#region src/providers/PageDescriptorProvider.ts
|
|
345
|
+
const envSchema$1 = t.object({ REACT_STRICT_MODE: t.boolean({ default: true }) });
|
|
346
|
+
var PageDescriptorProvider = class {
|
|
347
|
+
log = $logger();
|
|
348
|
+
env = $inject(envSchema$1);
|
|
349
|
+
alepha = $inject(Alepha);
|
|
350
|
+
pages = [];
|
|
351
|
+
getPages() {
|
|
352
|
+
return this.pages;
|
|
353
|
+
}
|
|
354
|
+
page(name) {
|
|
355
|
+
for (const page of this.pages) if (page.name === name) return page;
|
|
356
|
+
throw new Error(`Page ${name} not found`);
|
|
357
|
+
}
|
|
358
|
+
url(name, options = {}) {
|
|
359
|
+
const page = this.page(name);
|
|
360
|
+
if (!page) throw new Error(`Page ${name} not found`);
|
|
361
|
+
let url = page.path ?? "";
|
|
362
|
+
let parent = page.parent;
|
|
363
|
+
while (parent) {
|
|
364
|
+
url = `${parent.path ?? ""}/${url}`;
|
|
365
|
+
parent = parent.parent;
|
|
366
|
+
}
|
|
367
|
+
url = this.compile(url, options.params ?? {});
|
|
368
|
+
return new URL(url.replace(/\/\/+/g, "/") || "/", options.base ?? `http://localhost`);
|
|
369
|
+
}
|
|
370
|
+
root(state, context) {
|
|
371
|
+
const root = createElement(RouterContext.Provider, { value: {
|
|
372
|
+
alepha: this.alepha,
|
|
373
|
+
state,
|
|
374
|
+
context
|
|
375
|
+
} }, createElement(NestedView_default, {}, state.layers[0]?.element));
|
|
376
|
+
if (this.env.REACT_STRICT_MODE) return createElement(StrictMode, {}, root);
|
|
377
|
+
return root;
|
|
378
|
+
}
|
|
379
|
+
async createLayers(route, request) {
|
|
380
|
+
const { pathname, search } = request.url;
|
|
381
|
+
const layers = [];
|
|
382
|
+
let context = {};
|
|
383
|
+
const stack = [{ route }];
|
|
384
|
+
request.onError = (error) => this.renderError(error);
|
|
385
|
+
let parent = route.parent;
|
|
386
|
+
while (parent) {
|
|
387
|
+
stack.unshift({ route: parent });
|
|
388
|
+
parent = parent.parent;
|
|
389
|
+
}
|
|
390
|
+
let forceRefresh = false;
|
|
391
|
+
for (let i = 0; i < stack.length; i++) {
|
|
392
|
+
const it = stack[i];
|
|
393
|
+
const route$1 = it.route;
|
|
394
|
+
const config = {};
|
|
395
|
+
try {
|
|
396
|
+
config.query = route$1.schema?.query ? this.alepha.parse(route$1.schema.query, request.query) : request.query;
|
|
397
|
+
} catch (e) {
|
|
398
|
+
it.error = e;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
config.params = route$1.schema?.params ? this.alepha.parse(route$1.schema.params, request.params) : request.params;
|
|
403
|
+
} catch (e) {
|
|
404
|
+
it.error = e;
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
it.config = { ...config };
|
|
408
|
+
if (!route$1.resolve) continue;
|
|
409
|
+
const previous = request.previous;
|
|
410
|
+
if (previous?.[i] && !forceRefresh && previous[i].name === route$1.name) {
|
|
411
|
+
const url = (str) => str ? str.replace(/\/\/+/g, "/") : "/";
|
|
412
|
+
const prev = JSON.stringify({
|
|
413
|
+
part: url(previous[i].part),
|
|
414
|
+
params: previous[i].config?.params ?? {}
|
|
415
|
+
});
|
|
416
|
+
const curr = JSON.stringify({
|
|
417
|
+
part: url(route$1.path),
|
|
418
|
+
params: config.params ?? {}
|
|
419
|
+
});
|
|
420
|
+
if (prev === curr) {
|
|
421
|
+
it.props = previous[i].props;
|
|
422
|
+
it.error = previous[i].error;
|
|
423
|
+
context = {
|
|
424
|
+
...context,
|
|
425
|
+
...it.props
|
|
426
|
+
};
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
forceRefresh = true;
|
|
430
|
+
}
|
|
431
|
+
try {
|
|
432
|
+
const props = await route$1.resolve?.({
|
|
433
|
+
...request,
|
|
434
|
+
...config,
|
|
435
|
+
...context
|
|
436
|
+
}) ?? {};
|
|
437
|
+
it.props = { ...props };
|
|
438
|
+
context = {
|
|
439
|
+
...context,
|
|
440
|
+
...props
|
|
441
|
+
};
|
|
442
|
+
} catch (e) {
|
|
443
|
+
if (e instanceof RedirectionError) return {
|
|
444
|
+
layers: [],
|
|
445
|
+
redirect: typeof e.page === "string" ? e.page : this.href(e.page),
|
|
446
|
+
pathname,
|
|
447
|
+
search
|
|
448
|
+
};
|
|
449
|
+
this.log.error(e);
|
|
450
|
+
it.error = e;
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
let acc = "";
|
|
455
|
+
for (let i = 0; i < stack.length; i++) {
|
|
456
|
+
const it = stack[i];
|
|
457
|
+
const props = it.props ?? {};
|
|
458
|
+
const params = { ...it.config?.params };
|
|
459
|
+
for (const key of Object.keys(params)) params[key] = String(params[key]);
|
|
460
|
+
if (it.route.head && !it.error) this.fillHead(it.route, request, {
|
|
461
|
+
...props,
|
|
462
|
+
...context
|
|
463
|
+
});
|
|
464
|
+
acc += "/";
|
|
465
|
+
acc += it.route.path ? this.compile(it.route.path, params) : "";
|
|
466
|
+
const path = acc.replace(/\/+/, "/");
|
|
467
|
+
const localErrorHandler = this.getErrorHandler(it.route);
|
|
468
|
+
if (localErrorHandler) request.onError = localErrorHandler;
|
|
469
|
+
if (it.error) {
|
|
470
|
+
let element$1 = await request.onError(it.error);
|
|
471
|
+
if (element$1 === null) element$1 = this.renderError(it.error);
|
|
472
|
+
layers.push({
|
|
473
|
+
props,
|
|
474
|
+
error: it.error,
|
|
475
|
+
name: it.route.name,
|
|
476
|
+
part: it.route.path,
|
|
477
|
+
config: it.config,
|
|
478
|
+
element: this.renderView(i + 1, path, element$1, it.route),
|
|
479
|
+
index: i + 1,
|
|
480
|
+
path
|
|
481
|
+
});
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
const element = await this.createElement(it.route, {
|
|
485
|
+
...props,
|
|
486
|
+
...context
|
|
487
|
+
});
|
|
488
|
+
layers.push({
|
|
489
|
+
name: it.route.name,
|
|
490
|
+
props,
|
|
491
|
+
part: it.route.path,
|
|
492
|
+
config: it.config,
|
|
493
|
+
element: this.renderView(i + 1, path, element, it.route),
|
|
494
|
+
index: i + 1,
|
|
495
|
+
path
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
return {
|
|
499
|
+
layers,
|
|
500
|
+
pathname,
|
|
501
|
+
search
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
getErrorHandler(route) {
|
|
505
|
+
if (route.errorHandler) return route.errorHandler;
|
|
506
|
+
let parent = route.parent;
|
|
507
|
+
while (parent) {
|
|
508
|
+
if (parent.errorHandler) return parent.errorHandler;
|
|
509
|
+
parent = parent.parent;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
async createElement(page, props) {
|
|
513
|
+
if (page.lazy) {
|
|
514
|
+
const component = await page.lazy();
|
|
515
|
+
return createElement(component.default, props);
|
|
516
|
+
}
|
|
517
|
+
if (page.component) return createElement(page.component, props);
|
|
518
|
+
return void 0;
|
|
519
|
+
}
|
|
520
|
+
fillHead(page, ctx, props) {
|
|
521
|
+
if (!page.head) return;
|
|
522
|
+
ctx.head ??= {};
|
|
523
|
+
const head = typeof page.head === "function" ? page.head(props, ctx.head) : page.head;
|
|
524
|
+
if (head.title) {
|
|
525
|
+
ctx.head ??= {};
|
|
526
|
+
if (ctx.head.titleSeparator) ctx.head.title = `${head.title}${ctx.head.titleSeparator}${ctx.head.title}`;
|
|
527
|
+
else ctx.head.title = head.title;
|
|
528
|
+
ctx.head.titleSeparator = head.titleSeparator;
|
|
529
|
+
}
|
|
530
|
+
if (head.htmlAttributes) ctx.head.htmlAttributes = {
|
|
531
|
+
...ctx.head.htmlAttributes,
|
|
532
|
+
...head.htmlAttributes
|
|
533
|
+
};
|
|
534
|
+
if (head.bodyAttributes) ctx.head.bodyAttributes = {
|
|
535
|
+
...ctx.head.bodyAttributes,
|
|
536
|
+
...head.bodyAttributes
|
|
537
|
+
};
|
|
538
|
+
if (head.meta) ctx.head.meta = [...ctx.head.meta ?? [], ...head.meta ?? []];
|
|
539
|
+
}
|
|
540
|
+
renderError(error) {
|
|
541
|
+
return createElement(ErrorViewer_default, { error });
|
|
542
|
+
}
|
|
543
|
+
renderEmptyView() {
|
|
544
|
+
return createElement(NestedView_default, {});
|
|
545
|
+
}
|
|
546
|
+
href(page, params = {}) {
|
|
547
|
+
const found = this.pages.find((it) => it.name === page.options.name);
|
|
548
|
+
if (!found) throw new Error(`Page ${page.options.name} not found`);
|
|
549
|
+
let url = found.path ?? "";
|
|
550
|
+
let parent = found.parent;
|
|
551
|
+
while (parent) {
|
|
552
|
+
url = `${parent.path ?? ""}/${url}`;
|
|
553
|
+
parent = parent.parent;
|
|
554
|
+
}
|
|
555
|
+
url = this.compile(url, params);
|
|
556
|
+
return url.replace(/\/\/+/g, "/") || "/";
|
|
557
|
+
}
|
|
558
|
+
compile(path, params = {}) {
|
|
559
|
+
for (const [key, value] of Object.entries(params)) path = path.replace(`:${key}`, value);
|
|
560
|
+
return path;
|
|
561
|
+
}
|
|
562
|
+
renderView(index, path, view, page) {
|
|
563
|
+
view ??= this.renderEmptyView();
|
|
564
|
+
const element = page.client ? createElement(ClientOnly_default, typeof page.client === "object" ? page.client : {}, view) : view;
|
|
565
|
+
return createElement(RouterLayerContext.Provider, { value: {
|
|
566
|
+
index,
|
|
567
|
+
path
|
|
568
|
+
} }, element);
|
|
569
|
+
}
|
|
570
|
+
configure = $hook({
|
|
571
|
+
name: "configure",
|
|
572
|
+
handler: () => {
|
|
573
|
+
let hasNotFoundHandler = false;
|
|
574
|
+
const pages = this.alepha.getDescriptorValues($page);
|
|
575
|
+
for (const { value, key } of pages) value[OPTIONS].name ??= key;
|
|
576
|
+
for (const { value } of pages) {
|
|
577
|
+
if (value[OPTIONS].parent) continue;
|
|
578
|
+
if (value[OPTIONS].path === "/*") hasNotFoundHandler = true;
|
|
579
|
+
this.add(this.map(pages, value));
|
|
580
|
+
}
|
|
581
|
+
if (!hasNotFoundHandler && pages.length > 0) this.add({
|
|
582
|
+
path: "/*",
|
|
583
|
+
name: "notFound",
|
|
584
|
+
cache: true,
|
|
585
|
+
component: NotFoundPage,
|
|
586
|
+
afterHandler: ({ reply }) => {
|
|
587
|
+
reply.status = 404;
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
map(pages, target) {
|
|
593
|
+
const children = target[OPTIONS].children ?? [];
|
|
594
|
+
return {
|
|
595
|
+
...target[OPTIONS],
|
|
596
|
+
parent: void 0,
|
|
597
|
+
children: children.map((it) => this.map(pages, it))
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
add(entry) {
|
|
601
|
+
if (this.alepha.isReady()) throw new Error("Router is already initialized");
|
|
602
|
+
entry.name ??= this.nextId();
|
|
603
|
+
const page = entry;
|
|
604
|
+
page.match = this.createMatch(page);
|
|
605
|
+
this.pages.push(page);
|
|
606
|
+
if (page.children) for (const child of page.children) {
|
|
607
|
+
child.parent = page;
|
|
608
|
+
this.add(child);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
createMatch(page) {
|
|
612
|
+
let url = page.path ?? "/";
|
|
613
|
+
let target = page.parent;
|
|
614
|
+
while (target) {
|
|
615
|
+
url = `${target.path ?? ""}/${url}`;
|
|
616
|
+
target = target.parent;
|
|
617
|
+
}
|
|
618
|
+
let path = url.replace(/\/\/+/g, "/");
|
|
619
|
+
if (path.endsWith("/") && path !== "/") path = path.slice(0, -1);
|
|
620
|
+
return path;
|
|
621
|
+
}
|
|
622
|
+
_next = 0;
|
|
623
|
+
nextId() {
|
|
624
|
+
this._next += 1;
|
|
625
|
+
return `P${this._next}`;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
const isPageRoute = (it) => {
|
|
629
|
+
return it && typeof it === "object" && typeof it.path === "string" && typeof it.page === "object";
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
//#endregion
|
|
633
|
+
//#region src/providers/ServerHeadProvider.ts
|
|
634
|
+
var ServerHeadProvider = class {
|
|
635
|
+
renderHead(template, head) {
|
|
636
|
+
let result = template;
|
|
637
|
+
const htmlAttributes = head.htmlAttributes;
|
|
638
|
+
if (htmlAttributes) result = result.replace(/<html([^>]*)>/i, (_, existingAttrs) => `<html${this.mergeAttributes(existingAttrs, htmlAttributes)}>`);
|
|
639
|
+
const bodyAttributes = head.bodyAttributes;
|
|
640
|
+
if (bodyAttributes) result = result.replace(/<body([^>]*)>/i, (_, existingAttrs) => `<body${this.mergeAttributes(existingAttrs, bodyAttributes)}>`);
|
|
641
|
+
let headContent = "";
|
|
642
|
+
const title = head.title;
|
|
643
|
+
if (title) if (template.includes("<title>")) result = result.replace(/<title>(.*?)<\/title>/i, () => `<title>${this.escapeHtml(title)}</title>`);
|
|
644
|
+
else headContent += `<title>${this.escapeHtml(title)}</title>\n`;
|
|
645
|
+
if (head.meta) for (const meta of head.meta) headContent += `<meta name="${this.escapeHtml(meta.name)}" content="${this.escapeHtml(meta.content)}">\n`;
|
|
646
|
+
result = result.replace(/<head([^>]*)>(.*?)<\/head>/is, (_, existingAttrs, existingHead) => `<head${existingAttrs}>${existingHead}${headContent}</head>`);
|
|
647
|
+
return result.trim();
|
|
648
|
+
}
|
|
649
|
+
mergeAttributes(existing, attrs) {
|
|
650
|
+
const existingAttrs = this.parseAttributes(existing);
|
|
651
|
+
const merged = {
|
|
652
|
+
...existingAttrs,
|
|
653
|
+
...attrs
|
|
654
|
+
};
|
|
655
|
+
return Object.entries(merged).map(([k, v]) => ` ${k}="${this.escapeHtml(v)}"`).join("");
|
|
656
|
+
}
|
|
657
|
+
parseAttributes(attrStr) {
|
|
658
|
+
const attrs = {};
|
|
659
|
+
const attrRegex = /([^\s=]+)(?:="([^"]*)")?/g;
|
|
660
|
+
let match = attrRegex.exec(attrStr);
|
|
661
|
+
while (match) {
|
|
662
|
+
attrs[match[1]] = match[2] ?? "";
|
|
663
|
+
match = attrRegex.exec(attrStr);
|
|
664
|
+
}
|
|
665
|
+
return attrs;
|
|
666
|
+
}
|
|
667
|
+
escapeHtml(str) {
|
|
668
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
//#endregion
|
|
673
|
+
//#region src/providers/ReactServerProvider.ts
|
|
76
674
|
const envSchema = t.object({
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
675
|
+
REACT_SERVER_DIST: t.string({ default: "public" }),
|
|
676
|
+
REACT_SERVER_PREFIX: t.string({ default: "" }),
|
|
677
|
+
REACT_SSR_ENABLED: t.optional(t.boolean()),
|
|
678
|
+
REACT_ROOT_ID: t.string({ default: "root" })
|
|
81
679
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
renderToHtml(template, state, context) {
|
|
292
|
-
const element = this.pageDescriptorProvider.root(state, context);
|
|
293
|
-
this.serverTimingProvider.beginTiming("renderToString");
|
|
294
|
-
let app = "";
|
|
295
|
-
try {
|
|
296
|
-
app = renderToString(element);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
this.log.error("Error during SSR", error);
|
|
299
|
-
app = renderToString(context.onError(error));
|
|
300
|
-
}
|
|
301
|
-
this.serverTimingProvider.endTiming("renderToString");
|
|
302
|
-
const hydrationData = {
|
|
303
|
-
links: context.links,
|
|
304
|
-
layers: state.layers.map((it) => ({
|
|
305
|
-
...it,
|
|
306
|
-
error: it.error ? {
|
|
307
|
-
...it.error,
|
|
308
|
-
name: it.error.name,
|
|
309
|
-
message: it.error.message,
|
|
310
|
-
stack: it.error.stack
|
|
311
|
-
// TODO: Hide stack in production ?
|
|
312
|
-
} : void 0,
|
|
313
|
-
index: void 0,
|
|
314
|
-
path: void 0,
|
|
315
|
-
element: void 0
|
|
316
|
-
}))
|
|
317
|
-
};
|
|
318
|
-
const script = `<script>window.__ssr=${JSON.stringify(hydrationData)}<\/script>`;
|
|
319
|
-
const response = {
|
|
320
|
-
html: template
|
|
321
|
-
};
|
|
322
|
-
this.fillTemplate(response, app, script);
|
|
323
|
-
if (context.head) {
|
|
324
|
-
response.html = this.headProvider.renderHead(response.html, context.head);
|
|
325
|
-
}
|
|
326
|
-
return response.html;
|
|
327
|
-
}
|
|
328
|
-
fillTemplate(response, app, script) {
|
|
329
|
-
if (this.ROOT_DIV_REGEX.test(response.html)) {
|
|
330
|
-
response.html = response.html.replace(
|
|
331
|
-
this.ROOT_DIV_REGEX,
|
|
332
|
-
(_match, beforeId, afterId) => {
|
|
333
|
-
return `<div${beforeId} id="${this.env.REACT_ROOT_ID}"${afterId}>${app}</div>`;
|
|
334
|
-
}
|
|
335
|
-
);
|
|
336
|
-
} else {
|
|
337
|
-
const bodyOpenTag = /<body([^>]*)>/i;
|
|
338
|
-
if (bodyOpenTag.test(response.html)) {
|
|
339
|
-
response.html = response.html.replace(bodyOpenTag, (match) => {
|
|
340
|
-
return `${match}
|
|
341
|
-
<div id="${this.env.REACT_ROOT_ID}">${app}</div>`;
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
const bodyCloseTagRegex = /<\/body>/i;
|
|
346
|
-
if (bodyCloseTagRegex.test(response.html)) {
|
|
347
|
-
response.html = response.html.replace(
|
|
348
|
-
bodyCloseTagRegex,
|
|
349
|
-
`${script}
|
|
350
|
-
</body>`
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
680
|
+
var ReactServerProvider = class {
|
|
681
|
+
log = $logger();
|
|
682
|
+
alepha = $inject(Alepha);
|
|
683
|
+
pageDescriptorProvider = $inject(PageDescriptorProvider);
|
|
684
|
+
serverStaticProvider = $inject(ServerStaticProvider);
|
|
685
|
+
serverRouterProvider = $inject(ServerRouterProvider);
|
|
686
|
+
headProvider = $inject(ServerHeadProvider);
|
|
687
|
+
serverTimingProvider = $inject(ServerTimingProvider);
|
|
688
|
+
env = $inject(envSchema);
|
|
689
|
+
ROOT_DIV_REGEX = new RegExp(`<div([^>]*)\\s+id=["']${this.env.REACT_ROOT_ID}["']([^>]*)>(.*?)<\\/div>`, "is");
|
|
690
|
+
onConfigure = $hook({
|
|
691
|
+
name: "configure",
|
|
692
|
+
handler: async () => {
|
|
693
|
+
const pages = this.alepha.getDescriptorValues($page);
|
|
694
|
+
const ssrEnabled = pages.length > 0 && this.env.REACT_SSR_ENABLED !== false;
|
|
695
|
+
this.alepha.state("ReactServerProvider.ssr", ssrEnabled);
|
|
696
|
+
for (const { key, instance, value } of pages) {
|
|
697
|
+
const name = value[OPTIONS].name ?? key;
|
|
698
|
+
instance[key].prerender = this.createRenderFunction(name, true);
|
|
699
|
+
if (this.alepha.isTest()) instance[key].render = this.createRenderFunction(name);
|
|
700
|
+
}
|
|
701
|
+
if (this.alepha.isServerless() === "vite") {
|
|
702
|
+
await this.configureVite(ssrEnabled);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
let root = "";
|
|
706
|
+
if (!this.alepha.isServerless()) {
|
|
707
|
+
root = this.getPublicDirectory();
|
|
708
|
+
if (!root) this.log.warn("Missing static files, static file server will be disabled");
|
|
709
|
+
else {
|
|
710
|
+
this.log.debug(`Using static files from: ${root}`);
|
|
711
|
+
await this.configureStaticServer(root);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (ssrEnabled) {
|
|
715
|
+
await this.registerPages(async () => this.template);
|
|
716
|
+
this.log.info("SSR OK");
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
this.log.info("SSR is disabled, use History API fallback");
|
|
720
|
+
await this.serverRouterProvider.route({
|
|
721
|
+
path: "*",
|
|
722
|
+
handler: async ({ url, reply }) => {
|
|
723
|
+
if (url.pathname.includes(".")) {
|
|
724
|
+
reply.headers["content-type"] = "text/plain";
|
|
725
|
+
reply.body = "Not Found";
|
|
726
|
+
reply.status = 404;
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
reply.headers["content-type"] = "text/html";
|
|
730
|
+
return this.template;
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
get template() {
|
|
736
|
+
return this.alepha.state("ReactServerProvider.template");
|
|
737
|
+
}
|
|
738
|
+
async registerPages(templateLoader) {
|
|
739
|
+
for (const page of this.pageDescriptorProvider.getPages()) {
|
|
740
|
+
if (page.children?.length) continue;
|
|
741
|
+
this.log.debug(`+ ${page.match} -> ${page.name}`);
|
|
742
|
+
await this.serverRouterProvider.route({
|
|
743
|
+
...page,
|
|
744
|
+
schema: void 0,
|
|
745
|
+
method: "GET",
|
|
746
|
+
path: page.match,
|
|
747
|
+
handler: this.createHandler(page, templateLoader)
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
getPublicDirectory() {
|
|
752
|
+
const maybe = [join(process.cwd(), `dist/${this.env.REACT_SERVER_DIST}`), join(process.cwd(), this.env.REACT_SERVER_DIST)];
|
|
753
|
+
for (const it of maybe) if (existsSync(it)) return it;
|
|
754
|
+
return "";
|
|
755
|
+
}
|
|
756
|
+
async configureStaticServer(root) {
|
|
757
|
+
await this.serverStaticProvider.serve({
|
|
758
|
+
root,
|
|
759
|
+
path: this.env.REACT_SERVER_PREFIX
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
async configureVite(ssrEnabled) {
|
|
763
|
+
if (!ssrEnabled) return;
|
|
764
|
+
this.log.info("SSR (vite) OK");
|
|
765
|
+
const url = `http://${process.env.SERVER_HOST}:${process.env.SERVER_PORT}`;
|
|
766
|
+
await this.registerPages(() => fetch(`${url}/index.html`).then((it) => it.text()).catch(() => void 0));
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* For testing purposes, creates a render function that can be used.
|
|
770
|
+
*/
|
|
771
|
+
createRenderFunction(name, withIndex = false) {
|
|
772
|
+
return async (options = {}) => {
|
|
773
|
+
const page = this.pageDescriptorProvider.page(name);
|
|
774
|
+
const url = new URL(this.pageDescriptorProvider.url(name, options));
|
|
775
|
+
const context = {
|
|
776
|
+
url,
|
|
777
|
+
params: options.params ?? {},
|
|
778
|
+
query: options.query ?? {},
|
|
779
|
+
head: {},
|
|
780
|
+
onError: () => null
|
|
781
|
+
};
|
|
782
|
+
const state = await this.pageDescriptorProvider.createLayers(page, context);
|
|
783
|
+
if (!withIndex) return {
|
|
784
|
+
context,
|
|
785
|
+
html: renderToString(this.pageDescriptorProvider.root(state, context))
|
|
786
|
+
};
|
|
787
|
+
return {
|
|
788
|
+
context,
|
|
789
|
+
html: this.renderToHtml(this.template ?? "", state, context)
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
createHandler(page, templateLoader) {
|
|
794
|
+
return async (serverRequest) => {
|
|
795
|
+
const { url, reply, query, params } = serverRequest;
|
|
796
|
+
const template = await templateLoader();
|
|
797
|
+
if (!template) throw new Error("Template not found");
|
|
798
|
+
const context = {
|
|
799
|
+
url,
|
|
800
|
+
params,
|
|
801
|
+
query,
|
|
802
|
+
head: {},
|
|
803
|
+
onError: () => null
|
|
804
|
+
};
|
|
805
|
+
if (this.alepha.has(ServerLinksProvider)) {
|
|
806
|
+
const srv = this.alepha.get(ServerLinksProvider);
|
|
807
|
+
const schema = apiLinksResponseSchema;
|
|
808
|
+
context.links = this.alepha.parse(schema, await srv.getLinks({
|
|
809
|
+
user: serverRequest.user,
|
|
810
|
+
authorization: serverRequest.headers.authorization
|
|
811
|
+
}));
|
|
812
|
+
this.alepha.context.set("links", context.links);
|
|
813
|
+
}
|
|
814
|
+
let target = page;
|
|
815
|
+
while (target) {
|
|
816
|
+
if (page.can && !page.can()) {
|
|
817
|
+
reply.status = 403;
|
|
818
|
+
reply.headers["content-type"] = "text/plain";
|
|
819
|
+
return "Forbidden";
|
|
820
|
+
}
|
|
821
|
+
target = target.parent;
|
|
822
|
+
}
|
|
823
|
+
await this.alepha.emit("react:server:render", {
|
|
824
|
+
request: serverRequest,
|
|
825
|
+
pageRequest: context
|
|
826
|
+
}, { log: false });
|
|
827
|
+
this.serverTimingProvider.beginTiming("createLayers");
|
|
828
|
+
const state = await this.pageDescriptorProvider.createLayers(page, context);
|
|
829
|
+
this.serverTimingProvider.endTiming("createLayers");
|
|
830
|
+
if (state.redirect) return reply.redirect(state.redirect);
|
|
831
|
+
reply.headers["content-type"] = "text/html";
|
|
832
|
+
reply.headers["cache-control"] = "no-store, no-cache, must-revalidate, proxy-revalidate";
|
|
833
|
+
reply.headers.pragma = "no-cache";
|
|
834
|
+
reply.headers.expires = "0";
|
|
835
|
+
if (page.cache && serverRequest.user) delete context.links;
|
|
836
|
+
const html = this.renderToHtml(template, state, context);
|
|
837
|
+
page.afterHandler?.(serverRequest);
|
|
838
|
+
return html;
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
renderToHtml(template, state, context) {
|
|
842
|
+
const element = this.pageDescriptorProvider.root(state, context);
|
|
843
|
+
this.serverTimingProvider.beginTiming("renderToString");
|
|
844
|
+
let app = "";
|
|
845
|
+
try {
|
|
846
|
+
app = renderToString(element);
|
|
847
|
+
} catch (error) {
|
|
848
|
+
this.log.error("Error during SSR", error);
|
|
849
|
+
app = renderToString(context.onError(error));
|
|
850
|
+
}
|
|
851
|
+
this.serverTimingProvider.endTiming("renderToString");
|
|
852
|
+
const hydrationData = {
|
|
853
|
+
links: context.links,
|
|
854
|
+
layers: state.layers.map((it) => ({
|
|
855
|
+
...it,
|
|
856
|
+
error: it.error ? {
|
|
857
|
+
...it.error,
|
|
858
|
+
name: it.error.name,
|
|
859
|
+
message: it.error.message,
|
|
860
|
+
stack: it.error.stack
|
|
861
|
+
} : void 0,
|
|
862
|
+
index: void 0,
|
|
863
|
+
path: void 0,
|
|
864
|
+
element: void 0
|
|
865
|
+
}))
|
|
866
|
+
};
|
|
867
|
+
const script = `<script>window.__ssr=${JSON.stringify(hydrationData)}</script>`;
|
|
868
|
+
const response = { html: template };
|
|
869
|
+
this.fillTemplate(response, app, script);
|
|
870
|
+
if (context.head) response.html = this.headProvider.renderHead(response.html, context.head);
|
|
871
|
+
return response.html;
|
|
872
|
+
}
|
|
873
|
+
fillTemplate(response, app, script) {
|
|
874
|
+
if (this.ROOT_DIV_REGEX.test(response.html)) response.html = response.html.replace(this.ROOT_DIV_REGEX, (_match, beforeId, afterId) => {
|
|
875
|
+
return `<div${beforeId} id="${this.env.REACT_ROOT_ID}"${afterId}>${app}</div>`;
|
|
876
|
+
});
|
|
877
|
+
else {
|
|
878
|
+
const bodyOpenTag = /<body([^>]*)>/i;
|
|
879
|
+
if (bodyOpenTag.test(response.html)) response.html = response.html.replace(bodyOpenTag, (match) => {
|
|
880
|
+
return `${match}\n<div id="${this.env.REACT_ROOT_ID}">${app}</div>`;
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
const bodyCloseTagRegex = /<\/body>/i;
|
|
884
|
+
if (bodyCloseTagRegex.test(response.html)) response.html = response.html.replace(bodyCloseTagRegex, `${script}\n</body>`);
|
|
885
|
+
}
|
|
886
|
+
};
|
|
355
887
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
888
|
+
//#endregion
|
|
889
|
+
//#region src/providers/BrowserHeadProvider.ts
|
|
890
|
+
var BrowserHeadProvider = class {
|
|
891
|
+
renderHead(document, head) {
|
|
892
|
+
if (head.title) document.title = head.title;
|
|
893
|
+
if (head.bodyAttributes) for (const [key, value] of Object.entries(head.bodyAttributes)) if (value) document.body.setAttribute(key, value);
|
|
894
|
+
else document.body.removeAttribute(key);
|
|
895
|
+
if (head.htmlAttributes) for (const [key, value] of Object.entries(head.htmlAttributes)) if (value) document.documentElement.setAttribute(key, value);
|
|
896
|
+
else document.documentElement.removeAttribute(key);
|
|
897
|
+
if (head.meta) for (const [key, value] of Object.entries(head.meta)) {
|
|
898
|
+
const meta = document.querySelector(`meta[name="${key}"]`);
|
|
899
|
+
if (meta) meta.setAttribute("content", value.content);
|
|
900
|
+
else {
|
|
901
|
+
const newMeta = document.createElement("meta");
|
|
902
|
+
newMeta.setAttribute("name", key);
|
|
903
|
+
newMeta.setAttribute("content", value.content);
|
|
904
|
+
document.head.appendChild(newMeta);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
//#endregion
|
|
911
|
+
//#region src/providers/BrowserRouterProvider.ts
|
|
912
|
+
var BrowserRouterProvider = class extends RouterProvider {
|
|
913
|
+
log = $logger();
|
|
914
|
+
alepha = $inject(Alepha);
|
|
915
|
+
pageDescriptorProvider = $inject(PageDescriptorProvider);
|
|
916
|
+
add(entry) {
|
|
917
|
+
this.pageDescriptorProvider.add(entry);
|
|
918
|
+
}
|
|
919
|
+
configure = $hook({
|
|
920
|
+
name: "configure",
|
|
921
|
+
handler: async () => {
|
|
922
|
+
for (const page of this.pageDescriptorProvider.getPages()) if (page.component || page.lazy) this.push({
|
|
923
|
+
path: page.match,
|
|
924
|
+
page
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
async transition(url, options = {}) {
|
|
929
|
+
const { pathname, search } = url;
|
|
930
|
+
const state = {
|
|
931
|
+
pathname,
|
|
932
|
+
search,
|
|
933
|
+
layers: []
|
|
934
|
+
};
|
|
935
|
+
const context = {
|
|
936
|
+
url,
|
|
937
|
+
query: {},
|
|
938
|
+
params: {},
|
|
939
|
+
head: {},
|
|
940
|
+
onError: () => null,
|
|
941
|
+
...options.context ?? {}
|
|
942
|
+
};
|
|
943
|
+
await this.alepha.emit("react:transition:begin", {
|
|
944
|
+
state,
|
|
945
|
+
context
|
|
946
|
+
});
|
|
947
|
+
try {
|
|
948
|
+
const previous = options.previous;
|
|
949
|
+
const { route, params } = this.match(pathname);
|
|
950
|
+
const query = {};
|
|
951
|
+
if (search) for (const [key, value] of new URLSearchParams(search).entries()) query[key] = String(value);
|
|
952
|
+
context.query = query;
|
|
953
|
+
context.params = params ?? {};
|
|
954
|
+
context.previous = previous;
|
|
955
|
+
if (isPageRoute(route)) {
|
|
956
|
+
const result = await this.pageDescriptorProvider.createLayers(route.page, context);
|
|
957
|
+
if (result.redirect) return {
|
|
958
|
+
redirect: result.redirect,
|
|
959
|
+
state,
|
|
960
|
+
context
|
|
961
|
+
};
|
|
962
|
+
state.layers = result.layers;
|
|
963
|
+
}
|
|
964
|
+
if (state.layers.length === 0) state.layers.push({
|
|
965
|
+
name: "not-found",
|
|
966
|
+
element: createElement(NotFoundPage),
|
|
967
|
+
index: 0,
|
|
968
|
+
path: "/"
|
|
969
|
+
});
|
|
970
|
+
await this.alepha.emit("react:transition:success", { state });
|
|
971
|
+
} catch (e) {
|
|
972
|
+
this.log.error(e);
|
|
973
|
+
state.layers = [{
|
|
974
|
+
name: "error",
|
|
975
|
+
element: this.pageDescriptorProvider.renderError(e),
|
|
976
|
+
index: 0,
|
|
977
|
+
path: "/"
|
|
978
|
+
}];
|
|
979
|
+
await this.alepha.emit("react:transition:error", {
|
|
980
|
+
error: e,
|
|
981
|
+
state,
|
|
982
|
+
context
|
|
983
|
+
});
|
|
984
|
+
}
|
|
985
|
+
if (options.state) {
|
|
986
|
+
options.state.layers = state.layers;
|
|
987
|
+
options.state.pathname = state.pathname;
|
|
988
|
+
options.state.search = state.search;
|
|
989
|
+
}
|
|
990
|
+
await this.alepha.emit("react:transition:end", {
|
|
991
|
+
state: options.state,
|
|
992
|
+
context
|
|
993
|
+
});
|
|
994
|
+
return {
|
|
995
|
+
context,
|
|
996
|
+
state
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
root(state, context) {
|
|
1000
|
+
return this.pageDescriptorProvider.root(state, context);
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
//#endregion
|
|
1005
|
+
//#region src/providers/ReactBrowserProvider.ts
|
|
1006
|
+
var ReactBrowserProvider = class {
|
|
1007
|
+
log = $logger();
|
|
1008
|
+
client = $inject(HttpClient);
|
|
1009
|
+
alepha = $inject(Alepha);
|
|
1010
|
+
router = $inject(BrowserRouterProvider);
|
|
1011
|
+
headProvider = $inject(BrowserHeadProvider);
|
|
1012
|
+
root;
|
|
1013
|
+
transitioning;
|
|
1014
|
+
state = {
|
|
1015
|
+
layers: [],
|
|
1016
|
+
pathname: "",
|
|
1017
|
+
search: ""
|
|
1018
|
+
};
|
|
1019
|
+
get document() {
|
|
1020
|
+
return window.document;
|
|
1021
|
+
}
|
|
1022
|
+
get history() {
|
|
1023
|
+
return window.history;
|
|
1024
|
+
}
|
|
1025
|
+
get url() {
|
|
1026
|
+
return window.location.pathname + window.location.search;
|
|
1027
|
+
}
|
|
1028
|
+
async invalidate(props) {
|
|
1029
|
+
const previous = [];
|
|
1030
|
+
if (props) {
|
|
1031
|
+
const [key] = Object.keys(props);
|
|
1032
|
+
const value = props[key];
|
|
1033
|
+
for (const layer of this.state.layers) {
|
|
1034
|
+
if (layer.props?.[key]) {
|
|
1035
|
+
previous.push({
|
|
1036
|
+
...layer,
|
|
1037
|
+
props: {
|
|
1038
|
+
...layer.props,
|
|
1039
|
+
[key]: value
|
|
1040
|
+
}
|
|
1041
|
+
});
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
previous.push(layer);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
await this.render({ previous });
|
|
1048
|
+
}
|
|
1049
|
+
async go(url, options = {}) {
|
|
1050
|
+
const result = await this.render({ url });
|
|
1051
|
+
if (result.context.url.pathname !== url) {
|
|
1052
|
+
this.history.replaceState({}, "", result.context.url.pathname);
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
if (options.replace) {
|
|
1056
|
+
this.history.replaceState({}, "", url);
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
this.history.pushState({}, "", url);
|
|
1060
|
+
}
|
|
1061
|
+
async render(options = {}) {
|
|
1062
|
+
const previous = options.previous ?? this.state.layers;
|
|
1063
|
+
const url = options.url ?? this.url;
|
|
1064
|
+
this.transitioning = { to: url };
|
|
1065
|
+
const result = await this.router.transition(new URL(`http://localhost${url}`), {
|
|
1066
|
+
previous,
|
|
1067
|
+
state: this.state
|
|
1068
|
+
});
|
|
1069
|
+
if (result.redirect) return await this.render({ url: result.redirect });
|
|
1070
|
+
this.transitioning = void 0;
|
|
1071
|
+
return result;
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Get embedded layers from the server.
|
|
1075
|
+
*/
|
|
1076
|
+
getHydrationState() {
|
|
1077
|
+
try {
|
|
1078
|
+
if ("__ssr" in window && typeof window.__ssr === "object") return window.__ssr;
|
|
1079
|
+
} catch (error) {
|
|
1080
|
+
console.error(error);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
ready = $hook({
|
|
1084
|
+
name: "ready",
|
|
1085
|
+
handler: async () => {
|
|
1086
|
+
const hydration = this.getHydrationState();
|
|
1087
|
+
const previous = hydration?.layers ?? [];
|
|
1088
|
+
if (hydration?.links) for (const link of hydration.links.links) this.client.pushLink(link);
|
|
1089
|
+
const { context } = await this.render({ previous });
|
|
1090
|
+
if (context.head) this.headProvider.renderHead(this.document, context.head);
|
|
1091
|
+
await this.alepha.emit("react:browser:render", {
|
|
1092
|
+
state: this.state,
|
|
1093
|
+
context,
|
|
1094
|
+
hydration
|
|
1095
|
+
});
|
|
1096
|
+
window.addEventListener("popstate", () => {
|
|
1097
|
+
this.render();
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
onTransitionEnd = $hook({
|
|
1102
|
+
name: "react:transition:end",
|
|
1103
|
+
handler: async ({ context }) => {
|
|
1104
|
+
this.headProvider.renderHead(this.document, context.head);
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
};
|
|
447
1108
|
|
|
1109
|
+
//#endregion
|
|
1110
|
+
//#region src/hooks/RouterHookApi.ts
|
|
1111
|
+
var RouterHookApi = class {
|
|
1112
|
+
constructor(pages, state, layer, browser) {
|
|
1113
|
+
this.pages = pages;
|
|
1114
|
+
this.state = state;
|
|
1115
|
+
this.layer = layer;
|
|
1116
|
+
this.browser = browser;
|
|
1117
|
+
}
|
|
1118
|
+
get current() {
|
|
1119
|
+
return this.state;
|
|
1120
|
+
}
|
|
1121
|
+
get pathname() {
|
|
1122
|
+
return this.state.pathname;
|
|
1123
|
+
}
|
|
1124
|
+
get query() {
|
|
1125
|
+
const query = {};
|
|
1126
|
+
for (const [key, value] of new URLSearchParams(this.state.search).entries()) query[key] = String(value);
|
|
1127
|
+
return query;
|
|
1128
|
+
}
|
|
1129
|
+
async back() {
|
|
1130
|
+
this.browser?.history.back();
|
|
1131
|
+
}
|
|
1132
|
+
async forward() {
|
|
1133
|
+
this.browser?.history.forward();
|
|
1134
|
+
}
|
|
1135
|
+
async invalidate(props) {
|
|
1136
|
+
await this.browser?.invalidate(props);
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Create a valid href for the given pathname.
|
|
1140
|
+
*
|
|
1141
|
+
* @param pathname
|
|
1142
|
+
* @param layer
|
|
1143
|
+
*/
|
|
1144
|
+
createHref(pathname, layer = this.layer, options = {}) {
|
|
1145
|
+
if (typeof pathname === "object") pathname = pathname.options.path ?? "";
|
|
1146
|
+
if (options.params) for (const [key, value] of Object.entries(options.params)) pathname = pathname.replace(`:${key}`, String(value));
|
|
1147
|
+
return pathname.startsWith("/") ? pathname : `${layer.path}/${pathname}`.replace(/\/\/+/g, "/");
|
|
1148
|
+
}
|
|
1149
|
+
async go(path, options) {
|
|
1150
|
+
for (const page of this.pages) if (page.name === path) {
|
|
1151
|
+
path = page.path ?? "";
|
|
1152
|
+
break;
|
|
1153
|
+
}
|
|
1154
|
+
await this.browser?.go(this.createHref(path, this.layer, options), options);
|
|
1155
|
+
}
|
|
1156
|
+
anchor(path, options = {}) {
|
|
1157
|
+
for (const page of this.pages) if (page.name === path) {
|
|
1158
|
+
path = page.path ?? "";
|
|
1159
|
+
break;
|
|
1160
|
+
}
|
|
1161
|
+
const href = this.createHref(path, this.layer, options);
|
|
1162
|
+
return {
|
|
1163
|
+
href,
|
|
1164
|
+
onClick: (ev) => {
|
|
1165
|
+
ev.stopPropagation();
|
|
1166
|
+
ev.preventDefault();
|
|
1167
|
+
this.go(path, options).catch(console.error);
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Set query params.
|
|
1173
|
+
*
|
|
1174
|
+
* @param record
|
|
1175
|
+
* @param options
|
|
1176
|
+
*/
|
|
1177
|
+
setQueryParams(record, options = {}) {
|
|
1178
|
+
const func = typeof record === "function" ? record : () => record;
|
|
1179
|
+
const search = new URLSearchParams(func(this.query)).toString();
|
|
1180
|
+
const state = search ? `${this.pathname}?${search}` : this.pathname;
|
|
1181
|
+
if (options.push) window.history.pushState({}, "", state);
|
|
1182
|
+
else window.history.replaceState({}, "", state);
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
//#endregion
|
|
1187
|
+
//#region src/hooks/useRouter.ts
|
|
448
1188
|
const useRouter = () => {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}, []);
|
|
457
|
-
return useMemo(
|
|
458
|
-
() => new RouterHookApi(
|
|
459
|
-
pages,
|
|
460
|
-
ctx.state,
|
|
461
|
-
layer,
|
|
462
|
-
ctx.alepha.isBrowser() ? ctx.alepha.get(ReactBrowserProvider) : void 0
|
|
463
|
-
),
|
|
464
|
-
[layer]
|
|
465
|
-
);
|
|
1189
|
+
const ctx = useContext(RouterContext);
|
|
1190
|
+
const layer = useContext(RouterLayerContext);
|
|
1191
|
+
if (!ctx || !layer) throw new Error("useRouter must be used within a RouterProvider");
|
|
1192
|
+
const pages = useMemo(() => {
|
|
1193
|
+
return ctx.alepha.get(PageDescriptorProvider).getPages();
|
|
1194
|
+
}, []);
|
|
1195
|
+
return useMemo(() => new RouterHookApi(pages, ctx.state, layer, ctx.alepha.isBrowser() ? ctx.alepha.get(ReactBrowserProvider) : void 0), [layer]);
|
|
466
1196
|
};
|
|
467
1197
|
|
|
1198
|
+
//#endregion
|
|
1199
|
+
//#region src/components/Link.tsx
|
|
468
1200
|
const Link = (props) => {
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
1201
|
+
React.useContext(RouterContext);
|
|
1202
|
+
const router = useRouter();
|
|
1203
|
+
const to = typeof props.to === "string" ? props.to : props.to[OPTIONS].path;
|
|
1204
|
+
if (!to) return null;
|
|
1205
|
+
const can = typeof props.to === "string" ? void 0 : props.to[OPTIONS].can;
|
|
1206
|
+
if (can && !can()) return null;
|
|
1207
|
+
const name = typeof props.to === "string" ? void 0 : props.to[OPTIONS].name;
|
|
1208
|
+
const anchorProps = {
|
|
1209
|
+
...props,
|
|
1210
|
+
to: void 0
|
|
1211
|
+
};
|
|
1212
|
+
return /* @__PURE__ */ jsx("a", {
|
|
1213
|
+
...router.anchor(to),
|
|
1214
|
+
...anchorProps,
|
|
1215
|
+
children: props.children ?? name
|
|
1216
|
+
});
|
|
485
1217
|
};
|
|
1218
|
+
var Link_default = Link;
|
|
486
1219
|
|
|
1220
|
+
//#endregion
|
|
1221
|
+
//#region src/hooks/useActive.ts
|
|
487
1222
|
const useActive = (path) => {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
router.go(href).then(() => {
|
|
518
|
-
setPending(false);
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
};
|
|
1223
|
+
const router = useRouter();
|
|
1224
|
+
const ctx = useContext(RouterContext);
|
|
1225
|
+
const layer = useContext(RouterLayerContext);
|
|
1226
|
+
if (!ctx || !layer) throw new Error("useRouter must be used within a RouterProvider");
|
|
1227
|
+
let name;
|
|
1228
|
+
if (typeof path === "object" && path.options.name) name = path.options.name;
|
|
1229
|
+
const [current, setCurrent] = useState(ctx.state.pathname);
|
|
1230
|
+
const href = useMemo(() => router.createHref(path, layer), [path, layer]);
|
|
1231
|
+
const [isPending, setPending] = useState(false);
|
|
1232
|
+
const isActive = current === href;
|
|
1233
|
+
useRouterEvents({ onEnd: ({ state }) => setCurrent(state.pathname) });
|
|
1234
|
+
return {
|
|
1235
|
+
name,
|
|
1236
|
+
isPending,
|
|
1237
|
+
isActive,
|
|
1238
|
+
anchorProps: {
|
|
1239
|
+
href,
|
|
1240
|
+
onClick: (ev) => {
|
|
1241
|
+
ev.stopPropagation();
|
|
1242
|
+
ev.preventDefault();
|
|
1243
|
+
if (isActive) return;
|
|
1244
|
+
if (isPending) return;
|
|
1245
|
+
setPending(true);
|
|
1246
|
+
router.go(href).then(() => {
|
|
1247
|
+
setPending(false);
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
};
|
|
523
1252
|
};
|
|
524
1253
|
|
|
1254
|
+
//#endregion
|
|
1255
|
+
//#region src/hooks/useInject.ts
|
|
525
1256
|
const useInject = (clazz) => {
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
return useMemo(() => ctx.alepha.get(clazz), []);
|
|
1257
|
+
const ctx = useContext(RouterContext);
|
|
1258
|
+
if (!ctx) throw new Error("useRouter must be used within a <RouterProvider>");
|
|
1259
|
+
return useMemo(() => ctx.alepha.get(clazz), []);
|
|
531
1260
|
};
|
|
532
1261
|
|
|
1262
|
+
//#endregion
|
|
1263
|
+
//#region src/hooks/useClient.ts
|
|
533
1264
|
const useClient = (_scope) => {
|
|
534
|
-
|
|
1265
|
+
return useInject(HttpClient).of();
|
|
535
1266
|
};
|
|
536
1267
|
|
|
1268
|
+
//#endregion
|
|
1269
|
+
//#region src/hooks/useQueryParams.ts
|
|
537
1270
|
const useQueryParams = (schema, options = {}) => {
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
return { ...data, [key]: encode(ctx.alepha, schema, queryParams2) };
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
];
|
|
1271
|
+
const ctx = useContext(RouterContext);
|
|
1272
|
+
if (!ctx) throw new Error("useQueryParams must be used within a RouterProvider");
|
|
1273
|
+
const key = options.key ?? "q";
|
|
1274
|
+
const router = useRouter();
|
|
1275
|
+
const querystring = router.query[key];
|
|
1276
|
+
const [queryParams, setQueryParams] = useState(decode(ctx.alepha, schema, router.query[key]));
|
|
1277
|
+
useEffect(() => {
|
|
1278
|
+
setQueryParams(decode(ctx.alepha, schema, querystring));
|
|
1279
|
+
}, [querystring]);
|
|
1280
|
+
return [queryParams, (queryParams$1) => {
|
|
1281
|
+
setQueryParams(queryParams$1);
|
|
1282
|
+
router.setQueryParams((data) => {
|
|
1283
|
+
return {
|
|
1284
|
+
...data,
|
|
1285
|
+
[key]: encode(ctx.alepha, schema, queryParams$1)
|
|
1286
|
+
};
|
|
1287
|
+
});
|
|
1288
|
+
}];
|
|
560
1289
|
};
|
|
561
1290
|
const encode = (alepha, schema, data) => {
|
|
562
|
-
|
|
1291
|
+
return btoa(JSON.stringify(alepha.parse(schema, data)));
|
|
563
1292
|
};
|
|
564
1293
|
const decode = (alepha, schema, data) => {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
1294
|
+
try {
|
|
1295
|
+
return alepha.parse(schema, JSON.parse(atob(decodeURIComponent(data))));
|
|
1296
|
+
} catch (_error) {
|
|
1297
|
+
return {};
|
|
1298
|
+
}
|
|
570
1299
|
};
|
|
571
1300
|
|
|
1301
|
+
//#endregion
|
|
1302
|
+
//#region src/hooks/useRouterState.ts
|
|
572
1303
|
const useRouterState = () => {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
useRouterEvents({
|
|
580
|
-
onEnd: ({ state: state2 }) => setState({ ...state2 })
|
|
581
|
-
});
|
|
582
|
-
return state;
|
|
1304
|
+
const ctx = useContext(RouterContext);
|
|
1305
|
+
const layer = useContext(RouterLayerContext);
|
|
1306
|
+
if (!ctx || !layer) throw new Error("useRouter must be used within a RouterProvider");
|
|
1307
|
+
const [state, setState] = useState(ctx.state);
|
|
1308
|
+
useRouterEvents({ onEnd: ({ state: state$1 }) => setState({ ...state$1 }) });
|
|
1309
|
+
return state;
|
|
583
1310
|
};
|
|
584
1311
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
1312
|
+
//#endregion
|
|
1313
|
+
//#region src/index.ts
|
|
1314
|
+
/**
|
|
1315
|
+
* Alepha React Module
|
|
1316
|
+
*
|
|
1317
|
+
* Alepha React Module contains a router for client-side navigation and server-side rendering.
|
|
1318
|
+
* Routes can be defined using the `$page` descriptor.
|
|
1319
|
+
*
|
|
1320
|
+
* @see {@link $page}
|
|
1321
|
+
* @module alepha.react
|
|
1322
|
+
*/
|
|
1323
|
+
var AlephaReact = class {
|
|
1324
|
+
name = "alepha.react";
|
|
1325
|
+
$services = (alepha) => alepha.with(AlephaServer).with(AlephaServerCache).with(ReactServerProvider).with(PageDescriptorProvider);
|
|
1326
|
+
};
|
|
589
1327
|
__bind($page, AlephaReact);
|
|
590
1328
|
|
|
591
|
-
|
|
1329
|
+
//#endregion
|
|
1330
|
+
export { $page, AlephaReact, ClientOnly_default as ClientOnly, ErrorBoundary_default as ErrorBoundary, Link_default as Link, NestedView_default as NestedView, PageDescriptorProvider, ReactBrowserProvider, ReactServerProvider, RedirectionError, RouterContext, RouterHookApi, RouterLayerContext, isPageRoute, useActive, useAlepha, useClient, useInject, useQueryParams, useRouter, useRouterEvents, useRouterState };
|
|
1331
|
+
//# sourceMappingURL=index.js.map
|