Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
};
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -188,6 +189,35 @@ 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.
getDriver().navigate().refresh();
// 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 = 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,
sourceTypes.matches("\\[\"network\"(,\"network\")*\\]"));
}

@Test
public void testPwaResourcesOffline() {
open();
Expand Down