1 function handleString(event) {
2 event.respondWith(new Response('Test string'));
5 function handleBlob(event) {
6 event.respondWith(new Response(new Blob(['Test blob'])));
9 function handleReferrer(event) {
10 event.respondWith(new Response(new Blob(
11 ['Referrer: ' + event.request.referrer])));
14 function handleNullBody(event) {
15 event.respondWith(new Response());
18 function handleFetch(event) {
19 event.respondWith(fetch('other.html'));
22 function handleFormPost(event) {
23 event.respondWith(new Promise(function(resolve) {
25 .then(function(result) {
26 resolve(new Response(event.request.method + ':' + result));
31 var logForMultipleRespondWith = '';
33 function handleMultipleRespondWith(event) {
34 for (var i = 0; i < 3; ++i) {
35 logForMultipleRespondWith += '(' + i + ')';
37 event.respondWith(new Response(logForMultipleRespondWith));
39 logForMultipleRespondWith += '[' + e.name + ']';
44 var lastResponseForUsedCheck = undefined;
46 function handleUsedCheck(event) {
47 if (!lastResponseForUsedCheck) {
48 event.respondWith(fetch('other.html').then(function(response) {
49 lastResponseForUsedCheck = response;
53 event.respondWith(new Response(
54 'bodyUsed: ' + lastResponseForUsedCheck.bodyUsed));
58 self.addEventListener('fetch', function(event) {
59 var url = event.request.url;
61 { pattern: '?string', fn: handleString },
62 { pattern: '?blob', fn: handleBlob },
63 { pattern: '?referrer', fn: handleReferrer },
64 { pattern: '?ignore', fn: function() {} },
65 { pattern: '?null', fn: handleNullBody },
66 { pattern: '?fetch', fn: handleFetch },
67 { pattern: '?form-post', fn: handleFormPost },
68 { pattern: '?multiple-respond-with', fn: handleMultipleRespondWith },
69 { pattern: '?used-check', fn: handleUsedCheck }
73 for (var i = 0; i < handlers.length; ++i) {
74 if (url.indexOf(handlers[i].pattern) != -1) {
75 handler = handlers[i];
83 event.respondWith(new Response(new Blob(
84 ['Service Worker got an unexpected request: ' + url])));