@lolyjs/core 0.2.0-alpha.32 → 0.2.0-alpha.34

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 CHANGED
@@ -37,6 +37,7 @@ Loly is a full-stack React framework that combines the simplicity of file-based
37
37
 
38
38
  - 🔌 **Native WebSocket Support** - Built-in Socket.IO integration with automatic namespace routing
39
39
  - 🎯 **Route-Level Middlewares** - Define middlewares directly in your routes for pages and APIs
40
+ - 🌐 **Global Middlewares** - Middlewares that always execute (SSR and SPA) to establish consistent context
40
41
  - 📁 **Separation of Concerns** - Server logic in `page.server.hook.ts` and `layout.server.hook.ts` separate from React components
41
42
  - 🚀 **Hybrid Rendering** - SSR, SSG, and CSR with streaming support
42
43
  - 🛡️ **Security First** - Built-in rate limiting, validation, sanitization, and security headers
@@ -262,6 +263,65 @@ export async function POST(ctx: ApiContext) {
262
263
  - Method-specific middlewares for APIs
263
264
  - Clean separation of concerns
264
265
 
266
+ ### 🌐 Global Middlewares
267
+
268
+ Global middlewares always execute on every request (both SSR initial load and SPA client-side navigation). They establish context (`ctx.locals`) that is consistently available across your application.
269
+
270
+ Create `global.middleware.ts` in your project root:
271
+
272
+ ```tsx
273
+ // global.middleware.ts (in project root)
274
+ import type { GlobalMiddleware } from "@lolyjs/core";
275
+
276
+ export const globalMiddlewares: GlobalMiddleware[] = [
277
+ async (ctx, next) => {
278
+ // Establish context (e.g., session, user, tenant, locale)
279
+ // This runs on EVERY request (SSR and SPA navigation)
280
+ const sessionCookie = ctx.req.cookies?.session;
281
+ if (sessionCookie) {
282
+ ctx.locals.user = await getSession(sessionCookie);
283
+ ctx.locals.session = { id: sessionCookie };
284
+ } else {
285
+ ctx.locals.user = null;
286
+ ctx.locals.session = null;
287
+ }
288
+
289
+ await next();
290
+ },
291
+ ];
292
+ ```
293
+
294
+ **Important:** Global middlewares should only establish context (set `ctx.locals`), not make routing decisions. Routing decisions should be made in layout/page hooks.
295
+
296
+ **Use Cases:**
297
+
298
+ - Session/Authentication context
299
+ - Tenant/Multi-tenancy context
300
+ - Locale/Language detection
301
+ - Feature flags based on user
302
+ - Request tracing/logging
303
+
304
+ **Example with Protected Routes:**
305
+
306
+ ```tsx
307
+ // global.middleware.ts - Establishes user context
308
+ export const globalMiddlewares: GlobalMiddleware[] = [
309
+ async (ctx, next) => {
310
+ ctx.locals.user = await getSession(ctx.req);
311
+ await next();
312
+ },
313
+ ];
314
+
315
+ // app/dashboard/layout.server.hook.ts - Makes routing decision
316
+ export const getServerSideProps: ServerLoader = async (ctx) => {
317
+ // ctx.locals.user is already available from global middleware
318
+ if (!ctx.locals.user) {
319
+ return ctx.Redirect("/login", false);
320
+ }
321
+ return { props: { user: ctx.locals.user } };
322
+ };
323
+ ```
324
+
265
325
  ### 📁 File-Based Routing
266
326
 
267
327
  Routes are automatically created from your file structure:
@@ -1064,6 +1124,7 @@ your-app/
1064
1124
  ├── lib/ # Utilities
1065
1125
  ├── public/ # Static files (served at root: /sitemap.xml, /robots.txt, etc.)
1066
1126
  ├── loly.config.ts # Framework configuration
1127
+ ├── global.middleware.ts # Global middlewares (always execute)
1067
1128
  ├── init.server.ts # Server initialization (DB, services, etc.)
1068
1129
  └── package.json
1069
1130
  ```
@@ -1362,6 +1423,7 @@ import type {
1362
1423
  ApiContext,
1363
1424
  WssContext,
1364
1425
  RouteMiddleware,
1426
+ GlobalMiddleware,
1365
1427
  ApiMiddleware,
1366
1428
  GenerateStaticParams,
1367
1429
  } from "@lolyjs/core";
@@ -1411,6 +1473,7 @@ import type {
1411
1473
  ApiContext,
1412
1474
  WssContext,
1413
1475
  RouteMiddleware,
1476
+ GlobalMiddleware,
1414
1477
  ApiMiddleware,
1415
1478
  GenerateStaticParams,
1416
1479
  } from "@lolyjs/core";