Skip to content

The default behavior on 404 in handle() and fire() seems inconsistent and not documented #4632

@na-trium-144

Description

@na-trium-144

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?

  1. If I use handle(app) in fetch event handler in service worker and when my app returns 404, it seems handle(app) automatically fetches original request again. This is the default behavior because opts.fetch is defined by default. I couldn't find about this behavior (and how to disable this) in the documentation. (sorry if I was overlooking)

    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),
    }

    if (opts.fetch && res.status === 404) {
    return await opts.fetch(evt.request)
    }

  2. If I use fire(app) instead, it won't do that by default, because default value of options.fetch is undefined unlike handle(app). The documentation says "The fire() function automatically calls addEventListener('fetch', handle(app)) for you," but that's not entirely correct.

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions