indexeddbshim / IndexedDBShim

A polyfill for IndexedDB using WebSql
Other
968 stars 191 forks source link

The shim does not work on Safari 8 or 9 #327

Open dfahlander opened 6 years ago

dfahlander commented 6 years ago

Tested on Browserstack on iOS v9 Safari ( an IPhone 6S Plus).

Safari 9

<script src="https://unpkg.com/indexeddbshim/dist/indexeddbshim.js"></script>
<script> shimIndexedDB.__useShim();  </script>

...does not do its job to patch the browser no matter manually calling shimIndexedDB.__useShim() or not.

Expected behavior: window.indexedDB references the shim instead of the native. Actual behavior: window.indexedDB still is native.

But the following works perfectly:

<script src="https://unpkg.com/indexeddbshim@2.x/dist/indexeddbshim.js"></script>
<script> shimIndexedDB.__useShim();  </script>

Safari 8

As Safari 8 blocks from patching window.indexedDB, it will not be enough to call shimIndexedDB.__useShim() to support Safari 8. This is not something that can be solved within the shim. But we had a solution for it in Dexie 1.x where it automatically used the shim in case it was included.

Now with Dexie 2.x, the developer will have to manually connect the shim with Dexie.dependencies. I have found no way of doing this in IndexedDBShim 3.x as it does not expose IDBKeyRange publicly what I could see. Dexie 1.x solved this with IndexedDBShim 2.x under the hood for anyone who had included the shim, but due to this comment I removed that part in Dexie 2.x.

My Current Recommendations

I've put a recommended way for Dexie users to support older versions of Safari here: https://stackoverflow.com/a/50855488/1282923

Please advice how this could be improved.

brettz9 commented 6 years ago

I unfortunately don't have time now to take a closer look at this.

We do attempt to set IDBKeyRange. But if Safari is preventing it being redefined, and/or is preventing indexedDB, for being redefined, you might be interested in the following...

There is a "non-invasive" file in dist which lets the user call setGlobalVars themselves. It defines the global setGlobalVars and lets the user supply their own object to it on which the IndexedDB "globals" are set (as well as our shimIndexedDB) instead of relying on the default which checks for window (or self for workers or global in Node).

 <!DOCTYPE html>
 <html lang="en">
     <head>
         <meta charset="utf-8">
         <script src="node_modules/babel-polyfill/browser.js"></script>
         <script src="dist/indexeddbshim-noninvasive.js"></script>
     </head>
     <body>
<script>
const obj = {};
setGlobalVars(obj);
console.log(obj.IDBKeyRange.lowerBound('a'));
</script>
     </body>
 </html>

Note that you can also pass in a config object on the second argument.

For completeness' sake (though probably only of interest to users doing testing, e.g., for instanceof checking on return results), two of possible relevance are:

setGlobalVars(obj, {replaceNonIDBGlobals: true});

...which will attempt to add other shimmed objects on which we rely, i.e., DOMException, DOMStringList, CustomEvent, Event, and EventTarget. (The config addNonIDBGlobals is also available to post-fix these with "Shim", so they don't conflict with the likely already-existing versions if adding to window, but in your case, it seems you may need to add on an object anyways.)

You might take a look at https://github.com/axemclion/IndexedDBShim/blob/master/src/setGlobalVars.js , the file responsible for setting the globals (or pseudo-globals depending on how you call its shim).

Of course if it is possible for us to provide a change or fix targeting the affected Safari versions within the above-mentioned file in a way that makes sense, that'd be ideal, so feel free to pass on suggestions. It's just not on my plate to explore these things myself. A PR to better document this (if it is not already so) would also be welcome. Let us know what you may discover.

dfahlander commented 6 years ago

Thanks a lot @brettz9. It definitely looks like an idea for me to try using the non-invasive file. These are things that our users do not have the time to figure out themselves so I hope to find a simple and easy way to just making this work.

I will try (I hope to have the time) to incorporate the shim in dexie tests (both version 2 and 3/non-invasive) to see which one seems most stable as of now.

StabbarN commented 6 years ago

Regarding Safari 9

Expected behavior: window.indexedDB references the shim instead of the native. Actual behavior: window.indexedDB still is native.

Can it be solved by setting window.shimIndexedDB to Dexie's options?

new Dexie("appName", {
    indexedDB: window.shimIndexedDB,
    IDBKeyRange: window.IDBKeyRange,
});