Angular 2 with Html5 application cache

泪湿孤枕 提交于 2020-01-14 02:55:08

问题


I have angular 2 website. I want to use html 5 application cache mechanism for this website to creating offline app for browsers. is it possible? if it is can you guide me.


回答1:


Yes, It is possible through concept of service-worker, which should work in majority of the browsers.

you can do that using this

<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
      console.log('Service Worker registered');
    }).catch(function(err) {
      console.log('Service Worker registration failed: ', err);
    });
  }
</script>

Next you need to include service worker JS in index.html.

For caching Google Maintains sw-precache

Useful Links https://github.com/GoogleChrome/sw-precache

https://coryrylan.com/blog/fast-offline-angular-apps-with-service-workers



来源:https://stackoverflow.com/questions/42410977/angular-2-with-html5-application-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!