Sorry, not sorry 👍
navigator.serviceWorker.register("sw.js");
navigator
.serviceWorker
.register("sw.js")
.then(reg => console.log(reg));
Window
object.
fetch("https://example.com/some/text/")
.then(response => handleResponse(response))
.catch(err);
const request = new Request(url, opts);
fetch(request)
.then(response => handleResponse(response))
.catch(err);
Request
objectsurl
method
(GET, POST, HEAD, etc.)
mode
(CORS)
redirect
("follow", "error", "manual")
referrer
referrerPolicy
(e.g., "origin")
headers
(custom headers)
Request
objects
var req = new Request("/test/", {
referrer: "http://localhost:8080/foo/bar",
referrerPolicy: "origin", //"unsafe-url"
});
fetch(req).then(lookAtAnotherExample);
Request
objects
// Let's post some JSON
var req = new Request("./process", {
method: "POST",
body: JSON.stringify({greeting: "Hi"})
});
// Go, fetch!
fetch(req).then(letsLookAtResponse);
Response
objectsurl
type
(basic, error, opaque, etc.)
redirected
status
ok
statusText
headers
bodyUsed
var body = "Not found
";
var res = new Response(body, {
status: 404,
statusText: "Not Found",
});
Body
mixin Request
and Response
)arrayBuffer()
blob()
formData()
json()
text()
Body
mixin
var req = new Request("./process", {
method: "POST",
body: JSON.stringify({greeting: "Hi"})
});
// consumes the body
req.json()
.then(json => console.log(json))
.then(()=> console.log(req.bodyUsed))
Body
mixin
var req = new Request("./process", {
method: "POST",
body: JSON.stringify({greeting: "Hi"})
});
// clone's body not consumed
req.clone().json()
.then(json => console.log(json))
.then(() => console.log(req.bodyUsed))
Headers
objectsset(name, value);
delete(name);
get(name);
has(name);
keys(), values()
Headers
objects
req.headers.set("whatevs", "awesome");
res.headers.get("whatevs");
if(res.headers.has("whatevs")){
doWhatevs()
}
CacheStorage
APIwindow.caches
.open(name)
.delete(name)
.has(name)
.keys()
.match(request,{options})
CacheStorage
APIwindow.caches
const urls = ["./", "images/dom.jpg"];
caches.open("v1")
.then(cache => cache.addAll(urls));
caches.match("images/dom", {
cacheName: "v1",
}).then(response => doSomething);
Cache
APIadd()
addAll()
delete()
keys()
match()
matchAll()
put()
caches.open(cacheName)
.then(cache => cache.put(request, response.clone())
.then(doStuff);
Notification.requestPermission()
.then(perm => {
if(perm !== "granted") {
return;
}
const noti = new Notification("OMG HI");
noti.onclick = () => console.log("click!");
});
Is a lot of work to set up...