Skip to content

Offline transactions applied twice on reload #921

@oskarrough

Description

@oskarrough

I'm trying to make use of the offline-transactions module together with a tanstack db query collection and think I found a bug.

When using @tanstack/offline-transactions, the loadAndReplayTransactions() method is called twice during initialization, causing persisted offline transactions to be executed multiple times (duplicating data).

To reproduce

  1. Create an offline executor with a mutation function
  2. Go offline
  3. Create a transaction (e.g., insert a record)
  4. Close the tab while still offline
  5. Reopen the tab (now online)
  6. The mutation executes 2+ times, creating duplicate records

Note, if I don't refresh the tab, and only switch from offline to online, it works! No duplicated data.

Expected behavior

loadAndReplayTransactions() should only be called once during initialization.
OR, there is some deduping going on?

Possible fix

Not being overly confident with how this all plays together, I believe the issue is that loadAndReplayTransactions() is called in both initialize() and in setupEventListeners() inside OfflineExecutor.ts.

My AI robot overlord suggests to add a guard, but not sure that's the cleanest way to deal with this.

  // In initialize():
  this._initialLoadDone = false;
  this.setupEventListeners();
  if (isLeader) {
    await this.loadAndReplayTransactions();
    this._initialLoadDone = true;
  }

  // In setupEventListeners():
  if (isLeader && this._initialLoadDone) {
    this.loadAndReplayTransactions();
  }

Desktop:

  • OS: OSX
  • Browser Chrome
  • Version 142
    "@tanstack/offline-transactions": "^1.0.0",
    "@tanstack/query-db-collection": "^1.0.5",
    "@tanstack/query-persist-client-core": "^5.91.10",
    "@tanstack/svelte-db": "^0.1.49",
    "@tanstack/svelte-query": "^6.0.9",

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions