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
content / test / data / service_worker / static_router.js [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const composeCustomResponse = () => {
const headers = new Headers();
headers.append('Content-Type', 'text/html');
headers.append('X-Response-From', 'fetch-handler');
const options = {
status: 200,
statusText: 'Custom response from fetch handler',
headers
};
return new Response(
'[ServiceWorkerStaticRouter] Response from the fetch handler',
options);
};
self.addEventListener('install', e => {
e.addRoutes([{
condition: {
urlPattern: "/service_worker/direct",
requestMethod: "GET",
},
source: "network"
}, {
condition: {
urlPattern: "/service_worker/direct_if_not_running",
runningStatus: "not-running",
},
source: "network"
}, {
condition: {
urlPattern: "/service_worker/cache_with_name",
},
source: {cacheName: "test"}
}, {
condition: {
urlPattern: "/service_worker/cache_with_wrong_name",
},
source: {cacheName: "not_exist"}
}, {
condition: {
urlPattern: "/service_worker/cache_*",
},
source: "cache"
}, {
condition: {
not: {not: {urlPattern: "/service_worker/not_not_match"}}
},
source: "network"
}, {
condition: {
urlPattern: "/service_worker/fetch_event_rule"
},
source: "fetch-event"
}]);
caches.open("test").then((c) => {
const headers = new Headers();
headers.append('Content-Type', 'text/html');
headers.append('X-Response-From', 'cache');
const options = {
status: 200,
statusText: 'Custom response from cache',
headers
};
const response = new Response(
'[ServiceWorkerStaticRouter] Response from the cache',
options);
c.put("/service_worker/cache_hit", response.clone());
c.put("/service_worker/cache_with_name", response.clone());
c.put("/service_worker/cache_with_wrong_name", response.clone());
});
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(clients.claim());
});
self.addEventListener("fetch", async e => {
e.respondWith(composeCustomResponse());
});