Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a HTML response using a string inside a service worker's fetch event

I am messing around with service workers at the moment. I am trying to get the 'fetch' event to return custom HTML instead of just plain text like below:

self.addEventListener('fetch', function(event) {
  console.log("Caught a fetch!");
  event.respondWith(new Response("I want to return HTML here"));
});

Is this possible with the respondWith function?

like image 296
john23 Avatar asked Jul 06 '16 17:07

john23


1 Answers

Anyone else looking to do this - here is the answer:

self.addEventListener('fetch', function(event) {
  console.log("Caught a fetch!");
  event.respondWith(
      new Response("<h1>Hello!</h1>", {
        headers: {'Content-Type': 'text/html'}
      })
   )
});
like image 151
john23 Avatar answered Oct 16 '22 23:10

john23