zoukankan      html  css  js  c++  java
  • [PWA] 4. Hijacking Request

    We want to do offline first, the first thing we need to do is we should able to catch the browser request and return our cache data from serice worker. So user won't go to the real server to fetch data.  So let's see how to do that:

    self.addEventListener('fetch', function(event) {
        event.respondWith(
            new Response('Hello world');
        )
    });

    We use 'respondWith()  and Response()' to provdie a custom cache response.

    After you reopen the browser, you will see 'Hello World'.

    What if we want to show html instead of pain text? The 'Response()' take a second param which can set header.

    self.addEventListener('fetch', function(event) {
        event.respondWith(
            new Response('Hello <b class="a-winner-is-me">world</b>', {
                headers: {'Content-Type': 'text/html'}
            });
        )
    });

  • 相关阅读:
    Scrum冲刺第一篇
    项目冲刺
    需求改进&系统设计
    需求规格说明书
    团队作业第五周-测试和发布
    冲刺集合贴
    冲刺第6天
    冲刺第7天
    冲刺第5天
    冲刺第四天
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5496265.html
Copyright © 2011-2022 走看看