Repository navigation
feat: Introduce Service Worker Adapter - #3062
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #3062 +/- ##
==========================================
+ Coverage 95.87% 96.12% +0.25%
==========================================
Files 137 147 +10
Lines 13460 14795 +1335
Branches 2295 2602 +307
==========================================
+ Hits 12905 14222 +1317
- Misses 555 573 +18 â View full report in Codecov by Sentry. |
|
Sounds good! I'll try it later. |
|
Hi @nakasyou I've noticed that this adapter is so important! If we have this adapter, we can obsolete the // src/hono-base.ts
fire = (): void => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
addEventListener('fetch', (event: FetchEventLike): void => {
event.respondWith(this.dispatch(event.request, event, undefined, event.request.method))
})
}I think it's okay to delete it completely because this service worker style is now used only for Fastly Compute and Service Worker. |
|
@yusukebe It sounds good! |
Yeah. I'll consider it. |
|
Hi @nakasyou ! I've created the demo project to try this adapter: https://github.com/yusukebe/service-worker-adapter-demo And I'll suggest the code, though I'm not sure this is 100% good: diff --git a/src/adapter/service-worker/handler.ts b/src/adapter/service-worker/handler.ts
index 2c85343..b8801e3 100644
--- a/src/adapter/service-worker/handler.ts
+++ b/src/adapter/service-worker/handler.ts
@@ -1,12 +1,12 @@
+/// <reference lib="webworker" />
/**
* Handler for Service Worker
* @module
*/
import type { Hono } from '../../hono'
-import type { FetchEventLike } from '../../types'
-type Handler = (evt: FetchEventLike) => void
+type Handler = (evt: FetchEvent) => void
/**
* Adapter for Service Worker
@@ -14,24 +14,16 @@ type Handler = (evt: FetchEventLike) => void
export const handle = (
app: Hono,
opts: {
- fetch: (req: Request) => Promise<Response>
+ fetch: typeof fetch | undefined
} = {
fetch: fetch,
}
): Handler => {
- return (evt) => {
- const fetched = app.fetch(evt.request)
- if (fetched instanceof Response && fetched.status === 404) {
- return
+ return async (evt) => {
+ let res = await app.fetch(evt.request)
+ if (res.status === 404 && opts.fetch) {
+ res = await opts.fetch(evt.request)
}
- evt.respondWith(
- (async () => {
- const res = await fetched
- if (res.status === 404) {
- return await opts.fetch(evt.request)
- }
- return res
- })()
- )
+ evt.respondWith(res)
}
}There are two points:
Plus, with this code, you can pass through the handling 404 by assigning addEventListener(
'fetch',
handle(app, {
fetch: undefined
})
)What do you think of them? |
|
Hi @yusukebe, I agree for your comment. |
|
Hi @yusukebe, I have an issue at TypeScript. It is made by webworker types. |
|
How about defining types ourselves instead of referring to lib interface ExtendableEvent extends Event {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
waitUntil(f: Promise<any>): void
}
export interface FetchEvent extends ExtendableEvent {
readonly clientId: string
readonly handled: Promise<undefined>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly preloadResponse: Promise<any>
readonly request: Request
readonly resultingClientId: string
respondWith(r: Response | PromiseLike<Response>): void
} |
|
Awesome work! I'll merge this into the |
I added adapter for Service Worker on browser.
I think that Hono is good for creating Service Worker.
In the PR, you can write code like that:
When app responses 404, adapter fetches the original server.
The author should do the following, if applicable
bun run format:fix && bun run lint:fixto format the code