register middleware early

This commit is contained in:
Chris Wanstrath 2026-01-28 10:56:22 -08:00
parent c054ca1f82
commit 85fa79518c

View File

@ -37,12 +37,14 @@ export class Hype<
BasePath extends string = '/' BasePath extends string = '/'
> extends Hono<E, S, BasePath> { > extends Hono<E, S, BasePath> {
props: HypeProps props: HypeProps
middlewareRegistered = false
routesRegistered = false routesRegistered = false
constructor(props?: HypeProps & ConstructorParameters<typeof Hono<E, S, BasePath>>[0]) { constructor(props?: HypeProps & ConstructorParameters<typeof Hono<E, S, BasePath>>[0]) {
super(props) super(props)
this.props = props ?? {} this.props = props ?? {}
this.registerMiddleware()
} }
sse(path: string, handler: SSEHandler<E>) { sse(path: string, handler: SSEHandler<E>) {
@ -68,9 +70,9 @@ export class Hype<
}) })
} }
registerRoutes() { registerMiddleware() {
if (this.routesRegistered) return if (this.middlewareRegistered) return
this.routesRegistered = true this.middlewareRegistered = true
// static assets in pub/ // static assets in pub/
this.use('/*', serveStatic({ root: './pub' })) this.use('/*', serveStatic({ root: './pub' }))
@ -120,6 +122,11 @@ export class Hype<
c.res = new Response(formatted, c.res) c.res = new Response(formatted, c.res)
}) })
} }
}
registerRoutes() {
if (this.routesRegistered) return
this.routesRegistered = true
// serve frontend js // serve frontend js
this.use('*', async (c, next) => { this.use('*', async (c, next) => {