Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8d8f8d6
feat(offline-transactions): implement offline-first transaction system
KyleAMathews Sep 15, 2025
8bd35ff
fix(offline-transactions): correct transaction flow and remove halluc…
KyleAMathews Sep 17, 2025
67ce2dc
chore: dependency updates and WebLocksLeader improvements
KyleAMathews Sep 17, 2025
49b9684
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Sep 17, 2025
e452c3f
fix(offline-transactions): fix TypeScript types and build errors
KyleAMathews Sep 17, 2025
21eddc9
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Sep 18, 2025
2dba86f
Add example site + test harness
KyleAMathews Sep 19, 2025
3338124
fix(offline-transactions): resolve transaction timeout issues during …
KyleAMathews Sep 22, 2025
dd819c8
Switch from parallel to sequential transaction processing
KyleAMathews Sep 22, 2025
bb34b35
Merge origin/main into offline-transactions
KyleAMathews Sep 22, 2025
63a6dbe
lint fix
KyleAMathews Sep 22, 2025
63ff2dd
remove mistakenly checked in files
KyleAMathews Sep 22, 2025
e65d198
revert changes in db package
KyleAMathews Sep 22, 2025
a0867c4
fix
KyleAMathews Sep 22, 2025
3294058
fix lock file
KyleAMathews Sep 22, 2025
9d9719e
format
KyleAMathews Sep 22, 2025
9b0a390
moer format
KyleAMathews Sep 22, 2025
9c9a8f1
tweaky
KyleAMathews Sep 22, 2025
1304b9c
Fix type
KyleAMathews Sep 22, 2025
954271b
lock file
KyleAMathews Sep 25, 2025
2b6a7e2
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Sep 25, 2025
fd59893
chore(examples): upgrade todo example to TanStack Start v2
KyleAMathews Oct 1, 2025
35ae086
feat(offline-transactions): add retry logic to sync operations
KyleAMathews Oct 1, 2025
e830497
Add test
KyleAMathews Oct 1, 2025
b71a289
catchup
KyleAMathews Oct 1, 2025
73f581f
Add otel
KyleAMathews Oct 1, 2025
600ac24
fix eslint
KyleAMathews Oct 1, 2025
5c4dcf1
Merge origin/main into offline-transactions
KyleAMathews Oct 1, 2025
cd7dd7a
format
KyleAMathews Oct 1, 2025
3531de1
format
KyleAMathews Oct 2, 2025
6d9daa2
publish
KyleAMathews Oct 2, 2025
3a71f4e
correctly store spans offline
KyleAMathews Oct 2, 2025
e44f131
lint fix
KyleAMathews Oct 2, 2025
4cf5788
enforce onMutate is sync
KyleAMathews Oct 15, 2025
2a7b02f
feat(db): detect and throw error on duplicate @tanstack/db instances
KyleAMathews Oct 15, 2025
9357b08
fix(offline-transactions): use proper peerDependency version range
KyleAMathews Oct 15, 2025
e8d2915
Merge origin/main into offline-transactions
KyleAMathews Oct 15, 2025
81bfbbd
fix
KyleAMathews Oct 15, 2025
3f3550d
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Oct 20, 2025
8fc127f
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Oct 22, 2025
6c6d92d
feat(offline-transactions): add storage capability detection and grac…
KyleAMathews Oct 22, 2025
f8e0b40
fix(offline-transactions): suppress unused storage variable warning
KyleAMathews Oct 22, 2025
1e3bbaa
Merge remote-tracking branch 'origin/main' into offline-transactions
KyleAMathews Oct 22, 2025
311cd40
fix(offline-transactions): add initialization promise to prevent race…
KyleAMathews Oct 22, 2025
a2c680a
feat(db): improve duplicate instance detection with environment guards
KyleAMathews Oct 22, 2025
0d9da6f
refactor: remove OpenTelemetry from offline-transactions example
KyleAMathews Oct 22, 2025
9176d82
test(offline-transactions): add leader failover unit tests
KyleAMathews Oct 22, 2025
7a011c0
test(offline-transactions): add storage failure tests
KyleAMathews Oct 22, 2025
daa4b78
chore: add changeset for offline-transactions initial release
KyleAMathews Oct 22, 2025
2d78d14
prettier
KyleAMathews Oct 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .changeset/offline-transactions-initial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
"@tanstack/offline-transactions": minor
"@tanstack/db": patch
---

