diff --git a/src/components/FrontendToolsComponent.ts b/src/components/FrontendToolsComponent.ts index fb81432..1ae09f7 100644 --- a/src/components/FrontendToolsComponent.ts +++ b/src/components/FrontendToolsComponent.ts @@ -46,6 +46,16 @@ export default class FrontendToolsComponent extends ApplicationComponent { next(); }); + + // Add request context locals + router.use((req, res, next) => { + res.locals.url = req.url; + res.locals.params = req.params; + res.locals.query = req.query; + res.locals.body = req.body; + + next(); + }); } public async preCompileViews(watch: boolean): Promise { diff --git a/src/components/NunjucksComponent.ts b/src/components/NunjucksComponent.ts index 102a346..d494135 100644 --- a/src/components/NunjucksComponent.ts +++ b/src/components/NunjucksComponent.ts @@ -56,31 +56,8 @@ export default class NunjucksComponent extends ApplicationComponent { }); } - public async init(_router: Router): Promise { - this.use(NunjucksMiddleware); - } - public getEnvironment(): Environment { if (!this.environment) throw new Error('Environment not initialized.'); return this.environment; } } - -export class NunjucksMiddleware extends Middleware { - private env?: Environment; - - protected async handle(req: Request, res: Response, next: NextFunction): Promise { - this.env = this.app.as(NunjucksComponent).getEnvironment(); - res.locals.url = req.url; - res.locals.params = req.params; - res.locals.query = req.query; - res.locals.body = req.body; - - next(); - } - - public getEnvironment(): Environment { - if (!this.env) throw new Error('Environment not initialized.'); - return this.env; - } -}