web-llm icon indicating copy to clipboard operation
web-llm copied to clipboard

[ServiceWorker][Feature] Fix service worker keep alive using "fetch" events

Open Neet-Nestor opened this issue 9 months ago • 0 comments

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()
  • Rename web_service_worker.ts to service_worker.ts and service_worker.ts to extension_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 into RequestKind and ResponseKind sub-types

Testing

https://chat.webllm.ai/

The chat webapp is able to correctly keeping service worker alive after this change.

Neet-Nestor avatar May 17 '24 08:05 Neet-Nestor