diff --git a/index.js b/index.js index fa1bb0a..a55b06d 100644 --- a/index.js +++ b/index.js @@ -123,14 +123,34 @@ function bootstrapWebsocketHandler(route) { if (!wsRoutes[routeWithPrefix(route)]) { // initialize ws router. future updates will only require updating // the wsRoutes dict, not create a whole new route - wsRouter.ws(routeWithPrefix(route), (req, res) => { - return wsRoutes[routeWithPrefix(route)](req, res) + wsRouter.ws(routeWithPrefix(route), (ws, req) => { + ws.render = (template, context) => { + context = context || {}; + context.route = function (url) { + return __urlPrefix ? path.join(__urlPrefix, url) : url; + }; + const eta = model.getTemplater(route.structure_id); + const structure = model.getStructure(db, route.structure_id); + + ws.send( + bootstrapTemplateWithHTMXetc( + eta.render(template, context), + `/workshop/${route.structure_id}/route/${route.id}`, + `/workshop/${route.structure_id}/clone_modal/`, + route.verb == "GET" ? embedHTML(req.originalUrl) : null, + structure.head_injection, + htmxRequest = true, + ), + ); + }; + + return wsRoutes[routeWithPrefix(route)](ws, req) }); } - wsRoutes[routeWithPrefix(route)] = (req, res) => { + wsRoutes[routeWithPrefix(route)] = (ws, req) => { try { - return handler(req, res) + return handler(ws, req) } catch (e) { const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; model.createLog(db, route.structure_id, route.id, error, true);