@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
120 lines (82 loc) • 4.52 kB
Markdown
name: offline
description: >-
Let the published app open without internet and show the pages and data the
visitor last loaded, read only: useOfflineMode, the OfflineMode type, the
offline banner and saves made offline. The service worker is already served,
never write one.
metadata:
type: sub-skill
library: softr-sdk
library_version: 1.19.0
# Offline mode
With offline mode on, the published app opens without internet and shows the pages and data the visitor already loaded while online, as they were then, sometimes a little older. Pages the visitor never opened are not there: a link to one shows the app's error page. Offline mode keeps nothing to save later: a save made offline fails as it would on any network error, and the app tells the visitor, unless the app has its own offline saving.
> **CRITICAL**: Never write a service worker, a `public/sw.js`, or any `navigator.serviceWorker` or `caches` code for offline. The app already serves its service worker, shared with push notifications, and `useOfflineMode()` turns offline mode on. A worker of the app's own replaces it, and push notifications stop arriving.
>
> **CRITICAL**: `useOfflineMode()` keeps reads only. Saving offline is app code, built only when the builder asks for it: the changes wait in IndexedDB in the page and are sent when the connection is back. It never goes into a service worker.
## Setup
```ts
import { useOfflineMode } from '@softr/sdk'
import type { OfflineMode, OfflineModeOptions } from '@softr/sdk'
```
```ts
type OfflineModeOptions = {
enabled?: boolean // false turns offline mode off and deletes what browsers kept; defaults to true
}
type OfflineMode = {
online: boolean // false while the browser has no connection, or the app shows saved copies because it cannot reach the server; always true during server rendering
}
```
## Core Patterns
### Turning it on
Call `useOfflineMode()` once, in `RootDocument` in `src/routes/__root.tsx`, so every page is kept. Call it nowhere else: each call also turns offline mode on or off. Render the banner from the same place. Use the banner component the template ships when it has one.
```tsx
function OfflineBanner() {
const { online } = useOfflineMode()
if (online) return null
return <div role="status">You're offline. Showing the last saved data.</div>
}
```
Offline mode works only on the published app. Inside the Studio preview `online` still follows the connection, but nothing is kept on the device. That is expected, not a bug to fix.
### Saves made offline
A save made offline rejects like any failed request. Catch it as usual, and when `navigator.onLine` is `false`, say that the visitor is offline rather than showing a generic error. Do not call `useOfflineMode()` in the form for this:
```tsx
import { toast } from 'sonner'
// `save` is the form's own server function.
save(values).catch(() =>
toast.error(navigator.onLine ? 'Could not save your changes.' : "You're offline. Connect to save your changes."),
)
```
### Signing out
Nothing to add. The copies kept on the device are deleted when the visitor signs out, or when someone else signs in on the same browser.
### Turning it off
Remove the banner, and keep one call in `RootDocument` as `useOfflineMode({ enabled: false })`. That deletes what visitors' browsers kept the next time they open the app. Removing the call instead leaves offline mode on in browsers that ran it: they keep the visitor's copies, and keep saving new ones, until the visitor signs out or someone else signs in.
## Common Mistakes
### CRITICAL Writing a service worker for offline
Wrong:
```ts
navigator.serviceWorker.register('/sw.js')
```
Correct:
```tsx
const { online } = useOfflineMode()
```
The app serves one service worker for push and offline mode. A worker of the app's own replaces it, so push notifications stop and nothing is kept offline.
Source: packages/sdk/src/offline.ts
### HIGH Calling the hook only on some pages
Wrong:
```tsx
// src/routes/_app/orders.tsx
const { online } = useOfflineMode()
```
Correct:
```tsx
// RootDocument in src/routes/__root.tsx
<OfflineBanner />
```
A page is kept for offline only while the hook runs. Called from one page, every other page is missing offline.
Source: packages/sdk/src/offline.ts
## References
- [SKILL.md](../SKILL.md): the entrypoints and which one each export lives on
- [push-notifications/SKILL.md](../push-notifications/SKILL.md): push notifications, which share the same service worker