diff --git a/public/offline.html b/public/offline.html
index 7b43d2ce..78fee9ea 100644
--- a/public/offline.html
+++ b/public/offline.html
@@ -93,8 +93,16 @@
You're offline
try {
// The active worker gives us the correct deployment scope for deep
// links and /pr-preview/pr-N/ paths, not just the production root.
- const registration = await navigator.serviceWorker?.getRegistration?.();
- const scope = registration?.scope || new URL('/', window.location.origin).href;
+ let registration;
+ try {
+ registration = await navigator.serviceWorker?.getRegistration?.();
+ } catch {
+ // A blocked or unavailable service-worker API must not prevent
+ // recovery when the website is already reachable again.
+ }
+ const previewPath = window.location.pathname.match(/^\/pr-preview\/pr-[1-9]\d*\//)?.[0];
+ const fallbackScope = new URL(previewPath || '/', window.location.origin).href;
+ const scope = registration?.scope || fallbackScope;
// A non-navigation request bypasses our navigation-only offline
// fallback. Require a real, valid manifest before reloading.
const probeUrl = new URL('brand/site.webmanifest', scope);
diff --git a/tests/pwa-reconnect.test.mjs b/tests/pwa-reconnect.test.mjs
index 05e184b0..ef25d466 100644
--- a/tests/pwa-reconnect.test.mjs
+++ b/tests/pwa-reconnect.test.mjs
@@ -5,7 +5,7 @@ import { runInNewContext } from 'node:vm';
const read = path => readFile(new URL('../' + path, import.meta.url), 'utf8');
-async function offlinePageHarness({ reachable = false, hidden = false, validManifest = true } = {}) {
+async function offlinePageHarness({ reachable = false, hidden = false, validManifest = true, serviceWorkerFails = false } = {}) {
const html = await read('public/offline.html');
const script = html.match(/