diff --git a/packages/sds/src/message_channel/message_channel.spec.ts b/packages/sds/src/message_channel/message_channel.spec.ts index 461d1ba9c3..38b1881dad 100644 --- a/packages/sds/src/message_channel/message_channel.spec.ts +++ b/packages/sds/src/message_channel/message_channel.spec.ts @@ -1260,7 +1260,14 @@ describe("MessageChannel", function () { }); }); - describe("Default localStorage persistence", () => { + describe("localStorage persistence", function () { + // LocalStorage specific tests (browser) + before(function () { + if (typeof localStorage === "undefined") { + this.skip(); + } + }); + it("should restore messages from localStorage on channel recreation", async () => { const persistentChannelId = "persistent-channel"; diff --git a/packages/sds/src/message_channel/persistent_history.ts b/packages/sds/src/message_channel/persistent_history.ts index 7806476592..d575e1e3e7 100644 --- a/packages/sds/src/message_channel/persistent_history.ts +++ b/packages/sds/src/message_channel/persistent_history.ts @@ -1,8 +1,11 @@ import { bytesToHex, hexToBytes } from "@noble/hashes/utils"; +import { Logger } from "@waku/utils"; import { ILocalHistory, MemLocalHistory } from "./mem_local_history.js"; import { ChannelId, ContentMessage, HistoryEntry } from "./message.js"; +const log = new Logger("sds:persistent-history"); + export interface HistoryStorage { getItem(key: string): string | null; setItem(key: string, value: string): void; @@ -48,6 +51,13 @@ export class PersistentHistory implements ILocalHistory { this.memory = new MemLocalHistory(); this.storage = options.storage ?? DEFAULT_HISTORY_STORAGE; this.storageKey = `${HISTORY_STORAGE_PREFIX}${options.channelId}`; + + if (!this.storage) { + log.warn( + "Storage backend unavailable (localStorage not found). Falling back to in-memory history. Messages will not persist across sessions." + ); + } + this.load(); }