简体   繁体   English

使用服务工作者的fetch事件中的字符串创建HTML响应

[英]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: 我试图让'fetch'事件返回自定义HTML,而不仅仅是纯文本,如下所示:

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? 这是否可以使用respondWith函数?

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'}
      })
   )
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM