@softr/sdk 1.7.7 → 1.8.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @softr/sdk
2
2
 
3
+ ## 1.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - `usePwaInstall` returns `install()` while the browser offers its own install dialog (Chrome and Edge), and turns unavailable once the app is installed.
8
+
3
9
  ## 1.7.7
4
10
 
5
11
  ### Patch Changes
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.7.7";
2
+ var version = "1.8.0";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/dist/pwa.d.ts CHANGED
@@ -26,6 +26,7 @@ import "@tanstack/react-start";
26
26
  */
27
27
  declare function SoftrPwaHead(): react5.FunctionComponentElement<react5.FragmentProps>;
28
28
  type PwaPlatform = "ios" | "android" | "desktop";
29
+ type InstallChoice = "accepted" | "dismissed";
29
30
  type PwaInstall = {
30
31
  available: false;
31
32
  } | {
@@ -36,6 +37,12 @@ type PwaInstall = {
36
37
  name: string;
37
38
  /** The icon the home screen will show. */
38
39
  iconUrl: string;
40
+ /**
41
+ * Opens the browser's own install dialog. Only there while the browser offers one: never in
42
+ * iOS, Safari, Firefox or the Studio preview, and not again once the visitor has closed it.
43
+ * Call it from a click, and show the steps for `platform` while it is missing or rejects.
44
+ */
45
+ install?: () => Promise<InstallChoice>;
39
46
  };
40
47
  /**
41
48
  * Whether to offer installing the app, and on which platform. Render an
@@ -47,7 +54,17 @@ type PwaInstall = {
47
54
  * function InstallButton() {
48
55
  * const install = usePwaInstall()
49
56
  * if (!install.available) return null
50
- * return <button onClick={() => openSteps(install.platform)}>Install {install.name}</button>
57
+ * return (
58
+ * <button
59
+ * onClick={() =>
60
+ * install.install
61
+ * ? install.install().catch(() => openSteps(install.platform))
62
+ * : openSteps(install.platform)
63
+ * }
64
+ * >
65
+ * Install {install.name}
66
+ * </button>
67
+ * )
51
68
  * }
52
69
  */
53
70
  declare function usePwaInstall(): PwaInstall;
package/dist/pwa.js CHANGED
@@ -4,7 +4,7 @@ import { fetch } from "./fetch.js";
4
4
  import { sessionConfig } from "./auth/config.js";
5
5
  import { readCached } from "./cache.js";
6
6
  import { z } from "zod";
7
- import { Fragment, createElement, useEffect, useState } from "react";
7
+ import { Fragment, createElement, useEffect, useMemo, useState, useSyncExternalStore } from "react";
8
8
  import { createMiddleware } from "@tanstack/react-start";
9
9
 
10
10
  //#region src/pwa.ts
@@ -139,6 +139,22 @@ function SoftrPwaHead() {
139
139
  href: image.href
140
140
  })));
141
141
  }
142
+ function isInstallPromptEvent(event) {
143
+ return "prompt" in event && typeof event.prompt === "function" && "userChoice" in event;
144
+ }
145
+ let offeredPrompt = null;
146
+ const offerListeners = /* @__PURE__ */ new Set();
147
+ function setOfferedPrompt(next) {
148
+ offeredPrompt = next;
149
+ for (const listener of offerListeners) listener();
150
+ }
151
+ if (typeof window !== "undefined") {
152
+ window.addEventListener("beforeinstallprompt", (event) => {
153
+ if (!isInstallPromptEvent(event)) return;
154
+ setOfferedPrompt(event);
155
+ });
156
+ window.addEventListener("appinstalled", () => setOfferedPrompt(null));
157
+ }
142
158
  const installSchema = pwaSchema.shape.install;
143
159
  /**
144
160
  * Whether to offer installing the app, and on which platform. Render an
@@ -150,21 +166,38 @@ const installSchema = pwaSchema.shape.install;
150
166
  * function InstallButton() {
151
167
  * const install = usePwaInstall()
152
168
  * if (!install.available) return null
153
- * return <button onClick={() => openSteps(install.platform)}>Install {install.name}</button>
169
+ * return (
170
+ * <button
171
+ * onClick={() =>
172
+ * install.install
173
+ * ? install.install().catch(() => openSteps(install.platform))
174
+ * : openSteps(install.platform)
175
+ * }
176
+ * >
177
+ * Install {install.name}
178
+ * </button>
179
+ * )
154
180
  * }
155
181
  */
