@blinkk/root 1.0.0-beta.2 → 1.0.0-beta.21

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/render.js CHANGED
@@ -2,17 +2,205 @@ import {
2
2
  htmlMinify,
3
3
  htmlPretty,
4
4
  parseTagNames
5
- } from "./chunk-GGQGZ7ZE.js";
5
+ } from "./chunk-DXD5LKU3.js";
6
6
  import {
7
7
  HTML_CONTEXT,
8
8
  I18N_CONTEXT,
9
9
  REQUEST_CONTEXT,
10
10
  getTranslations
11
- } from "./chunk-WTSNW7BB.js";
11
+ } from "./chunk-WX2GLKRC.js";
12
12
 
13
13
  // src/render/render.tsx
14
14
  import renderToString from "preact-render-to-string";
15
15
 
16
+ // src/core/pages/ErrorPage.tsx
17
+ import { Fragment, jsx, jsxs } from "preact/jsx-runtime";
18
+ var STYLES = `
19
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
20
+
21
+ :root {
22
+ --font-family-text: "Inter", sans-serif;
23
+ }
24
+
25
+ body {
26
+ font-family: var(--font-family-text);
27
+ background: #F5F5F5;
28
+ padding: 40px 16px;
29
+ }
30
+
31
+ .root {
32
+ max-width: 1200px;
33
+ margin: 0 auto;
34
+ }
35
+
36
+ .root.align-center {
37
+ text-align: center;
38
+ }
39
+
40
+ h1.title {
41
+ margin-top: 0;
42
+ margin-bottom: 24px;
43
+ }
44
+
45
+ p.message {
46
+ margin-top: 0;
47
+ margin-bottom: 0;
48
+ }
49
+
50
+ .box {
51
+ font-size: 16px;
52
+ line-height: 1.5;
53
+ padding: 16px;
54
+ border-radius: 12px;
55
+ background: #ffffff;
56
+ }
57
+
58
+ pre.box {
59
+ white-space: pre-wrap;
60
+ }
61
+
62
+ @media (min-width: 500px) {
63
+ body {
64
+ padding: 40px;
65
+ }
66
+
67
+ .box {
68
+ padding: 24px;
69
+ }
70
+ }
71
+
72
+ @media (min-width: 1024px) {
73
+ body {
74
+ padding: 100px;
75
+ }
76
+ }
77
+ `;
78
+ function ErrorPage(props) {
79
+ const { code, message } = props;
80
+ const title = props.title || code;
81
+ return /* @__PURE__ */ jsxs(Fragment, {
82
+ children: [
83
+ /* @__PURE__ */ jsx("style", {
84
+ dangerouslySetInnerHTML: { __html: STYLES }
85
+ }),
86
+ /* @__PURE__ */ jsxs("div", {
87
+ className: `root align-${props.align || "left"}`,
88
+ children: [
89
+ /* @__PURE__ */ jsx("h1", {
90
+ className: "title",
91
+ children: title
92
+ }),
93
+ message && /* @__PURE__ */ jsx("p", {
94
+ className: "message",
95
+ children: message
96
+ }),
97
+ props.children
98
+ ]
99
+ })
100
+ ]
101
+ });
102
+ }
103
+
104
+ // src/core/pages/DevErrorPage.tsx
105
+ import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "preact/jsx-runtime";
106
+ function DevErrorPage(props) {
107
+ var _a;
108
+ const req = props.req;
109
+ const err = props.error;
110
+ const route = props.route;
111
+ const routeParams = props.routeParams;
112
+ let errMsg = String(err);
113
+ if (err && err.stack) {
114
+ errMsg = err.stack.replace(/\(.*node_modules/g, "(node_modules").replace(/at \/.*node_modules/g, "at node_modules");
115
+ if ((_a = req.rootConfig) == null ? void 0 : _a.rootDir) {
116
+ errMsg = errMsg.replaceAll(req.rootConfig.rootDir, "<root>");
117
+ }
118
+ if (process.env.HOME) {
119
+ errMsg = errMsg.replaceAll(process.env.HOME, "$HOME");
120
+ }
121
+ }
122
+ return /* @__PURE__ */ jsxs2(ErrorPage, {
123
+ code: 500,
124
+ title: "Something went wrong",
125
+ children: [
126
+ errMsg && /* @__PURE__ */ jsxs2(Fragment2, {
127
+ children: [
128
+ /* @__PURE__ */ jsx2("h2", {
129
+ children: "Error"
130
+ }),
131
+ /* @__PURE__ */ jsx2("pre", {
132
+ className: "box",
133
+ children: /* @__PURE__ */ jsx2("code", {
134
+ children: errMsg
135
+ })
136
+ })
137
+ ]
138
+ }),
139
+ /* @__PURE__ */ jsx2("h2", {
140
+ children: "Debug Info"
141
+ }),
142
+ /* @__PURE__ */ jsx2("pre", {
143
+ className: "box",
144
+ children: /* @__PURE__ */ jsx2("code", {
145
+ children: `url: ${req.originalUrl}
146
+ route: ${(route == null ? void 0 : route.src) || "null"}
147
+ routeParams: ${routeParams && JSON.stringify(routeParams) || "null"}`
148
+ })
149
+ })
150
+ ]
151
+ });
152
+ }
153
+
154
+ // src/core/pages/DevNotFoundPage.tsx
155
+ import { jsx as jsx3, jsxs as jsxs3 } from "preact/jsx-runtime";
156
+ function DevNotFoundPage(props) {
157
+ const req = props.req;
158
+ const routesList = [];
159
+ let urlMaxLength = 0;
160
+ Object.keys(props.sitemap).forEach((urlPath) => {
161
+ const route = props.sitemap[urlPath].route;
162
+ routesList.push(Object.assign({}, route, { urlPath }));
163
+ if (urlPath.length > urlMaxLength) {
164
+ urlMaxLength = urlPath.length;
165
+ }
166
+ });
167
+ const routesListString = routesList.map((route) => {
168
+ return `${route.urlPath.padEnd(urlMaxLength, " ")} => ${route.src}`;
169
+ }).join("\n");
170
+ return /* @__PURE__ */ jsxs3(ErrorPage, {
171
+ code: 404,
172
+ title: "Not found",
173
+ children: [
174
+ /* @__PURE__ */ jsx3("h2", {
175
+ children: "Routes"
176
+ }),
177
+ routesList.length > 0 ? /* @__PURE__ */ jsx3("pre", {
178
+ className: "box",
179
+ children: /* @__PURE__ */ jsx3("code", {
180
+ children: routesListString
181
+ })
182
+ }) : /* @__PURE__ */ jsxs3("div", {
183
+ className: "box",
184
+ children: [
185
+ "Add your first route at ",
186
+ /* @__PURE__ */ jsx3("code", {
187
+ children: "/routes/index.tsx"
188
+ })
189
+ ]
190
+ }),
191
+ /* @__PURE__ */ jsx3("h2", {
192
+ children: "Debug Info"
193
+ }),
194
+ /* @__PURE__ */ jsx3("pre", {
195
+ className: "box",
196
+ children: /* @__PURE__ */ jsx3("code", {
197
+ children: `url: ${req.originalUrl}`
198
+ })
199
+ })
200
+ ]
201
+ });
202
+ }
203
+
16
204
  // src/render/router.ts
17
205
  import path from "node:path";
18
206
 
@@ -242,186 +430,6 @@ function pathContainsPlaceholders(urlPath) {
242
430
  });
