From 8a303bdf9ed086b43b4c66b9fbce404666a111e1 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:31:22 +0000 Subject: [PATCH 1/4] fix: send POST requests past the PWA service worker Chromium can drop the unload beacon of a page that a service worker controls, so the UI of a reloaded page stays open on the server until the heartbeat timeout. The worker serves only GET requests, so a static route now sends POST requests to the network without involving it, in browsers that support static routing. Fixes #27586 --- .../com/vaadin/flow/server/frontend/sw.ts | 19 ++++++++++++++++ .../com/vaadin/flow/pwatest/ui/PwaTestIT.java | 22 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts b/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts index 2666f84c19c..1745d0537df 100644 --- a/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts +++ b/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts @@ -21,6 +21,25 @@ declare var self: ServiceWorkerGlobalScope & { // build's files from the precache while those pages still load from it. clientsClaim(); +// The worker only serves GET requests, but every request of a controlled page +// is dispatched to its fetch handler. Chromium can drop the beacon a page sends +// while it is reloaded, the one that closes its UI on the server, when that +// beacon has to pass through the worker. A static route sends POST requests, +// the UIDL and push requests among them, to the network without starting the +// worker, in the browsers that support static routing. +self.addEventListener('install', (event) => { + const routerEvent = event as ExtendableEvent & { + addRoutes?: (rules: Array<{ condition: { requestMethod: string }; source: 'network' }>) => Promise; + }; + if (routerEvent.addRoutes) { + // A rule the browser rejects only leaves POST requests going through the + // fetch handler as before, so it must not fail the installation. + event.waitUntil( + routerEvent.addRoutes([{ condition: { requestMethod: 'post' }, source: 'network' }]).catch(() => undefined) + ); + } +}); + declare var OFFLINE_PATH: string; // defined by Webpack/Vite declare global { diff --git a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java index d9fed20f6ba..3878957dbc6 100644 --- a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java +++ b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java @@ -188,6 +188,28 @@ private void assertAppShellStyleSheetHandling(String serviceWorkerJS, } } + @Test + public void uidlRequestBypassesServiceWorker() { + open(); + waitForServiceWorkerReady(); + // Reload so that the page is controlled by the worker from the start + // and its UIDL requests would be dispatched to it. + executeScript("window.location.reload();"); + waitUntil(driver -> Boolean.TRUE.equals(executeScript( + "return document.readyState === 'complete' && !!navigator.serviceWorker.controller"))); + // An unload beacon cannot be observed from a page, so the UIDL + // request of the reloaded page, a POST request as well, stands in for + // it. The matched source is empty when the fetch handler served it. + String sourceTypes = waitUntil(driver -> (String) executeScript( + "const types = performance.getEntriesByType('resource')" + + ".filter(e => e.name.includes('v-r=uidl'))" + + ".map(e => e.workerMatchedSourceType);" + + "return types.length ? JSON.stringify(types) : null;")); + Assert.assertEquals( + "UIDL requests should go to the network without the service worker", + "[\"network\"]", sourceTypes); + } + @Test public void testPwaResourcesOffline() { open(); From cf212ce10513ff4183b069b971d1c6ae5e97fda6 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:33:07 +0000 Subject: [PATCH 2/4] test: wait for reloaded page and document POST route of service worker --- .../vite-plugin-service-worker/ARCHITECTURE.md | 5 +++++ .../com/vaadin/flow/pwatest/ui/PwaTestIT.java | 15 +++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/flow-build-tools/src/main/resources/plugins/vite-plugin-service-worker/ARCHITECTURE.md b/flow-build-tools/src/main/resources/plugins/vite-plugin-service-worker/ARCHITECTURE.md index 96edbc8c201..c6aa7c19cb3 100644 --- a/flow-build-tools/src/main/resources/plugins/vite-plugin-service-worker/ARCHITECTURE.md +++ b/flow-build-tools/src/main/resources/plugins/vite-plugin-service-worker/ARCHITECTURE.md @@ -99,6 +99,11 @@ knows everything: route while offline. It is the only response made frameable by the same origin (`allowSameOriginFraming`), in case a proxy or security filter adds `X-Frame-Options: DENY` to everything (#25504). +- **POST requests** (UIDL, push and the unload beacon) go to the network + without reaching the worker, through a static route that the `install` + handler adds where the browser supports static routing. Chromium can + otherwise drop the unload beacon of a reloaded page, which leaves its UI + open on the server (#27586). - **Other requests** are served by the precache (cache-first), or network-first for `additionalNetworkFirstUrls`. In development mode, the same applies to `/VAADIN/*` on the server port. diff --git a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java index 3878957dbc6..683c4e82a7e 100644 --- a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java +++ b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java @@ -194,20 +194,19 @@ public void uidlRequestBypassesServiceWorker() { waitForServiceWorkerReady(); // Reload so that the page is controlled by the worker from the start // and its UIDL requests would be dispatched to it. - executeScript("window.location.reload();"); - waitUntil(driver -> Boolean.TRUE.equals(executeScript( - "return document.readyState === 'complete' && !!navigator.serviceWorker.controller"))); + getDriver().navigate().refresh(); // An unload beacon cannot be observed from a page, so the UIDL - // request of the reloaded page, a POST request as well, stands in for - // it. The matched source is empty when the fetch handler served it. + // requests of the reloaded page, POST requests as well, stand in for + // it. The matched source is empty when the fetch handler served one. String sourceTypes = waitUntil(driver -> (String) executeScript( "const types = performance.getEntriesByType('resource')" + ".filter(e => e.name.includes('v-r=uidl'))" + ".map(e => e.workerMatchedSourceType);" + "return types.length ? JSON.stringify(types) : null;")); - Assert.assertEquals( - "UIDL requests should go to the network without the service worker", - "[\"network\"]", sourceTypes); + Assert.assertTrue( + "UIDL requests should go to the network without the service worker, matched sources were: " + + sourceTypes, + sourceTypes.matches("\\[\"network\"(,\"network\")*\\]")); } @Test From 5c641f696e41c190783760279928f1213bc08ad7 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:56:19 +0000 Subject: [PATCH 3/4] test: report missing UIDL request instead of a bare timeout --- .../com/vaadin/flow/pwatest/ui/PwaTestIT.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java index 683c4e82a7e..70f65768b50 100644 --- a/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java +++ b/flow-tests/test-pwa/src/test/java/com/vaadin/flow/pwatest/ui/PwaTestIT.java @@ -34,6 +34,7 @@ import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.TimeoutException; import org.openqa.selenium.WebElement; import tools.jackson.databind.node.ObjectNode; @@ -198,11 +199,19 @@ public void uidlRequestBypassesServiceWorker() { // An unload beacon cannot be observed from a page, so the UIDL // requests of the reloaded page, POST requests as well, stand in for // it. The matched source is empty when the fetch handler served one. - String sourceTypes = waitUntil(driver -> (String) executeScript( - "const types = performance.getEntriesByType('resource')" - + ".filter(e => e.name.includes('v-r=uidl'))" - + ".map(e => e.workerMatchedSourceType);" - + "return types.length ? JSON.stringify(types) : null;")); + String sourceTypes = null; + try { + sourceTypes = waitUntil(driver -> (String) executeScript( + "const types = performance.getEntriesByType('resource')" + + ".filter(e => e.name.includes('v-r=uidl'))" + + ".map(e => e.workerMatchedSourceType);" + + "return types.length ? JSON.stringify(types) : null;")); + } catch (TimeoutException e) { + Assert.fail( + "Expected a UIDL request from the reloaded page, requested resources were: " + + executeScript( + "return performance.getEntriesByType('resource').map(e => e.name)")); + } Assert.assertTrue( "UIDL requests should go to the network without the service worker, matched sources were: " + sourceTypes, From 0ae8a5ca914333f3c141d31747b56f8b8e727a81 Mon Sep 17 00:00:00 2001 From: "totally-not-ai[bot]" <290682512+totally-not-ai[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:41:18 +0000 Subject: [PATCH 4/4] chore: write the HTTP method of the service worker route in upper case --- .../src/main/resources/com/vaadin/flow/server/frontend/sw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts b/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts index 1745d0537df..ede5faba215 100644 --- a/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts +++ b/flow-server/src/main/resources/com/vaadin/flow/server/frontend/sw.ts @@ -35,7 +35,7 @@ self.addEventListener('install', (event) => { // A rule the browser rejects only leaves POST requests going through the // fetch handler as before, so it must not fail the installation. event.waitUntil( - routerEvent.addRoutes([{ condition: { requestMethod: 'post' }, source: 'network' }]).catch(() => undefined) + routerEvent.addRoutes([{ condition: { requestMethod: 'POST' }, source: 'network' }]).catch(() => undefined) ); } });