Skip to content

Commit 5974e52

Browse files
committed
Added example covering OPFS storage clearing.
1 parent 31c22cc commit 5974e52

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

client-sdk-references/javascript-web.mdx

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,19 @@ powerSync.connect(connector, { connectionMethod: SyncStreamConnectionMethod.HTTP
310310
### Configuring Different SQLite Virtual Filesystems for Web
311311
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.
312312
`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.
313+
See the [WA-SQLite README](https://github.com/powersync-ja/wa-sqlite/blob/1bb58d3619b96a2708e0320e1c22d0f2b9db35c6/src/examples/README.md#L28) for more details on each option.
314314
315315
`OPFS` can be configured with a `WASQLiteOpenFactory`.
316316
317317
<CodeGroup>
318-
```js AccessHandlePoolVFS
318+
```js OPFSCoopSyncVFS (Safari Multiple Tabs Support)
319319
import { PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
320320

321321
export const db = new PowerSyncDatabase({
322322
schema: AppSchema,
323323
database: new WASQLiteOpenFactory({
324324
dbFilename: 'exampleVFS.db',
325-
vfs: WASQLiteVFS.AccessHandlePoolVFS,
325+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
326326
flags: {
327327
enableMultiTabs: typeof SharedWorker !== 'undefined'
328328
}
@@ -333,14 +333,14 @@ export const db = new PowerSyncDatabase({
333333
});
334334
```
335335
336-
```js OPFSCoopSyncVFS (Safari Multiple Tabs Support)
336+
```js AccessHandlePoolVFS
337337
import { PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
338338

339339
export const db = new PowerSyncDatabase({
340340
schema: AppSchema,
341341
database: new WASQLiteOpenFactory({
342342
dbFilename: 'exampleVFS.db',
343-
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
343+
vfs: WASQLiteVFS.AccessHandlePoolVFS,
344344
flags: {
345345
enableMultiTabs: typeof SharedWorker !== 'undefined'
346346
}
@@ -353,32 +353,55 @@ export const db = new PowerSyncDatabase({
353353
</CodeGroup>
354354
355355
**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+
357+
**Note**: There are known limitations with `OPFS` in Safari’s incognito mode, which may cause it to not function properly.
358+
356359
| VFS Type | Multi-Tab Support on Normal Browsers | Multi-Tab Support on Safari/Safari iOS | Notes |
357360
|-------------------------|-------------------------------------|----------------------------------------|-------------------------------------------------------------------|
358361
| **IDBBatchAtomicVFS** | Supported | Not supported | May have stability issues on Safari. |
359362
| **AccessHandlePoolVFS**| Not supported | Not supported | Does not work correctly in multi-tab scenarios. |
360363
| **OPFSCoopSyncVFS** | Supported | Supported | Works with multiple tabs on all browsers, including Safari. |
361364
362-
### HTTP Requirements
365+
### Clearing OPFS Storage
363366
364-
The use of `OPFS` requires a secure context and additional HTTP headers when serving a web application.
367+
Clearing `OPFS` storage isn’t as straightforward as clearing `IndexedDB` which can be done through browser developer tools.
368+
This requires developers to manually iterate through and delete files and directories.
365369
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-
});
370+
```js
371+
async function purgeVFS() {
372+
await powerSync.disconnect();
373+
await powerSync.close();
374+
375+
const root = await navigator.storage.getDirectory();
376+
377+
// .db-wal needs a moment to become deletable
378+
await new Promise((resolve) => setTimeout(resolve, 1));
379379

380+
for await (const [name, entry] of root.entries!()) {
381+
try {
382+
if (entry.kind === 'file') {
383+
console.log(`Deleted file: ${name}`);
384+
await root.removeEntry(name);
385+
} else if (entry.kind === 'directory') {
386+
await root.removeEntry(name, { recursive: true });
387+
console.log(`Deleted directory: ${name}`);
388+
}
389+
} catch (err) {
390+
console.error(`Failed to delete ${entry.kind}: ${name}`, err);
391+
}
392+
}
393+
}
394+
395+
async function listVfsEntries() {
396+
const root = await navigator.storage.getDirectory();
397+
398+
for await (const [name, entry] of root.entries()) {
399+
console.log(`Entry ${entry.kind}: ${name}`);
400+
}
401+
}
380402
```
381403
404+
382405
## ORM Support
383406
384407
See [JavaScript ORM Support](/client-sdk-references/javascript-web/javascript-orm/overview) for details.

0 commit comments

Comments
 (0)