Add offline-transactions package with robust offline-first capabilities

New package `@tanstack/offline-transactions` provides a comprehensive offline-first transaction system with:

**Core Features:**

- Persistent outbox pattern for reliable transaction processing
- Leader election for multi-tab coordination (Web Locks API with BroadcastChannel fallback)
- Automatic storage capability detection with graceful degradation
- Retry logic with exponential backoff and jitter
- Sequential transaction processing (FIFO ordering)

**Storage:**

- Automatic fallback chain: IndexedDB β†’ localStorage β†’ online-only
- Detects and handles private mode, SecurityError, QuotaExceededError
- Custom storage adapter support
- Diagnostic callbacks for storage failures

**Developer Experience:**

- TypeScript-first with full type safety
- Comprehensive test suite (25 tests covering leader failover, storage failures, e2e scenarios)
- Works in all modern browsers and server-side rendering environments

**@tanstack/db improvements:**

- Enhanced duplicate instance detection (dev-only, iframe-aware, with escape hatch)
- Better environment detection for SSR and worker contexts

Example usage:

```typescript
import {
startOfflineExecutor,
IndexedDBAdapter,
} from "@tanstack/offline-transactions"

const executor = startOfflineExecutor({
collections: { todos: todoCollection },
storage: new IndexedDBAdapter(),
mutationFns: {
syncTodos: async ({ transaction, idempotencyKey }) => {
// Sync mutations to backend
await api.sync(transaction.mutations, idempotencyKey)
},
},
onStorageFailure: (diagnostic) => {
console.warn("Running in online-only mode:", diagnostic.message)
},
})

// Create offline transaction
const tx = executor.createOfflineTransaction({
mutationFnName: "syncTodos",
autoCommit: false,
})

tx.mutate(() => {
todoCollection.insert({ id: "1", text: "Buy milk", completed: false })
})

await tx.commit() // Persists to outbox and syncs when online
```
25 changes: 25 additions & 0 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function readPackage(pkg, context) {
// Force all @tanstack/db dependencies to resolve to workspace version
if (pkg.dependencies && pkg.dependencies["@tanstack/db"]) {
pkg.dependencies["@tanstack/db"] = "workspace:*"
context.log(`Overriding @tanstack/db dependency in ${pkg.name}`)
}

if (pkg.devDependencies && pkg.devDependencies["@tanstack/db"]) {
pkg.devDependencies["@tanstack/db"] = "workspace:*"
context.log(`Overriding @tanstack/db devDependency in ${pkg.name}`)
}

if (pkg.peerDependencies && pkg.peerDependencies["@tanstack/db"]) {
pkg.peerDependencies["@tanstack/db"] = "workspace:*"
context.log(`Overriding @tanstack/db peerDependency in ${pkg.name}`)
}

return pkg
}

module.exports = {
hooks: {
readPackage,
},
}
3 changes: 3 additions & 0 deletions examples/react/offline-transactions/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Honeycomb API Key
# Get your API key from https://ui.honeycomb.io/account
HONEYCOMB_API_KEY=your_api_key_here
20 changes: 20 additions & 0 deletions examples/react/offline-transactions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
node_modules
package-lock.json
yarn.lock

.DS_Store
.cache
.env
.vercel
.output
.nitro
/build/
/api/
/server/build
/public/build# Sentry Config File
.env.sentry-build-plugin
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
.tanstack
4 changes: 4 additions & 0 deletions examples/react/offline-transactions/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/build
**/public
pnpm-lock.yaml
routeTree.gen.ts
72 changes: 72 additions & 0 deletions examples/react/offline-transactions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Welcome to TanStack.com!

This site is built with TanStack Router!

