简体   繁体   English

Angular PWA 和 NGRX.. 离线获取数据

[英]Angular PWA and NGRX.. fetch data if offline

I would like to ask if there is a way where i could still retrieve data using ngrx even if the angular app is down due to network or no internet.我想问一下,即使 angular 应用程序由于网络或没有互联网而关闭,我是否仍然可以使用 ngrx 检索数据。 the idea is like.. if no internet then pull default data that have been saved in the store so far.这个想法就像..如果没有互联网,则提取到目前为止已保存在商店中的默认数据。

For a minimalistic PWA, you need to do following:对于简约的 PWA,您需要执行以下操作:

First you register the service worker with:首先,您将 service worker 注册到:

if ('serviceWorker' in navigator) {
    window.addEventListener('load', function() {
      navigator.serviceWorker.register('/serviceworker.js').then(function(registration) {
        // Registration was successful
        console.log('ServiceWorker registration successful with scope: ', registration.scope);
      }, function(err) {
        // registration failed :(
        console.log('ServiceWorker registration failed: ', err);
      });
    });
  }

First you check if its even available in your browser.首先,您检查它是否在您的浏览器中可用。 If it is, then you need to pass the path to your service worker.如果是,那么您需要将路径传递给您的服务人员。 In this example its /serviceworker.js .在此示例中,它/serviceworker.js

Remember: Registration wont work if you are using HTTP or the file system C:// you need to use either HTTPS or with localhost it should work too.请记住:如果您使用 HTTP 或文件系统C:// ,则注册将不起作用,您需要使用 HTTPS 或 localhost 它也应该可以工作。

Now the service worker part.现在是服务人员部分。 With this exmaple you will cache every page you visit, and if you turn off your network it should still work.使用此示例,您将缓存您访问的每个页面,并且如果您关闭网络,它仍然可以工作。

//serviceworker.js

var CACHE_NAME = "my_cache_v1";

self.addEventListener('fetch', function(event) {
    event.respondWith(
      caches.match(event.request)
        .then(function(response) {
          // Cache hit - return response
          if (response) {
            return response;
          }
  
          return fetch(event.request).then(
            function(response) {
              // Check if we received a valid response
              if(!response || response.status !== 200 || response.type !== 'basic') {
                return response;
              }
  
              // IMPORTANT: Clone the response. A response is a stream
              // and because we want the browser to consume the response
              // as well as the cache consuming the response, we need
              // to clone it so we have two streams.
              var responseToCache = response.clone();
  
              caches.open(CACHE_NAME)
                .then(function(cache) {
                  cache.put(event.request, responseToCache);
                });
  
              return response;
            }
          );
        })
      );
  });

You can do more with PWA.您可以使用 PWA 做更多事情。 You can ask the user to install your app, or you can subscribe user to Push notifications.您可以要求用户安装您的应用程序,也可以为用户订阅推送通知。 Actually i work on a small library that should simplify that for you you can check it out here: (still under development)实际上,我在一个小型库上工作,应该为您简化它,您可以在这里查看:(仍在开发中)

https://github.com/ilijanovic/serviceHelper https://github.com/ilijanovic/serviceHelper

https://www.npmjs.com/package/servicehelper https://www.npmjs.com/package/servicehelper

To make it installable you would need to include a manifest to your site with the essential properties.要使其可安装,您需要在您的站点中包含一个具有基本属性的清单。

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

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