Skip to content

fix: send POST requests past the PWA service worker - #27588

Open
totally-not-ai[bot] wants to merge 6 commits into
mainfrom
fix/sw-bypass-post-requests
Open

totally-not-ai[bot] wants to merge 6 commits into
mainfrom
fix/sw-bypass-post-requests

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27586

behavior deviation · flow-server (PWA service worker) · apps with @PWA on Chromium browsers

Background — service worker static routing. A service worker with a fetch handler receives every request of the pages it controls, even ones it never answers. Static routing (InstallEvent.addRoutes) lets a worker declare at install time which requests go straight to the network without starting the worker.

When a page controlled by the PWA service worker is reloaded in a Chromium browser, the beacon that closes its UI on the server is sometimes lost. The old UI then stays open, with its listeners and any locks, until the heartbeat timeout. Bypassing the service worker for these requests avoids the loss; there was no workaround besides turning off the worker.

Risks:

  • ⚠️ Behaviour change: POST requests (UIDL, push, the unload beacon) no longer reach the worker's fetch handler where static routing is supported. The worker never answered them, so responses are the same.
  • ❓ The loss could not be reproduced in headless Chromium; the reporter saw it go away with DevTools "Bypass for network", which this route does for POST requests only.
  • ✅ No API change, no server-side change. Browsers without static routing (Firefox today), or that reject the rule, keep the current behaviour and still install the worker.

Context. The worker only routes GET requests (navigations, precache, network-first URLs), so a POST-only rule cannot take away anything it serves. A UIDL-only URL pattern was the alternative, but it would leave push and other POST requests going through the worker for no gain.

  • Added an install handler to sw.ts that adds a static route sending POST requests to the network, ignoring a rejected rule
  • Added an IT to PwaTestIT checking that the UIDL requests of a controlled page are matched to the network source
  • Described the route in the service worker ARCHITECTURE.md

Working on behalf of @Artur-

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
@totally-not-ai

totally-not-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Type of change

  • Bugfix

How to test

  1. Run flow-tests/test-pwa (view PwaTestView.java) and open it in Chrome.
  2. Reload once so the service worker controls the page, then reload again with DevTools → Network open.
  3. The ?v-r=uidl request is not marked as going through the service worker; in the console, performance.getEntriesByType('resource').filter(e => e.name.includes('v-r=uidl')).map(e => e.workerMatchedSourceType) gives ["network"].
Browser support
  • InstallEvent.addRoutes: Chrome/Edge 123+; Safari 27; not in Firefox (MDN browser-compat-data, Can I use, Chrome blog).
  • Within Vaadin's supported browsers (latest evergreen and Safari/iOS 17.x) only Chromium uses the route. Elsewhere addRoutes is missing, or the rule is rejected and ignored, and requests reach the worker as before.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Bundle size

Compared with the base commit 3c06b37.

File Minified Change Brotli Change
sw.js 20,831 B $\color{red}\textbf{+157 B}$ (+0.8%) 6,190 B $\color{red}\textbf{+49 B}$ (+0.8%)
VAADIN/build/ApplicationConnection.js 125,051 B no change 32,276 B no change
VAADIN/build/FlowBootstrap.js 3,191 B no change 1,027 B no change
VAADIN/build/FlowClient.js 10,723 B no change 3,503 B no change
VAADIN/build/frontend.js 409,486 B no change 111,748 B no change
VAADIN/build/generated-flow-imports.js 2,945 B no change 1,100 B no change
VAADIN/build/indexhtml.js 47 B no change 51 B no change
VAADIN/build/rolldown-runtime.js 870 B no change 437 B no change
Total 573,144 B $\color{red}\textbf{+157 B}$ (+0.0%) 156,332 B $\color{red}\textbf{+49 B}$ (+0.0%)

The production bundle of test-prod-bundle, an application with no code of its own: the Flow client, the files Flow generates and the npm packages it brings in. Brotli is what the build precompresses the files with.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — left 1 comment.

Finding
📐 The waiting step that stands in for an assertion does not handle TimeoutException

// 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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 The waiting step that stands in for an assertion does not handle TimeoutException

CONVENTIONS.md (Testing): "When an assertion becomes a wait, catch TimeoutException and fail with the message and the expected and actual values."

The waitUntil here asserts that the reloaded page made a UIDL request at all. If none is recorded — the bootstrap stops issuing one, the URL parameter changes, or the resource timing buffer overflows — the test dies on a bare TimeoutException naming only the lambda, and the carefully worded message on the assertTrue below never prints.

PwaTestIT.java:201 · conventions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaadin-review-bot The wait now catches TimeoutException and fails with a message saying no UIDL request was recorded, listing the resources the reloaded page did request, so the cause is visible without the stack trace.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

📦 Snapshot published

25.5.sw-bypass-post-requests-SNAPSHOT — built from 0ae8a5c (run).

Built without running tests. Look at the checks on this pull request before relying on it.

Every new commit on this branch republishes it while the snapshot build label is there. Add -U to pick the newest build up.

How to use it
<repository>
  <id>vaadin-snapshots</id>
  <url>https://maven.vaadin.com/vaadin-prereleases</url>
  <snapshots><enabled>true</enabled></snapshots>
</repository>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>flow-bom</artifactId>
      <version>25.5.sw-bypass-post-requests-SNAPSHOT</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <!-- After flow-bom, so that its Flow version wins -->
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>vaadin-bom</artifactId>
      <version>25.5-SNAPSHOT</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Replace the application's own vaadin-bom import with these two rather than adding them next to it: with two vaadin-bom imports, the first one decides the platform version. flow-bom has to come before vaadin-bom: Maven takes the first version it finds for an artifact, so the other order leaves the platform's own Flow version in use. 25.5-SNAPSHOT is the unreleased platform snapshot of the same minor version as this one, so this also moves the application off its released platform version.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 655 files  ±0   1 746 suites  ±0   1h 50m 13s ⏱️ + 3m 11s
14 080 tests +3  14 008 ✅ +3  72 💤 ±0  0 ❌ ±0 
14 506 runs  +3  14 434 ✅ +3  72 💤 ±0  0 ❌ ±0 

Results for commit 0ae8a5c. ± Comparison against base commit 3c06b37.

♻️ This comment has been updated with latest results.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@Artur-
Artur- marked this pull request as ready for review December 1, 2026 14:26

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@Artur- Artur- left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this break something like actual handling of POST requests offline?

@totally-not-ai

totally-not-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@Artur- No, it shouldn't. The rule only changes who handles POST requests; nothing in Flow handled them offline before.

  • Default worker: every route in the default sw.ts is GET-only. That includes the navigation route, the precache, the network-first URLs and the dev /VAADIN/ route, since Workbox's registerRoute defaults to GET. A POST matched no route, so the fetch handler never called respondWith, and the browser sent it to the network as usual. Offline, that request fails exactly as it does now, with the static route sending it to the network. The connection-lost tracking was not fed by POSTs before either, because only the GET strategies carry that plugin.
  • Custom worker: an app with its own frontend/sw.ts (or sw.js) replaces the default worker completely, so it never gets this rule. A custom worker that queues or replays POSTs offline, for example with Workbox background sync, keeps working as before.
  • Scope: static routes belong to the worker version that adds them in its install handler. Switching to a custom worker later drops the rule with the old version.

So the only POSTs that skip the worker are ones it already passed to the network untouched. I made no code change for this.

@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UI not detached on refresh cause of serivceworker

3 participants