对您的问题进行了一些研究和复制。这不是一件容易的事,因为 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,这不是此响应的一部分。不过,通常情况下,应用程序应始终考虑到数据库可能会丢失(例如用户清除它),并且如果发生这种情况,则能够从服务器重新填充它们。