webspresso 0.0.28 → 0.0.30
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/README.md +0 -78
- package/package.json +1 -1
- package/src/server.js +35 -17
package/README.md
CHANGED
|
@@ -589,84 +589,6 @@ function myPluginFactory(options = {}) {
|
|
|
589
589
|
}
|
|
590
590
|
```
|
|
591
591
|
|
|
592
|
-
### Script Injection
|
|
593
|
-
|
|
594
|
-
Plugins can inject content into templates dynamically:
|
|
595
|
-
|
|
596
|
-
```javascript
|
|
597
|
-
const myPlugin = {
|
|
598
|
-
name: 'my-plugin',
|
|
599
|
-
version: '1.0.0',
|
|
600
|
-
|
|
601
|
-
register(ctx) {
|
|
602
|
-
// Inject into <head> section
|
|
603
|
-
ctx.injectHead('<meta name="my-plugin" content="enabled">', { priority: 10 });
|
|
604
|
-
|
|
605
|
-
// Inject at end of <body>
|
|
606
|
-
ctx.injectBody('<script src="/my-plugin.js"></script>');
|
|
607
|
-
|
|
608
|
-
// Inject CSS styles
|
|
609
|
-
ctx.injectStyle(`
|
|
610
|
-
.my-plugin-widget { display: block; }
|
|
611
|
-
`);
|
|
612
|
-
|
|
613
|
-
// Register link for dev toolbar
|
|
614
|
-
ctx.registerDevLink({
|
|
615
|
-
name: 'My Plugin',
|
|
616
|
-
path: '/my-plugin',
|
|
617
|
-
icon: '🔌',
|
|
618
|
-
description: 'My plugin dashboard'
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
};
|
|
622
|
-
```
|
|
623
|
-
|
|
624
|
-
**Template Usage:**
|
|
625
|
-
|
|
626
|
-
```njk
|
|
627
|
-
<head>
|
|
628
|
-
{# Injected head content from plugins #}
|
|
629
|
-
{{ fsy.injectHead() | safe }}
|
|
630
|
-
</head>
|
|
631
|
-
<body>
|
|
632
|
-
...
|
|
633
|
-
{# Injected body content from plugins #}
|
|
634
|
-
{{ fsy.injectBody() | safe }}
|
|
635
|
-
|
|
636
|
-
{# Dev toolbar (only in development) #}
|
|
637
|
-
{{ fsy.devToolbar() | safe }}
|
|
638
|
-
</body>
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
### Dev Toolbar
|
|
642
|
-
|
|
643
|
-
A development toolbar appears at the bottom of pages in development mode:
|
|
644
|
-
|
|
645
|
-
- **Quick Links**: Dashboard, Admin Panel, Schema Explorer
|
|
646
|
-
- **Plugin Links**: Plugins can register custom links
|
|
647
|
-
- **Hover to Expand**: Minimized by default, expands on hover
|
|
648
|
-
- **Auto-hidden**: Not shown in production mode
|
|
649
|
-
|
|
650
|
-
```javascript
|
|
651
|
-
// Register custom link from plugin
|
|
652
|
-
ctx.registerDevLink({
|
|
653
|
-
name: 'API Docs',
|
|
654
|
-
path: '/docs/api',
|
|
655
|
-
icon: '📚',
|
|
656
|
-
description: 'API Documentation'
|
|
657
|
-
});
|
|
658
|
-
```
|
|
659
|
-
|
|
660
|
-
**Custom Links in Templates:**
|
|
661
|
-
|
|
662
|
-
```njk
|
|
663
|
-
{{ fsy.devToolbar({
|
|
664
|
-
customLinks: [
|
|
665
|
-
{ name: 'Logs', path: '/logs', icon: '📋' }
|
|
666
|
-
]
|
|
667
|
-
}) | safe }}
|
|
668
|
-
```
|
|
669
|
-
|
|
670
592
|
## File-Based Routing
|
|
671
593
|
|
|
672
594
|
### SSR Pages
|
package/package.json
CHANGED
package/src/server.js
CHANGED
|
@@ -9,7 +9,7 @@ const nunjucks = require('nunjucks');
|
|
|
9
9
|
const timeout = require('connect-timeout');
|
|
10
10
|
|
|
11
11
|
const { mountPages } = require('./file-router');
|
|
12
|
-
const { configureAssets } = require('./helpers');
|
|
12
|
+
const { configureAssets, createHelpers, getScriptInjector } = require('./helpers');
|
|
13
13
|
const { createPluginManager } = require('./plugin-manager');
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -322,22 +322,42 @@ function createApp(options = {}) {
|
|
|
322
322
|
}
|
|
323
323
|
}
|
|
324
324
|
|
|
325
|
+
// Helper to create error page context with fsy
|
|
326
|
+
function createErrorContext(req, extraData = {}) {
|
|
327
|
+
const baseUrl = process.env.BASE_URL || `http://localhost:${process.env.PORT || 3000}`;
|
|
328
|
+
const locale = req.query?.lang || process.env.DEFAULT_LOCALE || 'en';
|
|
329
|
+
|
|
330
|
+
// Create fsy helpers
|
|
331
|
+
const fsy = createHelpers({ req, res: {}, baseUrl, locale });
|
|
332
|
+
|
|
333
|
+
// Merge plugin helpers
|
|
334
|
+
const pluginHelpers = pluginManager.getHelpers();
|
|
335
|
+
Object.assign(fsy, pluginHelpers);
|
|
336
|
+
|
|
337
|
+
return {
|
|
338
|
+
fsy,
|
|
339
|
+
locale,
|
|
340
|
+
isDev,
|
|
341
|
+
url: req.url,
|
|
342
|
+
method: req.method,
|
|
343
|
+
...extraData
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
325
347
|
// 404 handler
|
|
326
348
|
app.use((req, res) => {
|
|
327
349
|
res.status(404);
|
|
350
|
+
const ctx = createErrorContext(req);
|
|
328
351
|
|
|
329
352
|
// Custom handler function
|
|
330
353
|
if (typeof errorPages.notFound === 'function') {
|
|
331
|
-
return errorPages.notFound(req, res);
|
|
354
|
+
return errorPages.notFound(req, res, ctx);
|
|
332
355
|
}
|
|
333
356
|
|
|
334
357
|
// Custom template
|
|
335
358
|
if (typeof errorPages.notFound === 'string') {
|
|
336
359
|
try {
|
|
337
|
-
const html = nunjucksEnv.render(errorPages.notFound,
|
|
338
|
-
url: req.url,
|
|
339
|
-
method: req.method
|
|
340
|
-
});
|
|
360
|
+
const html = nunjucksEnv.render(errorPages.notFound, ctx);
|
|
341
361
|
return res.send(html);
|
|
342
362
|
} catch (e) {
|
|
343
363
|
console.error('Error rendering 404 template:', e);
|
|
@@ -358,19 +378,17 @@ function createApp(options = {}) {
|
|
|
358
378
|
if (req.timedout) {
|
|
359
379
|
console.error('Request timed out:', req.method, req.url);
|
|
360
380
|
res.status(503);
|
|
381
|
+
const ctx = createErrorContext(req);
|
|
361
382
|
|
|
362
383
|
// Custom timeout handler
|
|
363
384
|
if (typeof errorPages.timeout === 'function') {
|
|
364
|
-
return errorPages.timeout(req, res);
|
|
385
|
+
return errorPages.timeout(req, res, ctx);
|
|
365
386
|
}
|
|
366
387
|
|
|
367
388
|
// Custom timeout template
|
|
368
389
|
if (typeof errorPages.timeout === 'string') {
|
|
369
390
|
try {
|
|
370
|
-
const html = nunjucksEnv.render(errorPages.timeout,
|
|
371
|
-
url: req.url,
|
|
372
|
-
method: req.method
|
|
373
|
-
});
|
|
391
|
+
const html = nunjucksEnv.render(errorPages.timeout, ctx);
|
|
374
392
|
return res.send(html);
|
|
375
393
|
} catch (e) {
|
|
376
394
|
console.error('Error rendering timeout template:', e);
|
|
@@ -387,20 +405,20 @@ function createApp(options = {}) {
|
|
|
387
405
|
|
|
388
406
|
console.error('Server error:', err);
|
|
389
407
|
res.status(err.status || 500);
|
|
408
|
+
const ctx = createErrorContext(req, {
|
|
409
|
+
error: isDev ? err : { message: 'Internal Server Error' },
|
|
410
|
+
status: err.status || 500
|
|
411
|
+
});
|
|
390
412
|
|
|
391
413
|
// Custom handler function
|
|
392
414
|
if (typeof errorPages.serverError === 'function') {
|
|
393
|
-
return errorPages.serverError(err, req, res);
|
|
415
|
+
return errorPages.serverError(err, req, res, ctx);
|
|
394
416
|
}
|
|
395
417
|
|
|
396
418
|
// Custom template
|
|
397
419
|
if (typeof errorPages.serverError === 'string') {
|
|
398
420
|
try {
|
|
399
|
-
const html = nunjucksEnv.render(errorPages.serverError,
|
|
400
|
-
error: isDev ? err : { message: 'Internal Server Error' },
|
|
401
|
-
status: err.status || 500,
|
|
402
|
-
isDev
|
|
403
|
-
});
|
|
421
|
+
const html = nunjucksEnv.render(errorPages.serverError, ctx);
|
|
404
422
|
return res.send(html);
|
|
405
423
|
} catch (e) {
|
|
406
424
|
console.error('Error rendering 500 template:', e);
|