Skip to content

Instantly share code, notes, and snippets.

@domenic
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenic/1bbec0f341ae3cfb3a8f to your computer and use it in GitHub Desktop.
Save domenic/1bbec0f341ae3cfb3a8f to your computer and use it in GitHub Desktop.
ServiceWorker server vs. client request
self.onfetch = ev => {
var serverReq = ev.request;
var clientReq = new Request(serverReq.url, {
method: serverReq.method,
headers: serverReq.headers,
mode: serverReq.mode,
credentials: serverReq.credentials
});
serverReq.body.pipeTo(clientReq.body);
// Note: lol, this re-uses a *client response* from fetch as a *server response*
// out of the service worker. So all that work distinguishing between client vs.
// server *requests* also needs to happen for response?!
fetch(clientReq).then(clientRes => ev.respondWith(clientRes));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment