'Offline First' is the theme of my upcoming project and a relational database with joins and primary keys (such as SQLite) is perhaps the only thing that fulfills the need. I'm exploring the options in this context and came across this illuminative article: The Current State Of SQLite Persistence On The Web.

The article describes the problem we are trying to solve in much detail. It's only been about 3-4 years since we have a semblance of a usable official library in the form of sqlite-wasm. But as the article suggests, the landscape is riddled with mines and you may still have to jump through some hooves during implementation.

For starters, the OPFS (Origin Private File System) which is needed for performant WASM components like sqlite-wasm is something that browsers still treat with some awe and schizophrenia. In order for SharedArrayBuffer to work, for example, your web server must necessarily support COOP and COEP headers.

And not just that, once you enable the COOP and COEP headers, they introduce additional quirks in your app - for example, you may no longer use any <script> or <link> tags unless the remote server explicitly opts in for cross-origin support. Again, this may not affect the typical react/tailwind/vite build workflows but you might eventually come across the need to place an external resource link directly in the html.

Another limitation is that concurrency is still considerably more constrained and complicated than native SQLite. For now, you might carefully design the SPA app around this constraint (as I'm planning to do), route all SQL queries through just one caller, etc. but eventually there will come a need for concurrent access in a non-trivial application - like say a backup extension or addon you provide separately for example.

Nevertheless, the heartening news is that most devices today live online and likely have a reasonably recent browser - and thus likely to support libraries like sqlite-wasm. Also heartening is that many big names like Notion and Adobe have started using sqlite in the browser as a first class OPFS citizen - without going through the IndexedDb route. I just hope WASM and OPFS keep on thriving along with sqlite on the web.

submitted by /u/pyeri
[link] [留言]