Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@hono/vite-build] Support handlers other than fetch in Cloudflare Workers adapter #214

Open
toga4 opened this issue Jan 19, 2025 · 1 comment

Comments

@toga4
Copy link

toga4 commented Jan 19, 2025

I would like to use a scheduled handler alongside a fetch handler created with HonoX.
However, @hono/vite-build/cloudflare-workers currently only supports the fetch handler. This is because the current implementation wraps the Hono instance that is default-exported in the entry point with a new Hono instance.

const appStr = `const modules = import.meta.glob([${globStr}], { import: 'default', eager: true })
let added = false
for (const [, app] of Object.entries(modules)) {
if (app) {
mainApp.route('/', app)
mainApp.notFound((c) => {
let executionCtx
try {
executionCtx = c.executionCtx
} catch {}
return app.fetch(c.req.raw, c.env, executionCtx)
})
added = true
}
}
if (!added) {
throw new Error("Can't import modules from [${globStr}]")
}`
const mainAppStr = `import { Hono } from 'hono'
const mainApp = new Hono()
${await hooksToString('mainApp', options.entryContentBeforeHooks)}
${appStr}
${await hooksToString('mainApp', options.entryContentAfterHooks)}
export default mainApp`

This makes difficult to use handlers other than fetch in Cloudflare Workers.

As shown in the Hono documentation, it would be better if the following implementation could be supported in the entry point:

const app = createApp()

export default {
  fetch: app.fetch,
  scheduled: async (batch, env) => {},
}

Furthermore, if the object that is default-exported in the entry point is directly default-exported in the built output file, it would make the behavior more intuitive.

What do you think?

@toga4
Copy link
Author

toga4 commented Jan 26, 2025

As a workaround, I was able to implement an additional handler using entryContentAfterHooks in vite.config.ts.

import { defineConfig } from 'vite'
import build from '@hono/vite-build/cloudflare-workers'

export default defineConfig({
  plugins: [
    build({
      entryContentAfterHooks: [
        (content) => {
          return `const handlerModules = import.meta.glob('/app/handlers.ts', { eager: true })
            for (const [, handlers] of Object.entries(handlerModules)) {
              if (handlers) {
                for (const [name, handler] of Object.entries(handlers)) {
                  ${content}[name] = handler
                }
              }
            }`
        },
      ],
    })
  ],
})

It seems that the current implementation of getEntryContent is designed to support multiple entry points.
However, handlers other than fetch are not Hono instances, which means that only one of them can be defined. This creates a conflict with the intended functionality of getEntryContent.

What do you think about supporting an additional entry point specifically for Cloudflare Workers handlers, as shown in the workaround above?
For example, vite.config.ts could specify an additional entry point for handlers like this:

import { defineConfig } from 'vite'
import build from '@hono/vite-build/cloudflare-workers'

export default defineConfig({
  plugins: [
    build({
      entry: ['src/index.ts','./src/index.tsx','./app/server.ts'],
      handlersEntry: './app/handlers.ts',
    }),
  ],
})

And additional entry point could look like this:

import type { Env } from 'hono'

export const scheduled: ExportedHandlerScheduledHandler<Env['Bindings']> = async (controller, env, ctx) => {
  console.log('Scheduled', controller, env, ctx)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant