Skip to content

Commit b1a76b3

Browse files
[Fix] SSR Mode Detection (#296)
1 parent c04ecfc commit b1a76b3

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

.changeset/four-birds-raise.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@powersync/common': minor
3+
'@powersync/web': minor
4+
---
5+
6+
Fixed SSR Mode detection for DB adapters. Removed the potential for SSR Web Streamining implementations from to perform syncing operations.

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { CrudBatch } from './sync/bucket/CrudBatch';
2323
import { CrudEntry, CrudEntryJSON } from './sync/bucket/CrudEntry';
2424
import { CrudTransaction } from './sync/bucket/CrudTransaction';
2525
import {
26-
AbstractStreamingSyncImplementation,
2726
DEFAULT_CRUD_UPLOAD_THROTTLE_MS,
2827
PowerSyncConnectionOptions,
2928
StreamingSyncImplementation,
@@ -239,7 +238,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
239238

240239
protected abstract generateSyncStreamImplementation(
241240
connector: PowerSyncBackendConnector
242-
): AbstractStreamingSyncImplementation;
241+
): StreamingSyncImplementation;
243242

244243
protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
245244

packages/web/src/db/PowerSyncDatabase.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
type AbstractStreamingSyncImplementation,
32
type BucketStorageAdapter,
43
type PowerSyncBackendConnector,
54
type PowerSyncCloseOptions,
@@ -11,7 +10,8 @@ import {
1110
PowerSyncDatabaseOptionsWithDBAdapter,
1211
PowerSyncDatabaseOptionsWithOpenFactory,
1312
PowerSyncDatabaseOptionsWithSettings,
14-
SqliteBucketStorage
13+
SqliteBucketStorage,
14+
StreamingSyncImplementation
1515
} from '@powersync/common';
1616
import { Mutex } from 'async-mutex';
1717
import { WASQLiteOpenFactory } from './adapters/wa-sqlite/WASQLiteOpenFactory';
@@ -138,9 +138,7 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
138138
return navigator.locks.request(`lock-${this.database.name}`, cb);
139139
}
140140

141-
protected generateSyncStreamImplementation(
142-
connector: PowerSyncBackendConnector
143-
): AbstractStreamingSyncImplementation {
141+
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector): StreamingSyncImplementation {
144142
const remote = new WebRemote(connector);
145143

146144
const syncOptions: WebStreamingSyncImplementationOptions = {

packages/web/src/db/adapters/AbstractWebSQLOpenFactory.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
1919
* A SSR implementation is loaded if SSR mode is detected.
2020
*/
2121
openDB() {
22-
const isSSR = isServerSide();
23-
if (isSSR && !this.resolvedFlags.disableSSRWarning) {
22+
const {
23+
resolvedFlags: { disableSSRWarning, enableMultiTabs, ssrMode = isServerSide() }
24+
} = this;
25+
if (ssrMode && !disableSSRWarning) {
2426
console.warn(
2527
`
2628
Running PowerSync in SSR mode.
@@ -29,13 +31,13 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
2931
);
3032
}
3133

32-
if (!this.resolvedFlags.enableMultiTabs) {
34+
if (!enableMultiTabs) {
3335
console.warn(
3436
'Multiple tab support is not enabled. Using this site across multiple tabs may not function correctly.'
3537
);
3638
}
3739

38-
if (isSSR) {
40+
if (ssrMode) {
3941
return new SSRDBAdapter();
4042
}
4143

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,76 @@
11
import {
2-
AbstractStreamingSyncImplementation,
32
AbstractStreamingSyncImplementationOptions,
3+
BaseObserver,
44
LockOptions,
5-
LockType
5+
LockType,
6+
PowerSyncConnectionOptions,
7+
StreamingSyncImplementation,
8+
SyncStatus,
9+
SyncStatusOptions
610
} from '@powersync/common';
711
import { Mutex } from 'async-mutex';
812

9-
export class SSRStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
13+
export class SSRStreamingSyncImplementation extends BaseObserver implements StreamingSyncImplementation {
1014
syncMutex: Mutex;
1115
crudMutex: Mutex;
1216

17+
isConnected: boolean;
18+
lastSyncedAt?: Date | undefined;
19+
syncStatus: SyncStatus;
20+
1321
constructor(options: AbstractStreamingSyncImplementationOptions) {
14-
super(options);
22+
super();
1523
this.syncMutex = new Mutex();
1624
this.crudMutex = new Mutex();
25+
this.syncStatus = new SyncStatus({});
26+
this.isConnected = false;
1727
}
1828

1929
obtainLock<T>(lockOptions: LockOptions<T>): Promise<T> {
2030
const mutex = lockOptions.type == LockType.CRUD ? this.crudMutex : this.syncMutex;
2131
return mutex.runExclusive(lockOptions.callback);
2232
}
33+
34+
/**
35+
* This is a no-op in SSR mode
36+
*/
37+
async connect(options?: PowerSyncConnectionOptions): Promise<void> {}
38+
39+
async dispose() {}
40+
41+
/**
42+
* This is a no-op in SSR mode
43+
*/
44+
async disconnect(): Promise<void> {}
45+
46+
/**
47+
* This SSR Mode implementation is immediately ready.
48+
*/
49+
async waitForReady() {}
50+
51+
/**
52+
* This will never resolve in SSR Mode.
53+
*/
54+
async waitForStatus(status: SyncStatusOptions) {
55+
return new Promise<void>((r) => {});
56+
}
57+
58+
/**
59+
* Returns a placeholder checkpoint. This should not be used.
60+
*/
61+
async getWriteCheckpoint() {
62+
return '1';
63+
}
64+
65+
/**
66+
* The SSR mode adapter will never complete syncing.
67+
*/
68+
async hasCompletedSync() {
69+
return false;
70+
}
71+
72+
/**
73+
* This is a no-op in SSR mode.
74+
*/
75+
triggerCrudUpload() {}
2376
}

0 commit comments

Comments
 (0)