Workbox: transferSize reported as 0 on mainThread when StaleWhileRevalidate has a cache miss and hits network

Created on 1 May 2020  路  5Comments  路  Source: GoogleChrome/workbox

Library Affected:
workbox-strategies v4.3.1

Browser & Platform:
all browsers.

Issue: transferSize reported as 0 on mainThread when StaleWhileRevalidate has a cache miss and hits network:

I manually deleted the entire cache and reloaded the page.
image

If it hits cache, the numbers are as below.
image

All 5 comments

This is expected behavior per the Resource Timing spec:

zero otherwise, including for resources retrieved from relevant application caches or from local resources.

Any time a request goes through the service worker, the transfer size will be reported as 0. To get the actual transfer size, you have to use the Resource Timing API from inside the service worker file.

Hey @philipwalton, I know this issue is closed, but wanted to utilize this thread to ask a question related to the topic.

I am trying to implement a perf workbox plugin where I gather information if the navigation request is coming from cache. I want to pass this information back to the main thread.
As service worker can be handling multiple client, what's the easiest way to send this data back to the client?.
I tried sending it as a response header, but turns out we cannot read navigation request headers.
Is there a better way to track navigation requests across tabs through some kind of identifier for each client?

The fetch event that initiated the navigation should have a resultingClientId property, which can be used to later match against the Clients API in the service worker.

Unfortunately, not all browsers support this property, but here's some code we use in Workbox to deal with that (that use can use as an example): https://github.com/GoogleChrome/workbox/blob/04ba6442c466d2e8197fe586672143d201af3a61/packages/workbox-core/src/_private/resultingClientExists.ts

thanks @philipwalton, I will explore this further.
I was looking into event.clientId, but looks like this is better for navigation requests

Yep, event.clientId will not not be set for navigation requests, as the client doesn't exist yet.

Was this page helpful?
0 / 5 - 0 ratings