243
431
  }
244
432
 
245
- // src/core/pages/ErrorPage.tsx
246
- import { Fragment, jsx, jsxs } from "preact/jsx-runtime";
247
- var STYLES = `
248
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
249
-
250
- :root {
251
- --font-family-text: "Inter", sans-serif;
252
- }
253
-
254
- body {
255
- font-family: var(--font-family-text);
256
- background: #F5F5F5;
257
- padding: 40px 16px;
258
- }
259
-
260
- .root {
261
- max-width: 1200px;
262
- margin: 0 auto;
263
- }
264
-
265
- h1 {
266
- margin-bottom: 40px;
267
- }
268
-
269
- h2 {
270
- margin-top: 30px;
271
- }
272
-
273
- .box {
274
- font-size: 16px;
275
- line-height: 1.5;
276
- padding: 16px;
277
- border-radius: 12px;
278
- background: #ffffff;
279
- }
280
-
281
- pre.box {
282
- white-space: pre-wrap;
283
- }
284
-
285
- @media (min-width: 500px) {
286
- body {
287
- padding: 40px;
288
- }
289
-
290
- .box {
291
- padding: 24px;
292
- }
293
- }
294
-
295
- @media (min-width: 1024px) {
296
- body {
297
- padding: 100px;
298
- }
299
- }
300
- `;
301
- function ErrorPage(props) {
302
- const { code, message } = props;
303
- const title = props.title ? `${code} | ${props.title}` : code;
304
- return /* @__PURE__ */ jsxs(Fragment, {
305
- children: [
306
- /* @__PURE__ */ jsx("style", {
307
- dangerouslySetInnerHTML: { __html: STYLES }
308
- }),
309
- /* @__PURE__ */ jsxs("div", {
310
- className: "root",
311
- children: [
312
- /* @__PURE__ */ jsx("h1", {
313
- children: title
314
- }),
315
- message && /* @__PURE__ */ jsx("p", {
316
- children: message
317
- }),
318
- props.children
319
- ]
320
- })
321
- ]
322
- });
323
- }
324
-
325
- // src/core/pages/DevNotFoundPage.tsx
326
- import { jsx as jsx2, jsxs as jsxs2 } from "preact/jsx-runtime";
327
- function DevNotFoundPage(props) {
328
- const req = props.req;
329
- const routesList = [];
330
- let urlMaxLength = 0;
331
- Object.keys(props.sitemap).forEach((urlPath) => {
332
- const route = props.sitemap[urlPath].route;
333
- routesList.push(Object.assign({}, route, { urlPath }));
334
- if (urlPath.length > urlMaxLength) {
335
- urlMaxLength = urlPath.length;
336
- }
337
- });
338
- const routesListString = routesList.map((route) => {
339
- return `${route.urlPath.padEnd(urlMaxLength, " ")} => ${route.src}`;
340
- }).join("\n");
341
- return /* @__PURE__ */ jsxs2(ErrorPage, {
342
- code: 404,
343
- title: "Root.js",
344
- children: [
345
- /* @__PURE__ */ jsx2("h2", {
346
- children: "Routes"
347
- }),
348
- routesList.length > 0 ? /* @__PURE__ */ jsx2("pre", {
349
- className: "box",
350
- children: /* @__PURE__ */ jsx2("code", {
351
- children: routesListString
352
- })
353
- }) : /* @__PURE__ */ jsxs2("div", {
354
- className: "box",
355
- children: [
356
- "Add your first route at ",
357
- /* @__PURE__ */ jsx2("code", {
358
- children: "/routes/index.tsx"
359
- })
360
- ]
361
- }),
362
- /* @__PURE__ */ jsx2("h2", {
363
- children: "Debug Info"
364
- }),
365
- /* @__PURE__ */ jsx2("pre", {
366
- className: "box",
367
- children: /* @__PURE__ */ jsx2("code", {
368
- children: `url: ${req.originalUrl}`
369
- })
370
- })
371
- ]
372
- });
373
- }
374
-
375
- // src/core/pages/DevErrorPage.tsx
376
- import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs3 } from "preact/jsx-runtime";
377
- function DevErrorPage(props) {
378
- var _a;
379
- const req = props.req;
380
- const err = props.error;
381
- const route = props.route;
382
- const routeParams = props.routeParams;
383
- let errMsg = String(err);
384
- if (err && err.stack) {
385
- errMsg = err.stack.replace(/\(.*node_modules/g, "(node_modules").replace(/at \/.*node_modules/g, "at node_modules");
386
- if ((_a = req.rootConfig) == null ? void 0 : _a.rootDir) {
387
- errMsg = errMsg.replaceAll(req.rootConfig.rootDir, "<root>");
388
- }
389
- if (process.env.HOME) {
390
- errMsg = errMsg.replaceAll(process.env.HOME, "$HOME");
391
- }
392
- }
393
- return /* @__PURE__ */ jsxs3(ErrorPage, {
394
- code: 500,
395
- title: "Root.js",
396
- children: [
397
- errMsg && /* @__PURE__ */ jsxs3(Fragment2, {
398
- children: [
399
- /* @__PURE__ */ jsx3("h2", {
400
- children: "Error"
401
- }),
402
- /* @__PURE__ */ jsx3("pre", {
403
- className: "box",
404
- children: /* @__PURE__ */ jsx3("code", {
405
- children: errMsg
406
- })
407
- })
408
- ]
409
- }),
410
- /* @__PURE__ */ jsx3("h2", {
411
- children: "Debug Info"
412
- }),
413
- /* @__PURE__ */ jsx3("pre", {
414
- className: "box",
415
- children: /* @__PURE__ */ jsx3("code", {
416
- children: `url: ${req.originalUrl}
417
- route: ${(route == null ? void 0 : route.src) || "null"}
418
- routeParams: ${routeParams && JSON.stringify(routeParams) || "null"}`
419
- })
420
- })
421
- ]
422
- });
423
- }
424
-
425
433
  // src/render/render.tsx
426
434
  import { jsx as jsx4, jsxs as jsxs4 } from "preact/jsx-runtime";
427
435
  var Renderer = class {
@@ -438,6 +446,9 @@ var Renderer = class {
438
446
  next();
439
447
  return;
440
448
  }
449
+ if (route.locale) {
450
+ routeParams.$locale = route.locale;
451
+ }
441
452
  const render404 = async () => {
442
453
  next();
443
454
  };
@@ -571,6 +582,9 @@ var Renderer = class {
571
582
  }
572
583
  async renderRoute(route, options) {
573
584
  const routeParams = options.routeParams;
585
+ if (route.locale) {
586
+ routeParams.$locale = route.locale;
587
+ }
574
588
  const Component = route.module.default;
575
589
  if (!Component) {
576
590
  throw new Error(
@@ -637,14 +651,24 @@ ${renderToString(page)}
637
651
  const Component = route.module.default;
638
652
  return this.renderComponent(Component, {}, { route, routeParams });
639
653
  }
640
- const mainHtml = renderToString(/* @__PURE__ */ jsx4(ErrorPage, {
641
- code: 404,
642
- title: "Not Found"
643
- }));
654
+ const mainHtml = renderToString(
655
+ /* @__PURE__ */ jsx4(ErrorPage, {
656
+ code: 404,
657
+ title: "Not found",
658
+ message: "Double-check the URL entered and try again.",
659
+ align: "center"
660
+ })
661
+ );
644
662
  const html = await this.renderHtml(mainHtml, {
645
- headComponents: [/* @__PURE__ */ jsx4("title", {
646
- children: "404"
647
- })]
663
+ headComponents: [
664
+ /* @__PURE__ */ jsx4("title", {
665
+ children: "404 Not Found"
666
+ }),
667
+ /* @__PURE__ */ jsx4("meta", {
668
+ name: "viewport",
669
+ content: "width=device-width, initial-scale=1.0"
670
+ })
671
+ ]
648
672
  });
649
673
  return { html };
650
674
  }
@@ -661,14 +685,21 @@ ${renderToString(page)}
661
685
  const mainHtml = renderToString(
662
686
  /* @__PURE__ */ jsx4(ErrorPage, {
663
687
  code: 500,
664
- title: "Error",
665
- message: "An unknown error occurred."
688
+ title: "Something went wrong",
689
+ message: "An unknown error occurred.",
690
+ align: "center"
666
691
  })
667
692
  );
668
693
  const html = await this.renderHtml(mainHtml, {
669
- headComponents: [/* @__PURE__ */ jsx4("title", {
670
- children: "500"
671
- })]
694
+ headComponents: [
695
+ /* @__PURE__ */ jsx4("title", {
696
+ children: "500 Error"
697
+ }),
698
+ /* @__PURE__ */ jsx4("meta", {
699
+ name: "viewport",
700
+ content: "width=device-width, initial-scale=1.0"
701
+ })
702
+ ]
672
703
  });
673
704
  return { html };
674
705
  }
@@ -682,7 +713,7 @@ ${renderToString(page)}
682
713
  );
683
714
  const html = await this.renderHtml(mainHtml, {
684
715
  headComponents: [/* @__PURE__ */ jsx4("title", {
685
- children: "404 | Root.js"
716
+ children: "404 Not found | Root.js"
686
717
  })]
687
718
  });
688
719
  return { html };
@@ -699,7 +730,7 @@ ${renderToString(page)}
699
730
  );
700
731
  const html = await this.renderHtml(mainHtml, {
701
732
  headComponents: [/* @__PURE__ */ jsx4("title", {
702
- children: "500 | Root.js"
733
+ children: "500 Error | Root.js"
703
734
  })]
704
735
  });
705
736
  return { html };