Repository: honojs/hono
File: src/adapter/service-worker/index.ts (TypeScript)

Code before:
 * Service Worker Adapter for Hono.
 * @module
 */
import type { Hono } from '../../hono'
import { handle } from './handler'

/**
 * Registers a Hono app to handle fetch events in a service worker.
 * This sets up `addEventListener('fetch', handle(app))` for the provided app.
 *
 * @param app - The Hono application instance
 * @example
 * ```ts
 * import { Hono } from 'hono'
 * import { fire } from 'hono/service-worker'
 *
 * const app = new Hono()
 *
 * app.get('/', (c) => c.text('Hi'))
 *
 * fire(app)
 * ```
 */
const fire = (app: Hono): void => {
  // @ts-expect-error addEventListener is not typed well
  addEventListener('fetch', handle(app))
}

export { handle, fire }


Review comment (suggestion):
Some of the use-cases discussed in the issue were leveraging the service worker adapter for additional environments ([cloudflare-workers](https://github.com/honojs/hono/issues/3127#issuecomment-2456912838) and webassembly [via componentizejs](https://github.com/bytecodealliance/ComponentizeJS?tab=readme-ov-file#using-starlingmonkeys-fetch-event)).

I don't know for sure for cloud-flare workers but I know for sure in the webassembly componentizejs environment that the [default fallback of `handle` to trigger a fetch](https://github.com/honojs/hono/blob/main/src/adapter/service-worker/handler.ts#L20) results in the wasm module making another fetch ([see discussion on issue](https://github.com/honojs/hono/issues/3127#issuecomment-2843529188)). That default makes sense in a service worker but not a web server.

If `fire` here is intended to replace the [base fire](https://github.com/honojs/hono/blob/c18c2b2e0992d15e530a69c7db6b9ccbd6f06e61/src/hono-base.ts#L506-L520) behavior (note does not fallback to fetch) / is for web server use cases I'd instead pass undefined explicitly like I had to do for wasm componentizejs use cases before switching to base fire (example from internal repo):
<img width="685" alt="image" src="https://github.com/user-attachments/assets/456ca010-cd07-42dc-aa8e-fafe00f7b0af" />

if `fire` should be used for both service worker and web server cases I'd make sure to passthrough the handle options and put a warning for web servers switching from `base fire` to `service worker fire` that the behavior is different and likely need to go from `fire()` to `fire({fetch: undefined})`. 

Code after:
 * Service Worker Adapter for Hono.
 * @module
 */
import type { Hono } from '../../hono'
import { handle } from './handler'
import type { HandleOptions } from './handler'

/**
 * Registers a Hono app to handle fetch events in a service worker.
 * This sets up `addEventListener('fetch', handle(app, options))` for the provided app.
 *
 * @param app - The Hono application instance
 * @param options - Options for handling requests (fetch defaults to undefined)
 * @example
 * ```ts
 * import { Hono } from 'hono'
 * import { fire } from 'hono/service-worker'
 *
 * const app = new Hono()
 *
 * app.get('/', (c) => c.text('Hi'))
 *
 * fire(app)
 * ```
 */
const fire = (
  app: Hono,
  options: HandleOptions = {
    fetch: undefined,
  }
): void => {
  // @ts-expect-error addEventListener is not typed well in ServiceWorker-like contexts, see: https://github.com/microsoft/TypeScript/issues/14877
  addEventListener('fetch', handle(app, options))
}

export { handle, fire }