156
182
  function usePwaInstall() {
157
183
  const [install, setInstall] = useState({ available: false });
184
+ const offered = useSyncExternalStore((listener) => {
185
+ offerListeners.add(listener);
186
+ return () => offerListeners.delete(listener);
187
+ }, () => offeredPrompt, () => null);
158
188
  useEffect(() => {
159
189
  if (isStandalone()) return;
160
190
  let cancelled = false;
161
191
  const standaloneQuery = window.matchMedia(STANDALONE_QUERY);
162
- const hideOnceStandalone = () => {
163
- if (!standaloneQuery.matches) return;
192
+ const hide = () => {
164
193
  cancelled = true;
165
194
  setInstall({ available: false });
166
195
  };
196
+ const hideOnceStandalone = () => {
197
+ if (standaloneQuery.matches) hide();
198
+ };
167
199
  standaloneQuery.addEventListener("change", hideOnceStandalone);
200
+ window.addEventListener("appinstalled", hide);
168
201
  fetchInstallInfo().then((installInfo) => {
169
202
  const parsed = installSchema.safeParse(installInfo);
170
203
  if (!parsed.success || cancelled) return;
@@ -177,9 +210,20 @@ function usePwaInstall() {
177
210
  return () => {
178
211
  cancelled = true;
179
212
  standaloneQuery.removeEventListener("change", hideOnceStandalone);
213
+ window.removeEventListener("appinstalled", hide);
180
214
  };
181
215
  }, []);
182
- return install;
216
+ return useMemo(() => {
217
+ if (!install.available || offered === null) return install;
218
+ return {
219
+ ...install,
220
+ install: async () => {
221
+ await offered.prompt();
222
+ setOfferedPrompt(null);
223
+ return (await offered.userChoice).outcome;
224
+ }
225
+ };
226
+ }, [install, offered]);
183
227
  }
184
228
  function detectPlatform() {
185
229
  const userAgent = navigator.userAgent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "1.7.7",
3
+ "version": "1.8.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: core
12
12
  library: softr-sdk
13
- library_version: 1.7.7
13
+ library_version: 1.8.0
14
14
  ---
15
15
 
16
16
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 1.7.7
16
+ library_version: 1.8.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.7.7
13
+ library_version: 1.8.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.7.7
13
+ library_version: 1.8.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.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.7.7
14
+ library_version: 1.8.0
15
15
  ---
16
16
 
17
17
  # The current user
@@ -12,7 +12,7 @@ description: >-
12
12
  metadata:
13
13
  type: sub-skill
14
14
  library: softr-sdk
15
- library_version: 1.7.7
15
+ library_version: 1.8.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.7.7
10
+ library_version: 1.8.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.
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 1.7.7
11
+ library_version: 1.8.0
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.7.7
10
+ library_version: 1.8.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.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.7
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **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.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.7
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -3,13 +3,13 @@ name: pwa
3
3
  description: >-
4
4
  Let visitors install the app on their home screen: SoftrPwaHead for the
5
5
  manifest, icon, iOS name and launch screen tags, usePwaInstall to decide
6
- whether to show an install button and which platform's steps to show, the
7
- PwaInstall and PwaPlatform types, and how to check that the app is
6
+ whether to show an install button, which platform's steps to show and, when
7
+ the browser offers it, install() for its own install dialog, the PwaInstall and PwaPlatform types, and how to check that the app is
8
8
  installable. The manifest and icon are already served; never write them.
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.7
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -37,6 +37,7 @@ type PwaInstall =
37
37
  platform: PwaPlatform // which steps install the app on this device
38
38
  name: string // what the home screen will show
39
39
  iconUrl: string // the icon the home screen will show
40
+ install?: () => Promise<'accepted' | 'dismissed'> // the browser's own install dialog, when offered
40
41
  }
41
42
  ```
42
43
 
@@ -71,7 +72,21 @@ function RootDocument({ children }: { children: React.ReactNode }) {
71
72
 
72
73
  It fetches after mount, so the first render, the server render included, always says `available: false`. It stays unavailable when the app is not installable, when the request fails, and when the app already runs installed from the home screen. Inside the Studio preview it says available, so the builder can see the button; installing itself only works on the published app.
73
74
 
74
- There is no `install()` to call. No browser opens its install dialog for this app on request, so the button opens steps for `platform` instead: Share, then Add to Home Screen on iOS; the browser menu's Install app on Android and desktop. `name` and `iconUrl` tell the visitor what they are adding.
75
+ `install` is there only while the browser offers its own install dialog: Chrome and Edge, on desktop and Android, and not in the Studio preview. Call it from a click. It rejects when the browser refuses the prompt, so open the steps then. It resolves with what the visitor chose, and the button disappears by itself once the app is installed. The browser shows its dialog only once per offer, so after the visitor closes it, `install` is gone until the browser offers again.
76
+
77
+ When `install` is missing, the button opens steps for `platform` instead: Share, then Add to Home Screen on iOS; the browser menu's Install app on Android and desktop. Safari and Firefox never offer the dialog. `name` and `iconUrl` tell the visitor what they are adding.
78
+
79
+ ```tsx
80
+ <button
81
+ onClick={() =>
82
+ install.install
83
+ ? install.install().catch(() => openSteps(install.platform))
84
+ : openSteps(install.platform)
85
+ }
86
+ >
87
+ Install {install.name}
88
+ </button>
89
+ ```
75
90
 
76
91
  ## References
77
92
 
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.7
13
+ library_version: 1.8.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.7.7
14
+ library_version: 1.8.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.7.7
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` 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.7.7
12
+ library_version: 1.8.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.7.7
12
+ library_version: 1.8.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.