web-llm
web-llm copied to clipboard
[ServiceWorker][Feature] Fix service worker keep alive using "fetch" events
Overview
This PR fixes the keep-alive mechanism of service worker by periodically fetching a dummy file following as suggested by the following discussions. https://stackoverflow.com/questions/29741922/prevent-service-worker-from-automatically-stopping
This PR also does some refactor for service workers files.
Primary Change
- ServiceWorkerEngine
- Add
missedHeatbeat
count so client can detect when service worker is stopped - Add fetching dummy file machanism in
keepAlive()
- Add
- Rename
web_service_worker.ts
toservice_worker.ts
andservice_worker.ts
toextension_service_worker.ts
. The export names are kept the same for now for not being a break change. - Update
examples/service-worker
to show how to keep service worker alive - Break type
RequestMessage
intoRequestKind
andResponseKind
sub-types
Testing
https://chat.webllm.ai/
The chat webapp is able to correctly keeping service worker alive after this change.