-
-
Notifications
You must be signed in to change notification settings - Fork 917
Description
What version of Hono are you using?
4.11.3
What runtime/platform is your app running on? (with version if possible)
Service Worker
What steps can reproduce the bug?
-
If I use
handle(app)in fetch event handler in service worker and when my app returns 404, it seemshandle(app)automatically fetches original request again. This is the default behavior becauseopts.fetchis defined by default. I couldn't find about this behavior (and how to disable this) in the documentation. (sorry if I was overlooking)
hono/src/adapter/service-worker/handler.ts
Lines 18 to 23 in 28452f0
export const handle = <E extends Env, S extends Schema, BasePath extends string>( app: Hono<E, S, BasePath>, opts: HandleOptions = { // To use `fetch` on a Service Worker correctly, bind it to `globalThis`. fetch: globalThis.fetch.bind(globalThis), }
hono/src/adapter/service-worker/handler.ts
Lines 30 to 32 in 28452f0
if (opts.fetch && res.status === 404) { return await opts.fetch(evt.request) } -
If I use
fire(app)instead, it won't do that by default, because default value of options.fetch isundefinedunlikehandle(app). The documentation says "Thefire()function automatically callsaddEventListener('fetch', handle(app))for you," but that's not entirely correct.
hono/src/adapter/service-worker/index.ts
Lines 28 to 36 in 28452f0
const fire = <E extends Env, S extends Schema, BasePath extends string>( app: Hono<E, S, BasePath>, 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)) }
What is the expected behavior?
I'm not sure which behavior is more suitable for most users, but I think it would be better to have one consistent (though it might be a breaking change...) and it should be documented.
What do you see instead?
No response
Additional information
No response