Skip to content

Commit 79da11a

Browse files
committed
Disable the dev service worker whenever Playwright drives the browser so dynamic navigations hit the real pages instead of the offline fallback.
1 parent c181408 commit 79da11a

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎@types/window.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ declare global {
5252
*/
5353
isPlaywrightControlled?: boolean
5454

55+
/**
56+
* Allows Playwright tests to disable PWA service worker registration
57+
*/
58+
__disableServiceWorkerForE2E?: boolean
59+
5560
/**
5661
* Snapshot of environment-client results for Playwright assertions
5762
*/

‎src/components/Pwa/ServiceWorker/client/index.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ export const registerServiceWorker = () => {
99
return
1010
}
1111

12+
const disableForE2E = typeof window !== 'undefined' && window.__disableServiceWorkerForE2E === true
13+
if (disableForE2E) {
14+
console.info('[pwa] skipping service worker registration for e2e tests')
15+
void navigator.serviceWorker.getRegistrations().then((registrations) => {
16+
registrations.forEach((registration) => {
17+
console.info('[pwa] unregistering service worker (e2e disable)', { scope: registration.scope })
18+
void registration.unregister()
19+
})
20+
})
21+
return
22+
}
23+
1224
const devMode = isDev()
1325
const allowDevServiceWorker = devMode && isE2eTest()
1426

‎test/e2e/helpers/pageObjectModels/BasePage.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class BasePage {
3636
protected static async setupPlaywrightGlobals(page: Page): Promise<void> {
3737
await page.addInitScript(() => {
3838
window.isPlaywrightControlled = true
39+
if (typeof window.__disableServiceWorkerForE2E === 'undefined') {
40+
window.__disableServiceWorkerForE2E = true
41+
}
3942

4043
if (typeof window.__astroPageLoadCounter !== 'number') {
4144
window.__astroPageLoadCounter = 0

0 commit comments

Comments
 (0)