Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions public/offline.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ <h1 id="offline-title">You're offline</h1>
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);
Expand Down
18 changes: 14 additions & 4 deletions tests/pwa-reconnect.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<script>([\s\S]*?)<\/script>/)?.[1];
assert.ok(script, 'offline page must contain a self-contained reconnect script');
Expand Down Expand Up @@ -35,16 +35,18 @@ async function offlinePageHarness({ reachable = false, hidden = false, validMani
const window = {
location: {
origin: 'https://preview.radio.eplus.dev',
pathname: '/pr-preview/pr-24/asia/viet-nam/station',
reload: () => { reloadCount += 1; },
},
addEventListener: (name, listener) => windowEvents.set(name, listener),
};
const navigator = {
language: 'vi-VN',
serviceWorker: {
getRegistration: async () => ({
scope: 'https://preview.radio.eplus.dev/pr-preview/pr-24/',
}),
getRegistration: async () => {
if (serviceWorkerFails) throw new Error('Service worker lookup blocked');
return { scope: 'https://preview.radio.eplus.dev/pr-preview/pr-24/' };
},
},
};

Expand Down Expand Up @@ -152,3 +154,11 @@ test('PWA offline shell suspends probing when hidden and checks on foreground',
await page.flush();
assert.equal(page.reloadCount, 1);
});

test('PWA offline shell survives denied service-worker lookup and retains preview scope', async () => {
const page = await offlinePageHarness({ reachable: true, serviceWorkerFails: true });
await page.flush();
assert.equal(page.reloadCount, 1);
assert.equal(page.requests[0].url,
'https://preview.radio.eplus.dev/pr-preview/pr-24/brand/site.webmanifest');
});
Loading