@softr/sdk 1.15.2 → 1.17.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/offline.d.ts +24 -0
- package/dist/offline.js +95 -0
- package/dist/package.js +1 -1
- package/dist/push.js +5 -4
- package/package.json +38 -11
- package/skills/softr-sdk/SKILL.md +5 -1
- package/skills/softr-sdk/ai/SKILL.md +1 -1
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +1 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/email/SKILL.md +1 -1
- package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
- package/skills/softr-sdk/formatting/SKILL.md +1 -1
- package/skills/softr-sdk/integrations/SKILL.md +1 -1
- package/skills/softr-sdk/mcp/SKILL.md +1 -1
- package/skills/softr-sdk/offline/SKILL.md +113 -0
- package/skills/softr-sdk/push-notifications/SKILL.md +1 -1
- package/skills/softr-sdk/pwa/SKILL.md +1 -1
- package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
- package/skills/softr-sdk/record-queries/SKILL.md +1 -1
- package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -21,8 +21,9 @@ import { useOverrides } from "./core/use-overrides.js";
|
|
|
21
21
|
import { useUserTimezone } from "./timezone.js";
|
|
22
22
|
import { PwaInstall, PwaPlatform, SoftrPwaHead, usePwaInstall } from "./pwa.js";
|
|
23
23
|
import { PushNotifications, PushPrompt, usePushNotifications } from "./push.js";
|
|
24
|
+
import { OfflineMode, useOfflineMode } from "./offline.js";
|
|
24
25
|
import { DefaultErrorOverlay } from "./default-error-overlay.js";
|
|
25
26
|
import { Audience } from "./route/audience.js";
|
|
26
27
|
import { Logo, LogoFile } from "./softr-config.schema.js";
|
|
27
28
|
import { getLogo } from "./softr-config.js";
|
|
28
|
-
export { type ActionPermissions, type AiAnswer, type AiFile, type AiGenerateInit, type AiMessage, type AiUsage, Audience, type AuthOptions, type AuthSettings, AuthSettingsProvider, type DateOptions, type DateParts, type DateSeparator, type DateValue, DefaultErrorOverlay, type EmailErrorCode, type EmailSendInit, type EmailSent, type FieldShape, type FileAttachment, type FileItem, type FixedDateFormat, FormattedDate, type Formatting, FormattingScope, type Logo, type LogoFile, type Page, type PageSettings, type PushNotifications, type PushPrompt, type PwaInstall, type PwaPlatform, type SoftrContext, type SoftrDatasources, type SoftrFieldRegistry, SoftrPwaHead, type SoftrUserGroupId, type SoftrUserGroups, type SoftrUserProperties, type SsoError, type Tool, type ToolDefinition, type User, type WeekDay, assertRouteAccess, checkAccess, defineAiAction, defineEmailAction, defineIntegration, defineMutation, definePage, defineQuery, defineTool, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getLogo, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, ssoSignInHref, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
|
29
|
+
export { type ActionPermissions, type AiAnswer, type AiFile, type AiGenerateInit, type AiMessage, type AiUsage, Audience, type AuthOptions, type AuthSettings, AuthSettingsProvider, type DateOptions, type DateParts, type DateSeparator, type DateValue, DefaultErrorOverlay, type EmailErrorCode, type EmailSendInit, type EmailSent, type FieldShape, type FileAttachment, type FileItem, type FixedDateFormat, FormattedDate, type Formatting, FormattingScope, type Logo, type LogoFile, type OfflineMode, type Page, type PageSettings, type PushNotifications, type PushPrompt, type PwaInstall, type PwaPlatform, type SoftrContext, type SoftrDatasources, type SoftrFieldRegistry, SoftrPwaHead, type SoftrUserGroupId, type SoftrUserGroups, type SoftrUserProperties, type SsoError, type Tool, type ToolDefinition, type User, type WeekDay, assertRouteAccess, checkAccess, defineAiAction, defineEmailAction, defineIntegration, defineMutation, definePage, defineQuery, defineTool, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getLogo, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, ssoSignInHref, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOfflineMode, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { toReturnPath } from "./auth/returnPath.js";
|
|
|
8
8
|
import { ssoSignInHref } from "./auth/sso.js";
|
|
9
9
|
import { FormattedDate, FormattingScope, formatDate, formatDateTime, formatNumber, useFormatting } from "./formatting.js";
|
|
10
10
|
import { SoftrPwaHead, usePwaInstall } from "./pwa.js";
|
|
11
|
+
import { useOfflineMode } from "./offline.js";
|
|
11
12
|
import { usePushNotifications } from "./push.js";
|
|
12
13
|
import { AuthSettingsProvider, getAuthSettings, getSoftrContext, initialSoftrContext, softrAuthMiddleware, useAuthSettings, useCurrentUser } from "./auth/isomorphic.js";
|
|
13
14
|
import { useIdleTimeout } from "./auth/idle-timeout.js";
|
|
@@ -19,4 +20,4 @@ import { useUserTimezone } from "./timezone.js";
|
|
|
19
20
|
import { DefaultErrorOverlay } from "./default-error-overlay.js";
|
|
20
21
|
import { Audience } from "./route/audience.js";
|
|
21
22
|
|
|
22
|
-
export { Audience, AuthSettingsProvider, DefaultErrorOverlay, FormattedDate, FormattingScope, SoftrPwaHead, assertRouteAccess, checkAccess, defineAiAction, defineEmailAction, defineIntegration, defineMutation, definePage, defineQuery, defineTool, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getLogo, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, ssoSignInHref, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
|
23
|
+
export { Audience, AuthSettingsProvider, DefaultErrorOverlay, FormattedDate, FormattingScope, SoftrPwaHead, assertRouteAccess, checkAccess, defineAiAction, defineEmailAction, defineIntegration, defineMutation, definePage, defineQuery, defineTool, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getLogo, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, ssoSignInHref, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOfflineMode, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/offline.d.ts
|
|
2
|
+
|
|
3
|
+
type OfflineMode = {
|
|
4
|
+
/** `false` while the browser has no connection. Always `true` during server rendering. */
|
|
5
|
+
online: boolean;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Lets the published app open without internet and show the pages and data
|
|
9
|
+
* the visitor last loaded, read only. Saves made offline fail as usual.
|
|
10
|
+
*
|
|
11
|
+
* Call it once, in the root route's `RootDocument`. The saved copies are
|
|
12
|
+
* deleted when another visitor signs in on this browser, or when the visitor
|
|
13
|
+
* signs out. In the Studio preview it only reports `online`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* function OfflineBanner() {
|
|
17
|
+
* const { online } = useOfflineMode()
|
|
18
|
+
* if (online) return null
|
|
19
|
+
* return <div role="status">You're offline. Showing the last saved data.</div>
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
declare function useOfflineMode(): OfflineMode;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { OfflineMode, useOfflineMode };
|
package/dist/offline.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useEffect, useSyncExternalStore } from "react";
|
|
2
|
+
import { rootRouteId, useLocation, useRouteContext } from "@tanstack/react-router";
|
|
3
|
+
|
|
4
|
+
//#region src/offline.ts
|
|
5
|
+
const WORKER_PATH = "/_softr/sw.js";
|
|
6
|
+
const CACHE = "softr-offline-v1";
|
|
7
|
+
const OWNER_KEY = "/_softr/offline-owner";
|
|
8
|
+
/**
|
|
9
|
+
* Added to the service worker the app serves. Same-origin GET requests go to
|
|
10
|
+
* the network first, and the last good answer is kept on the device for when
|
|
11
|
+
* the network fails. Writes are never kept.
|
|
12
|
+
*
|
|
13
|
+
* It does nothing until `useOfflineMode()` creates the cache, so an app that
|
|
14
|
+
* registers the worker only for push keeps working as before.
|
|
15
|
+
*/
|
|
16
|
+
const OFFLINE_WORKER = `
|
|
17
|
+
// Offline mode, added by @softr/sdk.
|
|
18
|
+
self.addEventListener('fetch', (event) => {
|
|
19
|
+
const request = event.request;
|
|
20
|
+
if (request.method !== 'GET') return;
|
|
21
|
+
const url = new URL(request.url);
|
|
22
|
+
if (url.origin !== self.location.origin) return;
|
|
23
|
+
if (url.pathname.startsWith('/_softr/') || url.pathname.startsWith('/.softr/')) return;
|
|
24
|
+
event.respondWith((async () => {
|
|
25
|
+
if (!(await caches.has(${JSON.stringify(CACHE)}))) return fetch(request);
|
|
26
|
+
const cache = await caches.open(${JSON.stringify(CACHE)});
|
|
27
|
+
try {
|
|
28
|
+
const response = await fetch(request);
|
|
29
|
+
if (response.status === 200 && response.type === 'basic') {
|
|
30
|
+
event.waitUntil(cache.put(request, response.clone()).catch(() => {}));
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
const copy = await cache.match(request);
|
|
35
|
+
if (copy) return copy;
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
})());
|
|
39
|
+
});
|
|
40
|
+
`;
|
|
41
|
+
/**
|
|
42
|
+
* Lets the published app open without internet and show the pages and data
|
|
43
|
+
* the visitor last loaded, read only. Saves made offline fail as usual.
|
|
44
|
+
*
|
|
45
|
+
* Call it once, in the root route's `RootDocument`. The saved copies are
|
|
46
|
+
* deleted when another visitor signs in on this browser, or when the visitor
|
|
47
|
+
* signs out. In the Studio preview it only reports `online`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* function OfflineBanner() {
|
|
51
|
+
* const { online } = useOfflineMode()
|
|
52
|
+
* if (online) return null
|
|
53
|
+
* return <div role="status">You're offline. Showing the last saved data.</div>
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
function useOfflineMode() {
|
|
57
|
+
const userId = useRouteContext({
|
|
58
|
+
from: rootRouteId,
|
|
59
|
+
select: (context) => context.user?.id ?? null
|
|
60
|
+
});
|
|
61
|
+
const href = useLocation({ select: (location) => location.href });
|
|
62
|
+
const online = useSyncExternalStore(subscribeToConnection, () => navigator.onLine, () => true);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (!isOn()) return;
|
|
65
|
+
(async () => {
|
|
66
|
+
await keepCopiesOnlyFor(userId ?? "");
|
|
67
|
+
await navigator.serviceWorker.register(WORKER_PATH, { scope: "/" });
|
|
68
|
+
if (!navigator.onLine) return;
|
|
69
|
+
const cache = await caches.open(CACHE);
|
|
70
|
+
if (await cache.match(href) === void 0) await cache.add(href);
|
|
71
|
+
})().catch((error) => console.warn("[@softr/sdk] Offline mode could not save this page:", error));
|
|
72
|
+
}, [userId, href]);
|
|
73
|
+
return { online };
|
|
74
|
+
}
|
|
75
|
+
function isOn() {
|
|
76
|
+
return process.env.NODE_ENV === "production" && "serviceWorker" in navigator && "caches" in window;
|
|
77
|
+
}
|
|
78
|
+
/** Deletes the copies when they belong to someone else, then marks them as this visitor's. */
|
|
79
|
+
async function keepCopiesOnlyFor(owner) {
|
|
80
|
+
const stored = await (await caches.open(CACHE)).match(OWNER_KEY);
|
|
81
|
+
if (stored !== void 0 && await stored.text() === owner) return;
|
|
82
|
+
await caches.delete(CACHE);
|
|
83
|
+
await (await caches.open(CACHE)).put(OWNER_KEY, new Response(owner));
|
|
84
|
+
}
|
|
85
|
+
function subscribeToConnection(onChange) {
|
|
86
|
+
window.addEventListener("online", onChange);
|
|
87
|
+
window.addEventListener("offline", onChange);
|
|
88
|
+
return () => {
|
|
89
|
+
window.removeEventListener("online", onChange);
|
|
90
|
+
window.removeEventListener("offline", onChange);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { OFFLINE_WORKER, useOfflineMode };
|
package/dist/package.js
CHANGED
package/dist/push.js
CHANGED
|
@@ -4,6 +4,7 @@ import { sessionConfig } from "./auth/config.js";
|
|
|
4
4
|
import { readSessionToken } from "./auth/session.js";
|
|
5
5
|
import { readCached } from "./cache.js";
|
|
6
6
|
import { detectPlatform, isStandalone } from "./pwa.js";
|
|
7
|
+
import { OFFLINE_WORKER } from "./offline.js";
|
|
7
8
|
import { z } from "zod";
|
|
8
9
|
import { useCallback, useEffect, useState } from "react";
|
|
9
10
|
import { createMiddleware } from "@tanstack/react-start";
|
|
@@ -55,9 +56,9 @@ function readWorker() {
|
|
|
55
56
|
});
|
|
56
57
|
}
|
|
57
58
|
/**
|
|
58
|
-
* Serves push notifications from the app's own origin: the service worker,
|
|
59
|
-
* app's push settings, and saving or removing
|
|
60
|
-
* the signed-in visitor. Every other request passes through untouched.
|
|
59
|
+
* Serves push notifications from the app's own origin: the service worker,
|
|
60
|
+
* which also runs offline mode, the app's push settings, and saving or removing
|
|
61
|
+
* this browser's subscription for the signed-in visitor. Every other request passes through untouched.
|
|
61
62
|
*/
|
|
62
63
|
function pushMiddleware() {
|
|
63
64
|
return createMiddleware({ type: "request" }).server(async ({ next, pathname, request }) => {
|
|
@@ -67,7 +68,7 @@ function pushMiddleware() {
|
|
|
67
68
|
status: 503,
|
|
68
69
|
headers: { "cache-control": "no-store" }
|
|
69
70
|
});
|
|
70
|
-
return new Response(worker, { headers: {
|
|
71
|
+
return new Response(worker + OFFLINE_WORKER, { headers: {
|
|
71
72
|
"content-type": "text/javascript; charset=utf-8",
|
|
72
73
|
"cache-control": "no-cache",
|
|
73
74
|
"service-worker-allowed": "/"
|
package/package.json
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softr/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
".": {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"./
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./client": {
|
|
11
|
+
"types": "./dist/client.d.ts",
|
|
12
|
+
"import": "./dist/client.js"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server.d.ts",
|
|
16
|
+
"import": "./dist/server.js"
|
|
17
|
+
},
|
|
18
|
+
"./vite": {
|
|
19
|
+
"types": "./dist/vite.d.ts",
|
|
20
|
+
"import": "./dist/vite.js"
|
|
21
|
+
},
|
|
22
|
+
"./logo-icon": {
|
|
23
|
+
"types": "./dist/logo-icon.d.ts",
|
|
24
|
+
"import": "./dist/logo-icon.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"softr-sdk": "./dist/cli.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"schema",
|
|
33
|
+
"skills",
|
|
34
|
+
"!skills/_artifacts",
|
|
35
|
+
"CHANGELOG.md"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
15
40
|
"scripts": {
|
|
16
41
|
"build": "tsdown",
|
|
17
42
|
"test": "node --import ./src/test-utils/setup.ts --test",
|
|
@@ -43,7 +68,9 @@
|
|
|
43
68
|
"optional": true
|
|
44
69
|
}
|
|
45
70
|
},
|
|
46
|
-
"keywords": [
|
|
71
|
+
"keywords": [
|
|
72
|
+
"tanstack-intent"
|
|
73
|
+
],
|
|
47
74
|
"intent": {
|
|
48
75
|
"version": 1,
|
|
49
76
|
"repo": "@softr/sdk",
|
|
@@ -11,7 +11,7 @@ description: >-
|
|
|
11
11
|
metadata:
|
|
12
12
|
type: core
|
|
13
13
|
library: softr-sdk
|
|
14
|
-
library_version: 1.
|
|
14
|
+
library_version: 1.16.0
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
# Softr SDK core
|
|
@@ -57,6 +57,7 @@ Each sub-skill stands alone for its task and repeats the boundary above, so most
|
|
|
57
57
|
| Open the app to AI assistants over MCP | [mcp/SKILL.md](./mcp/SKILL.md) |
|
|
58
58
|
| Let visitors install the app on their home screen | [pwa/SKILL.md](./pwa/SKILL.md) |
|
|
59
59
|
| Let visitors turn on push notifications | [push-notifications/SKILL.md](./push-notifications/SKILL.md) |
|
|
60
|
+
| Let the app open without internet, or show that the visitor is offline | [offline/SKILL.md](./offline/SKILL.md) |
|
|
60
61
|
| Show a date, a time or a number | [formatting/SKILL.md](./formatting/SKILL.md) |
|
|
61
62
|
|
|
62
63
|
## Quick Decision Tree
|
|
@@ -104,6 +105,9 @@ Adding an install button, or making the app installable?
|
|
|
104
105
|
Letting visitors turn on push notifications, or "notify users when …"?
|
|
105
106
|
load softr-sdk/push-notifications
|
|
106
107
|
|
|
108
|
+
Letting the app work offline, or showing that the visitor is offline?
|
|
109
|
+
load softr-sdk/offline
|
|
110
|
+
|
|
107
111
|
Showing a date, a time or a number on a page?
|
|
108
112
|
load softr-sdk/formatting
|
|
109
113
|
|
|
@@ -13,7 +13,7 @@ description: >-
|
|
|
13
13
|
metadata:
|
|
14
14
|
type: sub-skill
|
|
15
15
|
library: softr-sdk
|
|
16
|
-
library_version: 1.
|
|
16
|
+
library_version: 1.16.0
|
|
17
17
|
---
|
|
18
18
|
|
|
19
19
|
**Boundary.** `ai` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineAiAction` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.16.0
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.16.0
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A helper that calls `ds` outside a handler goes in `createServerOnlyFn` from `@tanstack/react-start` (the `record-queries` boundary). `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -12,7 +12,7 @@ description: >-
|
|
|
12
12
|
metadata:
|
|
13
13
|
type: sub-skill
|
|
14
14
|
library: softr-sdk
|
|
15
|
-
library_version: 1.
|
|
15
|
+
library_version: 1.16.0
|
|
16
16
|
---
|
|
17
17
|
|
|
18
18
|
**Boundary.** `email` and `EmailError` live on `@softr/sdk/server` and run only inside a `createServerFn` handler — importing them into a component fails the build. `defineEmailAction` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 1.
|
|
10
|
+
library_version: 1.16.0
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
**Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 1.
|
|
10
|
+
library_version: 1.16.0
|
|
11
11
|
---
|
|
12
12
|
|
|
13
13
|
**Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -11,7 +11,7 @@ description: >-
|
|
|
11
11
|
metadata:
|
|
12
12
|
type: sub-skill
|
|
13
13
|
library: softr-sdk
|
|
14
|
-
library_version: 1.
|
|
14
|
+
library_version: 1.16.0
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
**Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP. The tools you hand it come from `defineTool`, also on `@softr/sdk`, and call the same server logic the pages do.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: offline
|
|
3
|
+
description: >-
|
|
4
|
+
Let the published app open without internet and show the pages and data the
|
|
5
|
+
visitor last loaded, read only: useOfflineMode, the OfflineMode type, the
|
|
6
|
+
offline banner and saves made offline. The service worker is already served,
|
|
7
|
+
never write one. Editing offline is not supported.
|
|
8
|
+
metadata:
|
|
9
|
+
type: sub-skill
|
|
10
|
+
library: softr-sdk
|
|
11
|
+
library_version: 1.16.0
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# Offline mode
|
|
15
|
+
|
|
16
|
+
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. Pages the visitor never opened are not there. Nothing can be saved offline: a save fails as it would on any network error, and the app tells the visitor.
|
|
17
|
+
|
|
18
|
+
> **CRITICAL**: Never write a service worker, a `public/sw.js`, or any `navigator.serviceWorker`, `caches` or IndexedDB 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.
|
|
19
|
+
>
|
|
20
|
+
> **CRITICAL**: Editing offline is not supported. Do not build a queue of changes to send later. When the builder asks for it, say plainly that the app shows the last loaded data offline, and saving needs a connection.
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { useOfflineMode } from '@softr/sdk'
|
|
26
|
+
import type { OfflineMode } from '@softr/sdk'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
type OfflineMode = {
|
|
31
|
+
online: boolean // false while the browser has no connection; always true during server rendering
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Core Patterns
|
|
36
|
+
|
|
37
|
+
### Turning it on
|
|
38
|
+
|
|
39
|
+
Call `useOfflineMode()` once, in `RootDocument` in `src/routes/__root.tsx`, so every page is kept. Render the banner from the same place. Use the banner component the template ships when it has one.
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
function OfflineBanner() {
|
|
43
|
+
const { online } = useOfflineMode()
|
|
44
|
+
if (online) return null
|
|
45
|
+
return <div role="status">You're offline. Showing the last saved data.</div>
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
### Saves made offline
|
|
52
|
+
|
|
53
|
+
A save made offline rejects like any failed request. Catch it as usual, and when `online` is `false`, say that the visitor is offline rather than showing a generic error:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
const { online } = useOfflineMode()
|
|
57
|
+
save(values).catch(() =>
|
|
58
|
+
toast.error(online ? 'Could not save your changes.' : "You're offline. Connect to save your changes."),
|
|
59
|
+
)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Signing out
|
|
63
|
+
|
|
64
|
+
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.
|
|
65
|
+
|
|
66
|
+
### Turning it off
|
|
67
|
+
|
|
68
|
+
Remove the `useOfflineMode()` call and the banner. Copies already kept on visitors' devices are no longer used.
|
|
69
|
+
|
|
70
|
+
## Common Mistakes
|
|
71
|
+
|
|
72
|
+
### CRITICAL Writing a service worker for offline
|
|
73
|
+
|
|
74
|
+
Wrong:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
navigator.serviceWorker.register('/sw.js')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Correct:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
const { online } = useOfflineMode()
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
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.
|
|
87
|
+
|
|
88
|
+
Source: packages/sdk/src/offline.ts
|
|
89
|
+
|
|
90
|
+
### HIGH Calling the hook only on some pages
|
|
91
|
+
|
|
92
|
+
Wrong:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
// src/routes/_app/orders.tsx
|
|
96
|
+
const { online } = useOfflineMode()
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Correct:
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
// RootDocument in src/routes/__root.tsx
|
|
103
|
+
<OfflineBanner />
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
A page is kept for offline only while the hook runs. Called from one page, every other page is missing offline.
|
|
107
|
+
|
|
108
|
+
Source: packages/sdk/src/offline.ts
|
|
109
|
+
|
|
110
|
+
## References
|
|
111
|
+
|
|
112
|
+
- [SKILL.md](../SKILL.md): the entrypoints and which one each export lives on
|
|
113
|
+
- [push-notifications/SKILL.md](../push-notifications/SKILL.md): push notifications, which share the same service worker
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.16.0
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -11,7 +11,7 @@ description: >-
|
|
|
11
11
|
metadata:
|
|
12
12
|
type: sub-skill
|
|
13
13
|
library: softr-sdk
|
|
14
|
-
library_version: 1.
|
|
14
|
+
library_version: 1.16.0
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A route `loader` also runs in the browser, so it calls a server function, never `ds`. A helper that calls `ds` outside a handler, in this file or in another one, goes in `createServerOnlyFn` from `@tanstack/react-start`, or inside the handler. An exported helper without it keeps the server-only import in the browser bundle, and every page stops loading until it moves. Do not make such a helper a server function when it takes an id: any visitor can call a server function with any id. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.16.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.16.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.
|