Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cribjs-editor
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Cédric Le Ninivin
cribjs-editor
Commits
3e628223
Commit
3e628223
authored
Jun 26, 2020
by
Cédric Le Ninivin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove Service Workers from Crib Editor
parent
a1f97058
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
296 deletions
+0
-296
serviceworker-cache-polyfill.js
serviceworker-cache-polyfill.js
+0
-87
sw.js
sw.js
+0
-209
No files found.
serviceworker-cache-polyfill.js
deleted
100644 → 0
View file @
a1f97058
// Via https://github.com/coonsta/cache-polyfill/blob/master/dist/serviceworker-cache-polyfill.js
// Adds in some functionality missing in Chrome 40.
if
(
!
Cache
.
prototype
.
add
)
{
Cache
.
prototype
.
add
=
function
add
(
request
)
{
return
this
.
addAll
([
request
]);
};
}
if
(
!
Cache
.
prototype
.
addAll
)
{
Cache
.
prototype
.
addAll
=
function
addAll
(
requests
)
{
var
cache
=
this
;
// Since DOMExceptions are not constructable:
function
NetworkError
(
message
)
{
this
.
name
=
'
NetworkError
'
;
this
.
code
=
19
;
this
.
message
=
message
;
}
NetworkError
.
prototype
=
Object
.
create
(
Error
.
prototype
);
return
Promise
.
resolve
().
then
(
function
()
{
if
(
arguments
.
length
<
1
)
throw
new
TypeError
();
// Simulate sequence<(Request or USVString)> binding:
var
sequence
=
[];
requests
=
requests
.
map
(
function
(
request
)
{
if
(
request
instanceof
Request
)
{
return
request
;
}
else
{
return
String
(
request
);
// may throw TypeError
}
});
return
Promise
.
all
(
requests
.
map
(
function
(
request
)
{
if
(
typeof
request
===
'
string
'
)
{
request
=
new
Request
(
request
);
}
var
scheme
=
new
URL
(
request
.
url
).
protocol
;
if
(
scheme
!==
'
http:
'
&&
scheme
!==
'
https:
'
)
{
throw
new
NetworkError
(
"
Invalid scheme
"
);
}
return
fetch
(
request
.
clone
());
})
);
}).
then
(
function
(
responses
)
{
// TODO: check that requests don't overwrite one another
// (don't think this is possible to polyfill due to opaque responses)
return
Promise
.
all
(
responses
.
map
(
function
(
response
,
i
)
{
return
cache
.
put
(
requests
[
i
],
response
);
})
);
}).
then
(
function
()
{
return
undefined
;
});
};
}
if
(
!
CacheStorage
.
prototype
.
match
)
{
// This is probably vulnerable to race conditions (removing caches etc)
CacheStorage
.
prototype
.
match
=
function
match
(
request
,
opts
)
{
var
caches
=
this
;
return
this
.
keys
().
then
(
function
(
cacheNames
)
{
var
match
;
return
cacheNames
.
reduce
(
function
(
chain
,
cacheName
)
{
return
chain
.
then
(
function
()
{
return
match
||
caches
.
open
(
cacheName
).
then
(
function
(
cache
)
{
return
cache
.
match
(
request
,
opts
);
}).
then
(
function
(
response
)
{
match
=
response
;
return
match
;
});
});
},
Promise
.
resolve
());
});
};
}
sw.js
deleted
100644 → 0
View file @
a1f97058
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This polyfill provides Cache.add(), Cache.addAll(), and CacheStorage.match(),
// which are not implemented in Chrome 40.
// Should not be needed for Chromium > 47 And Firefox > 39
// See https://developer.mozilla.org/en-US/docs/Web/API/Cache
importScripts
(
'
./serviceworker-cache-polyfill.js
'
);
// While overkill for this specific sample in which there is only one cache,
// this is one best practice that can be followed in general to keep track of
// multiple caches used by a given service worker, and keep them all versioned.
// It maps a shorthand identifier for a cache to a specific, versioned cache name.
// Note that since global state is discarded in between service worker restarts, these
// variables will be reinitialized each time the service worker handles an event, and you
// should not attempt to change their values inside an event handler. (Treat them as constants.)
// If at any point you want to force pages that use this service worker to start using a fresh
// cache, then increment the CACHE_VERSION value. It will kick off the service worker update
// flow and the old cache(s) will be purged as part of the activate event handler when the
// updated service worker is activated.
var
CACHE_VERSION
=
1
;
var
CURRENT_CACHES
=
{
'
cribjs
'
:
'
post-message-cache-v
'
+
CACHE_VERSION
};
var
URL_LIST
;
self
.
addEventListener
(
'
activate
'
,
function
(
event
)
{
// Delete all caches that aren't named in CURRENT_CACHES.
// While there is only one cache in this example, the same logic will handle the case where
// there are multiple versioned caches.
var
expectedCacheNames
=
Object
.
keys
(
CURRENT_CACHES
).
map
(
function
(
key
)
{
return
CURRENT_CACHES
[
key
];
});
URL_LIST
=
{};
event
.
waitUntil
(
caches
.
keys
().
then
(
function
(
cacheNames
)
{
return
Promise
.
all
(
cacheNames
.
map
(
function
(
cacheName
)
{
if
(
expectedCacheNames
.
indexOf
(
cacheName
)
==
-
1
)
{
// If this cache name isn't present in the array of "expected" cache names, then delete it.
console
.
log
(
'
Deleting out of date cache:
'
,
cacheName
);
return
caches
.
delete
(
cacheName
);
}
})
);
})
);
});
self
.
addEventListener
(
'
fetch
'
,
function
(
event
)
{
//console.log('Handling fetch event for', event.request.url);
if
(
event
.
request
.
method
===
"
GET
"
)
{
/// XXX Why is URL_LIST undefined in some cases???
if
(
URL_LIST
===
undefined
)
URL_LIST
=
{};
if
(
URL_LIST
[
event
.
request
.
url
]
===
undefined
)
{
URL_LIST
[
event
.
request
.
url
]
=
{
cached
:
false
,
request_number
:
0
,
url
:
event
.
request
.
url
,
};
}
URL_LIST
[
event
.
request
.
url
].
request_number
=
URL_LIST
[
event
.
request
.
url
].
request_number
+
1
;
event
.
respondWith
(
caches
.
open
(
CURRENT_CACHES
[
'
cribjs
'
]).
then
(
function
(
cache
)
{
return
cache
.
match
(
event
.
request
).
then
(
function
(
response
)
{
if
(
response
)
{
// If there is an entry in the cache for event.request, then response will be defined
// and we can just return it. Note that in this example, only font resources are cached.
//console.log(' Found response in cache:', response.url);
URL_LIST
[
event
.
request
.
url
].
cached
=
true
;
return
response
;
}
else
{
// Otherwise, if there is no entry in the cache for event.request, response will be
// undefined, and we need to fetch() the resource.
//console.log(' No response for %s found in cache. About to fetch from network...', event.request.url);
// We call .clone() on the request since we might use it in a call to cache.put() later on.
// Both fetch() and cache.put() "consume" the request, so we need to make a copy.
// (see https://fetch.spec.whatwg.org/#dom-request-clone)
return
fetch
(
event
.
request
.
clone
()).
then
(
function
(
response
)
{
//console.log(' Response for %s from network is: %O', event.request.url, response.url);
// Return the original response object, which will be used to fulfill the resource request.
//cache.put(event.request, response.clone());
return
response
;
});
}
}).
catch
(
function
(
error
)
{
// This catch() will handle exceptions that arise from the match() or fetch() operations.
// Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
// It will return a normal response object that has the appropriate error code set.
console
.
error
(
'
Error in fetch handler:
'
,
error
);
throw
error
;
});
})
);
}
else
{
event
.
respondWith
(
fetch
(
event
.
request
));
}
});
self
.
addEventListener
(
'
message
'
,
function
(
event
)
{
var
request
;
console
.
log
(
'
Handling message event:
'
,
event
);
// URL_LIST is a hack it should use a persistent object
if
(
URL_LIST
===
undefined
)
URL_LIST
=
{};
caches
.
open
(
CURRENT_CACHES
[
'
cribjs
'
]).
then
(
function
(
cache
)
{
switch
(
event
.
data
.
command
)
{
// This command returns a list of the URLs corresponding to the Request objects
// that serve as keys for the current cache.
case
'
keys
'
:
cache
.
keys
().
then
(
function
(
requests
)
{
var
urls
=
requests
.
map
(
function
(
request
)
{
return
request
.
url
;
});
// event.ports[0] corresponds to the MessagePort that was transferred as part of the controlled page's
// call to controller.postMessage(). Therefore, event.ports[0].postMessage() will trigger the onmessage
// handler from the controlled page.
// It's up to you how to structure the messages that you send back; this is just one example.
event
.
ports
[
0
].
postMessage
({
error
:
null
,
urls
:
urls
.
sort
()
});
});
break
;
case
'
allDocs
'
:
cache
.
keys
().
then
(
function
(
requests
)
{
var
urls
=
requests
.
map
(
function
(
request
)
{
return
request
.
url
;
}),
i
,
i_len
,
url
;
for
(
i
=
0
,
i_len
=
urls
.
length
;
i
<
i_len
;
i
+=
1
)
{
url
=
urls
[
i
];
if
(
URL_LIST
[
url
]
===
undefined
)
{
URL_LIST
[
url
]
=
{
cached
:
true
,
request_number
:
0
,
url
:
url
,
};
}
}
event
.
ports
[
0
].
postMessage
({
error
:
null
,
urls
:
URL_LIST
});
});
break
;
// This command adds a new request/response pair to the cache.
case
'
add
'
:
// If event.data.url isn't a valid URL, new Request() will throw a TypeError which will be handled
// by the outer .catch().
// Hardcode {mode: 'no-cors} since the default for new Requests constructed from strings is to require
// CORS, and we don't have any way of knowing whether an arbitrary URL that a user entered supports CORS.
request
=
new
Request
(
event
.
data
.
url
,
{
mode
:
'
no-cors
'
}),
response
=
new
Response
(
event
.
data
.
information
);
if
(
URL_LIST
[
request
.
url
]
===
undefined
)
{
URL_LIST
[
request
.
url
]
=
{
cached
:
true
,
request_number
:
0
,
url
:
request
.
url
,
};
}
cache
.
put
(
request
,
response
).
then
(
function
()
{
event
.
ports
[
0
].
postMessage
({
error
:
null
});
});
break
;
// This command removes a request/response pair from the cache (assuming it exists).
case
'
delete
'
:
request
=
new
Request
(
event
.
data
.
url
,
{
mode
:
'
no-cors
'
});
cache
.
delete
(
request
).
then
(
function
(
success
)
{
event
.
ports
[
0
].
postMessage
({
error
:
success
?
null
:
'
Item was not found in the cache.
'
});
});
if
(
URL_LIST
[
request
.
url
]
!==
undefined
)
{
URL_LIST
[
request
.
url
].
cached
=
false
;
}
break
;
default
:
// This will be handled by the outer .catch().
throw
'
Unknown command:
'
+
event
.
data
.
command
;
}
}).
catch
(
function
(
error
)
{
// If the promise rejects, handle it by returning a standardized error message to the controlled page.
console
.
error
(
'
Message handling failed:
'
,
error
);
event
.
ports
[
0
].
postMessage
({
error
:
error
.
toString
()
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment