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?
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'}
})
)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With