diff --git a/packages/editor/src/lib/config/TLSessionStateSnapshot.ts b/packages/editor/src/lib/config/TLSessionStateSnapshot.ts index cd309f1d9..a3315feda 100644 --- a/packages/editor/src/lib/config/TLSessionStateSnapshot.ts +++ b/packages/editor/src/lib/config/TLSessionStateSnapshot.ts @@ -50,8 +50,9 @@ function iOS() { * A string that is unique per browser tab * @public */ -export const TAB_ID: string = - window?.[tabIdKey] ?? window?.sessionStorage[tabIdKey] ?? `TLDRAW_INSTANCE_STATE_V1_` + uniqueId() +export const TAB_ID: string = window + ? window[tabIdKey] ?? window.sessionStorage[tabIdKey] ?? `TLDRAW_INSTANCE_STATE_V1_` + uniqueId() + : '' if (window) { window[tabIdKey] = TAB_ID if (iOS()) { diff --git a/packages/editor/src/lib/config/TLUserPreferences.ts b/packages/editor/src/lib/config/TLUserPreferences.ts index 1ad2925c6..8c6cf55ad 100644 --- a/packages/editor/src/lib/config/TLUserPreferences.ts +++ b/packages/editor/src/lib/config/TLUserPreferences.ts @@ -240,18 +240,24 @@ const channel = channel?.addEventListener('message', (e) => { const data = e.data as undefined | UserChangeBroadcastMessage - if (data?.type === broadcastEventKey && data?.origin !== broadcastOrigin) { + if (data?.type === broadcastEventKey && data?.origin !== getBroadcastOrigin()) { globalUserPreferences.set(migrateUserPreferences(data.data)) } }) -const broadcastOrigin = uniqueId() +let _broadcastOrigin = null as null | string +function getBroadcastOrigin() { + if (_broadcastOrigin === null) { + _broadcastOrigin = uniqueId() + } + return _broadcastOrigin +} const broadcastEventKey = 'tldraw-user-preferences-change' as const function broadcastUserPreferencesChange() { channel?.postMessage({ type: broadcastEventKey, - origin: broadcastOrigin, + origin: getBroadcastOrigin(), data: { user: getUserPreferences(), version: userMigrations.currentVersion,