简体   繁体   English

如何在Progressive Web Apps的浏览器中存储脱机存储永久数据?

[英]How to store offline Storage permanent data inside browser in Progressive Web Apps?

Is it Possible to store permanent data in offline Storage inside browser? 是否可以在浏览器内部的离线存储中存储永久数据? If any solution is there then help me so that i can solve my problem. 如果有任何解决方案,那么请帮助我,以便我可以解决我的问题。 I read Some Tutorial but that is not useful me . 我阅读了一些教程,但这对我没有用。 Thanks in Advance! 提前致谢!

Let's get right to the point with a general recommendation for storing data offline: 让我们直接提出离线存储数据的一般建议:

  • For the network resources necessary to load your app while offline, use the Cache API (part of service workers). 对于脱机时加载应用程序所需的网络资源,请使用Cache API(服务工作者的一部分)。
  • For all other data, use IndexedDB (with a promises wrapper). 对于所有其他数据,请使用IndexedDB(带有promises包装器)。

I'll tell you how to use both after i tell you the storage limits per browser. 在告诉您每个浏览器的存储限制后,我将告诉您如何使用两者。

  • Chrome supports use of <6% of free space Chrome支持使用少于6%的可用空间
  • Firefox supports use of <10% of free space Firefox支持使用少于10%的可用空间
  • Safari supports use of <50MB Safari支持使用<50MB
  • Internet Explorer 10 supports use of <250MB Internet Explorer 10支持使用<250MB
  • With Edge it depends on volume size 使用Edge取决于卷大小

A working example of a service worker using the cache api is 使用缓存API的服务工作者的工作示例是

var CACHE_VERSION = 1;

// Shorthand identifier mapped to specific versioned cache.
var CURRENT_CACHES = {
  font: 'font-cache-v' + CACHE_VERSION
};

self.addEventListener('activate', function(event) {
  var expectedCacheNames = Object.values(CURRENT_CACHES);

  // Active worker won't be treated as activated until promise
  // resolves successfully.
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.map(function(cacheName) {
          if (!expectedCacheNames.includes(cacheName)) {
            console.log('Deleting out of date cache:', cacheName);

            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

self.addEventListener('fetch', function(event) {
  console.log('Handling fetch event for', event.request.url);

  event.respondWith(

    // Opens Cache objects that start with 'font'.
    caches.open(CURRENT_CACHES['font']).then(function(cache) {
      return cache.match(event.request).then(function(response) {
        if (response) {
          console.log('Found response in cache:', response);

          return response;
        }

        console.log('Fetching request from the network');

        return fetch(event.request).then(function(networkResponse) {
          cache.put(event.request, networkResponse.clone());

          return networkResponse;
        });
      }).catch(function(error) {

        // Handles exceptions that arise from match() or fetch().
        console.error('Error in fetch handler:', error);

        throw error;
      });
    })
  );
});

[Source and Explanation] [来源和说明]

And i'm not sure about how to use IndexedDB Some other ways are 我不确定如何使用IndexedDB其他一些方法是

  • The File System API which is only for Chrome 13+, Edge, Firefox 50+ and Opera 15+ [Tutorial] 仅适用于Chrome 13 +,Edge,Firefox 50+和Opera 15+的文件系统API [教程]
  • Web Storage (eg LocalStorage and SessionStorage) is synchronous, has no Web Worker support and is size and type (strings only) limited. Web存储(例如LocalStorage和SessionStorage)是同步的,不支持Web Worker,并且大小和类型(仅字符串)受限制。 [Tutorial] [教学课程]
  • There are other but those aren't widely supported and are very difficult 还有其他一些,但并没有得到广泛支持,而且非常困难

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

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