【问题标题】:NotFoundError : DOM IDVDatabase Exception 8 - while using dexie on ipad (working on desktop chrome & safari))NotFoundError : DOM IDVDatabase Exception 8 - 在 ipad 上使用 dexie 时(在桌面 chrome 和 safari 上工作))
【发布时间】:2018-11-21 08:06:40
【问题描述】:

我正在开发使用带有包装器dexie.js 的 IndexedDB 的应用程序。它在桌面 Chrome 和 Safari 上运行良好,但显示以下错误:

NotFoundError : DOM IDVDatabase Exception 8" 在 iPad (iOS 9.3.5) 上。

在最新的 iOS iPad 上,它可以正常工作。我需要一个修复程序,使它适用于所有 iOS 版本。

我已尝试应用 github 和 stackoverflow 上可用的修复,但没有奏效。

谢谢。任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过包含 IndexedDBShim?它尽最大努力填补 IndexedDB 有缺陷(旧 iOS 设备)或不存在(旧 iOS 设备)的空白
  • 感谢大卫的回复。是的,我确实使用了它,但它没有用。

标签: indexeddb dexie


【解决方案1】:

对您的问题进行了一些研究和复制。这不是一件容易的事,因为 IndexedDBShim 3.7.0 似乎没有正确地完成它的工作。幸运的是,我们有 2.x 版本的 shim,它在 iOS 9 上的行为与预期一样 :-)

好的,所以我现在已经对此进行了几个小时的研究,在 Browserstack 等上进行了测试,并找到了一个真正可行的解决方案。请注意,IndexedDBShim 本身也存在一些问题,但 Dexie 中的基本内容应该比 iOS 8 和 9 上的原生 IndexedDB 更好。

将以下 sn-p 插入到为您的应用程序提供服务的 HTML 页面的顶部,最好作为 HEAD 标记中的第一个脚本标记之一:

<script>
    //
    // Download & use the shim if on buggy Safari (internal version no below 602)
    //
    (function(){

      //
      // Check if we are on Safari
      //
      var isSafari = typeof navigator !== 'undefined' &&
        /Safari/.test(navigator.userAgent) &&
        !/(Chrome\/|Edge\/)/.test(navigator.userAgent);

      if (isSafari) {

        //
        // Check Internal Safari Version
        //
        var safariInternalVersion = [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1];

        if (safariInternalVersion < 602) {

          //
          // Download and apply the shim now!
          //

          // IMPORTANT: Use 2.x version of the shim, as 3.x latest (3.7.0 as of 2018-06-14) does NOT work on iOS 9!
          document.write('<script src="https://unpkg.com/indexeddbshim@2.x/dist/indexeddbshim.js">\x3C/script>');
          // IMPORTANT: Force the shim to be used, as itself do not inforce automatically for certain buggy versions.
          document.write('<script> shimIndexedDB.__useShim(); \x3C/script>');
        }
      }
    })()
</script>

重要提示:此脚本必须在包含 dexie.js(或您的 webpack 包)之前。

上面的 sn-p 足以支持 Safari 9。除了上面的脚本之外,Safari 8 还需要另一个小的 JS 代码。 sn-p 应该在包含 Dexie 之后执行(无论包含策略),但在第一次使用 Dexie 之前:

  //
  // Also support Safari 8, where indexedDB is non-configurable on
  // window object, making the shim unable to do its work.
  // 
  // What we do here is to manually connect Dexie with the shim
  // in case the shim has been included by the script described at:
  // https://stackoverflow.com/posts/50855488
  //
  // This snippet should execute after including Dexie (no matter 
  // include strategy), but before using Dexie first time:
  //
  if (typeof shimIndexedDB !== 'undefined') {
    Dexie.dependencies.indexedDB = shimIndexedDB.modules.shimIndexedDB;
    Dexie.dependencies.IDBKeyRange = shimIndexedDB.modules.IDBKeyRange;
  }

像这样包含垫片的好处是它不会损害不需要它的浏览器的性能。

注意:当您的用户更新他们的设备并获得更新版本的 Safari 时,Dexie 将开始使用本机 IndexedDB,这当然是空的。如果不希望这样做,您将需要进行一些更高级的检查或将数据库迁移到 indexedDB,这不是此响应的一部分。不过,通常情况下,应用程序应始终考虑到数据库可能会丢失(例如用户清除它),并且如果发生这种情况,则能够从服务器重新填充它们。

【讨论】:

  • 感谢您在此 David 上花费的时间 :) 我会试试这个并告诉您进展如何。
  • 很高兴听到它对您有用。请为我的答案投票,以便其他有相同问题的人注意到这是一个有效的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多