Skip to content

Commit

Permalink
[ServiceWorker] Refine error message and fix lint warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Neet-Nestor committed May 23, 2024
1 parent f228d05 commit 3c600d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type RequestKind =
| "keepAlive"
| "heartbeat";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ResponseKind =
| "return"
| "throw"
Expand Down
20 changes: 14 additions & 6 deletions src/service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,21 @@ export async function CreateServiceWorkerMLCEngine(
engineConfig?: MLCEngineConfig,
): Promise<ServiceWorkerMLCEngine> {
if (!("serviceWorker" in navigator)) {
throw new Error("Service worker API is not available");
throw new Error(
"Service worker API is not available in your browser. Please ensure that your browser supports service workers and that you are using a secure context (HTTPS). " +
"Check the browser compatibility and ensure that service workers are not disabled in your browser settings.",
);
}
const registration = await (navigator.serviceWorker as ServiceWorkerContainer)
.ready;
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(
registration.active!,
);
const serviceWorkerAPI = navigator.serviceWorker as ServiceWorkerContainer;
const registration = await serviceWorkerAPI.ready;
const serviceWorker = registration.active || serviceWorkerAPI.controller;
if (!serviceWorker) {
throw new Error(
"Service worker failed to initialize. This could be due to a failure in the service worker registration process or because the service worker is not active. " +
"Please refresh the page to retry initializing the service worker.",
);
}
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(serviceWorker);
serviceWorkerMLCEngine.setInitProgressCallback(
engineConfig?.initProgressCallback,
);
Expand Down

0 comments on commit 3c600d6

Please sign in to comment.