-
Notifications
You must be signed in to change notification settings - Fork 38
consider adding attributes to track Cache API performance in a service worker FetchEvent handler #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@asutherland @mrbkap @bakulf @youennf @toddreifsteck @aliams What do you all think? I can put together a spec text PR and WPT if folks think this is a reasonable approach. |
Sounds reasonable and useful. I think an interesting question is whether it's worth including a third number like |
Interesting idea, but I think I'd prefer to start with something minimal and expand to more granular information like this if necessary in the future. |
Here's another possible approach that is simple, but might be a bit more flexible than the original comment above:
Some examples of what this would look like in practice: //
// service worker script
//
self.addEventListener('fetch', evt => {
if (evt.request.url.endsWith('cache')) {
evt.respondWith(caches.match(evt.request));
return;
}
if (evt.request.url.endsWith('fetch')) {
evt.respondWith(fetch(evt.request));
return;
}
if (evt.request.url.endsWith('constructor')) {
evt.respondWith(new Response('hello world'));
return;
}
});
//
// main window script
//
async function test() {
// Results in the following PerformanceResourceTiming attributes:
// * requestStart: time FetchEvent is fired
// * workerCacheMatchStart: time caches.match() begins
// * workerResponseCreated: time caches.match() resolve the Response
// * responseStart: time respondWith promise is fulfilled with a Response
const cacheURL = baseURL + "?q=cache";
await fetch(cacheURL);
let cacheEntry = performance.getEntriesByName(cacheURL)[0];
console.log(cacheEntry);
// Results in the following PerformanceResourceTiming attributes:
// * requestStart: time FetchEvent is fired
// * workerFetchStart: time fetch() begins
// * workerResponseCreated: time fetch() resolve the Response
// * responseStart: time respondWith promise is fulfilled with a Response
const fetchURL = baseURL + "?q=fetch";
await fetch(fetchURL);
let fetchEntry = performance.getEntriesByName(fetchURL)[0];
console.log(fetchEntry);
// Results in the following PerformanceResourceTiming attributes:
// * requestStart: time FetchEvent is fired
// * workerResponseCreated: time `new Response()` is called
// * responseStart: time respondWith promise is fulfilled with a Response
const constructorURL = baseURL + "?q=constructor";
await fetch(constructorURL);
let constructorEntry = performance.getEntriesByName(constructorURL)[0];
console.log(constructorEntry);
// Results in the following PerformanceResourceTiming attributes:
// * requestStart: time FetchEvent is fired (or time network fallback request starts???)
// * responseStart: time fallback network response is available
const fallbackURL = baseURL + "?q=fallback";
await fetch(fallback);
let fallbackEntry = performance.getEntriesByName(fallbackURL)[0];
console.log(fallbackEntry);
} Of course, all of these examples are somewhat silly since the Response creation process encompasses the entire fetch event handler. In practice, though, real web sites do substantially more work in their fetch event handlers for analytics, etc. It would be useful to measure the Response creation bits separately from the overall time. Edit: Updated example to make it clearer... |
Let's discuss this at TPAC. Added it as an item on the proposals list |
Thank you. I'll write an explainer for my larger "workerTiming" proposal to include in that discussion as well. It seemed like some folks were more excited/interested in that then this modest proposal. |
It would be nice to give sites some more insight into the time spent in a service worker FetchEvent handler. Currently browser mostly (see #119) provide these two PerformanceResourceTiming markers:
requestStart
marks when the FetchEvent is dispatchedresponseStart
marks when theevt.respondWith()
promise resolvesFor sites using a service worker to offline their resources the fetch handler will often be a mix of various javascript and cache API operations like:
Its not easy to associate the
cache.open()
time with the FetchEvent, but we could add PerformanceResourceTiming attributes like:cacheMatchStart
marks the beginning of thecache.match()
orcache.matchAll()
that produced theResponse
passed toevt.respondWith()
cacheMatchEnd
marks when thecache.match()
promise resolved theResponse
passed toevt.respondWith()
This would require setting some internal timing information on the
Response
. This information would be copied automatically across aResponse.clone()
. Any other consumption and re-packing of theResponse
would lose the information, though.The text was updated successfully, but these errors were encountered: