Skip to content

Commit 31c22cc

Browse files
committed
Initial OPFS docs.
1 parent 09963e4 commit 31c22cc

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

client-sdk-references/javascript-web.mdx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,78 @@ powerSync.connect(connector)
307307
powerSync.connect(connector, { connectionMethod: SyncStreamConnectionMethod.HTTP });
308308
```
309309
310+
### Configuring Different SQLite Virtual Filesystems for Web
311+
The default SQLite VFS (`IDBBatchAtomicVFS`) can be replaced by supported [OPFS](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system) alternatives.
312+
`IDBBatchAtomicVFS` uses `IndexedDB` as the underlying storage which may have worse performance and stability issues with browsers such as Safari when compared with `OPFS` based VFSs.
313+
See [WA-SQLite README](https://github.com/powersync-ja/wa-sqlite/blob/1bb58d3619b96a2708e0320e1c22d0f2b9db35c6/src/examples/README.md#L28) for more details on each option.
314+
315+
`OPFS` can be configured with a `WASQLiteOpenFactory`.
316+
317+
<CodeGroup>
318+
```js AccessHandlePoolVFS
319+
import { PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
320+
321+
export const db = new PowerSyncDatabase({
322+
schema: AppSchema,
323+
database: new WASQLiteOpenFactory({
324+
dbFilename: 'exampleVFS.db',
325+
vfs: WASQLiteVFS.AccessHandlePoolVFS,
326+
flags: {
327+
enableMultiTabs: typeof SharedWorker !== 'undefined'
328+
}
329+
}),
330+
flags: {
331+
enableMultiTabs: typeof SharedWorker !== 'undefined'
332+
}
333+
});
334+
```
335+
336+
```js OPFSCoopSyncVFS (Safari Multiple Tabs Support)
337+
import { PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
338+
339+
export const db = new PowerSyncDatabase({
340+
schema: AppSchema,
341+
database: new WASQLiteOpenFactory({
342+
dbFilename: 'exampleVFS.db',
343+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
344+
flags: {
345+
enableMultiTabs: typeof SharedWorker !== 'undefined'
346+
}
347+
}),
348+
flags: {
349+
enableMultiTabs: typeof SharedWorker !== 'undefined'
350+
}
351+
});
352+
```
353+
</CodeGroup>
354+
355+
**Note**: The `AccessHandlePoolVFS` does not to work correctly in the multiple tabs use case. `OPFSCoopSyncVFS` works with multiple tabs, and adds multiple tab support for both Safari and Safari iOS.
356+
| VFS Type | Multi-Tab Support on Normal Browsers | Multi-Tab Support on Safari/Safari iOS | Notes |
357+
|-------------------------|-------------------------------------|----------------------------------------|-------------------------------------------------------------------|
358+
| **IDBBatchAtomicVFS** | Supported | Not supported | May have stability issues on Safari. |
359+
| **AccessHandlePoolVFS**| Not supported | Not supported | Does not work correctly in multi-tab scenarios. |
360+
| **OPFSCoopSyncVFS** | Supported | Supported | Works with multiple tabs on all browsers, including Safari. |
361+
362+
### HTTP Requirements
363+
364+
The use of `OPFS` requires a secure context and additional HTTP headers when serving a web application.
365+
366+
For Vite these headers can be configured in the Vite config file:
367+
`vite.config.mts`
368+
```js
369+
import { defineConfig } from 'vite';
370+
371+
export default defineConfig({
372+
server: {
373+
headers: {
374+
'Cross-Origin-Opener-Policy': 'same-origin',
375+
'Cross-Origin-Embedder-Policy': 'require-corp',
376+
},
377+
},
378+
});
379+
380+
```
381+
310382
## ORM Support
311383
312384
See [JavaScript ORM Support](/client-sdk-references/javascript-web/javascript-orm/overview) for details.

0 commit comments

Comments
 (0)