blob: fac0d8de2a7361aa2f55fdcde87b969a952b92d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
async function wait_for_performance_entries(url) {
let entries = performance.getEntriesByName(url);
if (entries.length > 0) {
return entries;
}
return new Promise((resolve) => {
new PerformanceObserver((list) => {
const entries = list.getEntriesByName(url);
if (entries.length > 0) {
resolve(entries);
}
}).observe({ entryTypes: ['resource'] });
});
}
self.addEventListener('activate', event => {
event.waitUntil(self.registration.navigationPreload.enable());
});
self.addEventListener('fetch', event => {
let headers;
event.respondWith(
event.preloadResponse
.then(response => {
headers = response.headers;
return response.text()
})
.then(_ => wait_for_performance_entries(event.request.url))
.then(entries =>
new Response(
JSON.stringify({
decodedBodySize: headers.get('X-Decoded-Body-Size'),
encodedBodySize: headers.get('X-Encoded-Body-Size'),
timingEntries: entries
}),
{headers: {'Content-Type': 'text/html'}})));
});
|