-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathService-Worker-Analysis.js
More file actions
192 lines (172 loc) ยท 6.27 KB
/
Service-Worker-Analysis.js
File metadata and controls
192 lines (172 loc) ยท 6.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Service Worker Analysis
// https://webperf-snippets.nucliweb.net
(async () => {
if (!('serviceWorker' in navigator)) {
console.log(
'%cโ ๏ธ Service Workers not supported in this browser',
'color: #f59e0b; font-weight: bold;'
);
return;
}
const registrations = await navigator.serviceWorker.getRegistrations();
const controller = navigator.serviceWorker.controller;
const navEntry = performance.getEntriesByType('navigation')[0];
const resources = performance.getEntriesByType('resource');
// Resources intercepted by SW (workerStart > 0)
const swResources = resources.filter((r) => r.workerStart > 0);
const fromCache = swResources.filter((r) => r.transferSize === 0);
const fromNetwork = swResources.filter((r) => r.transferSize > 0);
const notIntercepted = resources.filter((r) => r.workerStart === 0);
console.group(
'%cโ๏ธ Service Worker Analysis',
'font-weight: bold; font-size: 14px;'
);
// Registrations
console.log('');
console.log('%c๐ Registrations:', 'font-weight: bold;');
if (registrations.length === 0) {
console.log('%c โ No Service Workers registered', 'color: #ef4444;');
console.groupEnd();
return;
}
for (const reg of registrations) {
console.log('');
console.log(`%c Scope: ${reg.scope}`, 'font-family: monospace;');
if (reg.active) {
console.log(
`%c โ
Active: ${reg.active.state}`,
'color: #22c55e;'
);
console.log(` Script: ${reg.active.scriptURL}`);
}
if (reg.waiting) {
console.log(
'%c โณ Waiting SW detected โ update pending',
'color: #f59e0b;'
);
console.log(` Script: ${reg.waiting.scriptURL}`);
console.log(
' ๐ก Call skipWaiting() to activate the new version'
);
}
if (reg.installing) {
console.log('%c ๐ Installing...', 'color: #3b82f6;');
}
// Navigation Preload
if (reg.navigationPreload) {
try {
const preload = await reg.navigationPreload.getState();
console.log('');
console.log('%c ๐ Navigation Preload:', 'font-weight: bold;');
if (preload.enabled) {
console.log('%c โ
Enabled', 'color: #22c55e;');
console.log(` Header value: "${preload.headerValue}"`);
} else {
console.log('%c โ Disabled', 'color: #ef4444;');
console.log(
' ๐ก Enable with: registration.navigationPreload.enable()'
);
}
} catch {
// Access may be restricted
}
}
}
// Controller
console.log('');
console.log('%c๐ฎ Controller:', 'font-weight: bold;');
if (controller) {
console.log('%c โ
Page is controlled by SW', 'color: #22c55e;');
console.log(` State: ${controller.state}`);
} else {
console.log('%c โ ๏ธ Page is NOT controlled by SW', 'color: #f59e0b;');
console.log(' Hard reload detected or first visit. Do a normal reload.');
}
// SW Startup overhead
if (navEntry && navEntry.workerStart > 0) {
const workerStart = navEntry.workerStart - navEntry.startTime;
const fetchStart = navEntry.fetchStart - navEntry.startTime;
const swOverhead = Math.max(fetchStart - workerStart, 0);
console.log('');
console.log('%cโฑ๏ธ SW Startup Overhead:', 'font-weight: bold;');
console.log(` Worker start: ${workerStart.toFixed(1)}ms`);
console.log(` Fetch start: ${fetchStart.toFixed(1)}ms`);
console.log(` SW overhead: ${swOverhead.toFixed(1)}ms`);
if (swOverhead > 100) {
console.log(
'%c ๐ด High SW startup time โ enable Navigation Preload',
'color: #ef4444;'
);
} else if (swOverhead > 50) {
console.log(
'%c ๐ก Moderate SW startup time',
'color: #f59e0b;'
);
} else {
console.log(
'%c ๐ข SW startup overhead is low',
'color: #22c55e;'
);
}
}
// Cache hit/miss ratio
console.log('');
console.log('%c๐ Resource Cache Analysis:', 'font-weight: bold;');
console.log(` Total resources: ${resources.length}`);
console.log(` SW intercepted: ${swResources.length}`);
console.log(` Not intercepted: ${notIntercepted.length}`);
if (swResources.length > 0) {
const hitRate = ((fromCache.length / swResources.length) * 100).toFixed(1);
const savedKB = (
fromCache.reduce((sum, r) => sum + (r.encodedBodySize || 0), 0) / 1024
).toFixed(1);
console.log('');
console.log(' SW-intercepted breakdown:');
console.log(` โโ Served from cache: ${fromCache.length} (${hitRate}% hit rate)`);
console.log(` โโ Fetched from network: ${fromNetwork.length}`);
console.log(` Network bytes saved: ~${savedKB} KB`);
const rate = parseFloat(hitRate);
if (rate >= 80) {
console.log('%c ๐ข Excellent cache hit rate', 'color: #22c55e;');
} else if (rate >= 50) {
console.log('%c ๐ก Good cache hit rate', 'color: #f59e0b;');
} else {
console.log(
'%c ๐ด Low cache hit rate โ review caching strategy',
'color: #ef4444;'
);
}
console.log('');
console.log('%c๐ SW-intercepted Resources (top 20):', 'font-weight: bold;');
console.table(
swResources.slice(0, 20).map((r) => ({
'Cache': r.transferSize === 0 ? 'โ
Cache' : '๐ Network',
'Transfer (KB)': r.transferSize > 0 ? (r.transferSize / 1024).toFixed(1) : '0',
'Duration (ms)': r.duration.toFixed(0),
'Type': r.initiatorType,
'URL': r.name.length > 60 ? '...' + r.name.slice(-57) : r.name,
}))
);
}
// Cache Storage inventory
if ('caches' in window) {
try {
const cacheNames = await caches.keys();
if (cacheNames.length > 0) {
console.log('');
console.log('%c๐พ Cache Storage:', 'font-weight: bold;');
let totalEntries = 0;
for (const name of cacheNames) {
const cache = await caches.open(name);
const keys = await cache.keys();
totalEntries += keys.length;
console.log(` โโ "${name}": ${keys.length} entries`);
}
console.log(` Total entries: ${totalEntries}`);
}
} catch {
// Cross-origin restrictions may prevent cache access
}
}
console.groupEnd();
})();