- [TanStack Router Docs](https://tanstack.com/router)

It's deployed automagically with Netlify!

- [Netlify](https://netlify.com/)

## Development

From your terminal:

```sh
pnpm install
pnpm dev
```

This starts your app in development mode, rebuilding assets on file changes.

## Editing and previewing the docs of TanStack projects locally

The documentations for all TanStack projects except for `React Charts` are hosted on [https://tanstack.com](https://tanstack.com), powered by this TanStack Router app.
In production, the markdown doc pages are fetched from the GitHub repos of the projects, but in development they are read from the local file system.

Follow these steps if you want to edit the doc pages of a project (in these steps we'll assume it's [`TanStack/form`](https://github.com/tanstack/form)) and preview them locally :

1. Create a new directory called `tanstack`.

```sh
mkdir tanstack
```

2. Enter the directory and clone this repo and the repo of the project there.

```sh
cd tanstack
git clone [email protected]:TanStack/tanstack.com.git
git clone [email protected]:TanStack/form.git
```

> [!NOTE]
> Your `tanstack` directory should look like this:
>
> ```
> tanstack/
> |
> +-- form/
> |
> +-- tanstack.com/
> ```
> [!WARNING]
> Make sure the name of the directory in your local file system matches the name of the project's repo. For example, `tanstack/form` must be cloned into `form` (this is the default) instead of `some-other-name`, because that way, the doc pages won't be found.
3. Enter the `tanstack/tanstack.com` directory, install the dependencies and run the app in dev mode:
```sh
cd tanstack.com
pnpm i
# The app will run on https://localhost:3000 by default
pnpm dev
```
4. Now you can visit http://localhost:3000/form/latest/docs/overview in the browser and see the changes you make in `tanstack/form/docs`.

> [!NOTE]
> The updated pages need to be manually reloaded in the browser.
> [!WARNING]
> You will need to update the `docs/config.json` file (in the project's repo) if you add a new doc page!
37 changes: 37 additions & 0 deletions examples/react/offline-transactions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "tanstack-start-example-basic",
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build && tsc --noEmit",
"start": "node .output/server/index.mjs"
},
"dependencies": {
"@tanstack/offline-transactions": "workspace:*",
"@tanstack/query-db-collection": "workspace:*",
"@tanstack/react-db": "workspace:*",
"@tanstack/react-query": "^5.89.0",
"@tanstack/react-router": "^1.131.47",
"@tanstack/react-router-devtools": "^1.131.47",
"@tanstack/react-start": "^1.131.47",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"tailwind-merge": "^2.6.0",
"zod": "^3.24.2"
},
"devDependencies": {
"@types/node": "^22.5.4",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"@vitejs/plugin-react": "^5.0.3",
"autoprefixer": "^10.4.20",
"chokidar": "^4.0.3",
"postcss": "^8.5.1",
"tailwindcss": "^3.4.17",
"typescript": "^5.7.2",
"vite": "^7.1.7",
"vite-tsconfig-paths": "^5.1.4"
}
}
6 changes: 6 additions & 0 deletions examples/react/offline-transactions/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions examples/react/offline-transactions/public/site.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "",
"short_name": "",
"icons": [
{
"src": "/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
ErrorComponent,
Link,
rootRouteId,
useMatch,
useRouter,
} from "@tanstack/react-router"
import type { ErrorComponentProps } from "@tanstack/react-router"

export function DefaultCatchBoundary({ error }: ErrorComponentProps) {
const router = useRouter()
const isRoot = useMatch({
strict: false,
select: (state) => state.id === rootRouteId,
})

console.error(`DefaultCatchBoundary Error:`, error)

return (
<div className="min-w-0 flex-1 p-4 flex flex-col items-center justify-center gap-6">
<ErrorComponent error={error} />
<div className="flex gap-2 items-center flex-wrap">
<button
onClick={() => {
router.invalidate()
}}
className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded text-white uppercase font-extrabold`}
>
Try Again
</button>
{isRoot ? (
<Link
to="/"
className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded text-white uppercase font-extrabold`}
>
Home
</Link>
) : (
<Link
to="/"
className={`px-2 py-1 bg-gray-600 dark:bg-gray-700 rounded text-white uppercase font-extrabold`}
onClick={(e) => {
e.preventDefault()
window.history.back()
}}
>
Go Back
</Link>
)}
</div>
</div>
)
}
25 changes: 25 additions & 0 deletions examples/react/offline-transactions/src/components/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Link } from "@tanstack/react-router"

export function NotFound({ children }: { children?: any }) {
return (
<div className="space-y-2 p-2">
<div className="text-gray-600 dark:text-gray-400">
{children || <p>The page you are looking for does not exist.</p>}
</div>
<p className="flex items-center gap-2 flex-wrap">
<button
onClick={() => window.history.back()}
className="bg-emerald-500 text-white px-2 py-1 rounded uppercase font-black text-sm"
>
Go back
</button>
<Link
to="/"
className="bg-cyan-600 text-white px-2 py-1 rounded uppercase font-black text-sm"
>
Start Over
</Link>
</p>
</div>
)
}
Loading
Loading