@sidebandai/sideband-react 1.4.6-rc.1 → 1.4.6-rc.2

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.
Files changed (3) hide show
  1. package/README.md +178 -13
  2. package/dist/index.js +1 -1
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -21,9 +21,8 @@ npm install @sidebandai/sideband-react
21
21
 
22
22
  ## Configure
23
23
 
24
- Import the stylesheet once and wrap your application content in
25
- `SidebandProvider`. Pass your API key and application version through its
26
- `config` prop:
24
+ Import the stylesheet once and render `SidebandProvider` once, near the root of
25
+ your app. Pass your API key and application version through its `config` prop:
27
26
 
28
27
  ```tsx
29
28
  import { SidebandProvider } from '@sidebandai/sideband-react'
@@ -42,8 +41,65 @@ function App() {
42
41
  your application's version, not the SDK version. `appVersion` must be a
43
42
  non-empty string of at most 64 characters.
44
43
 
45
- The provider presents available Pulses automatically. In server-rendered apps,
46
- place it within a client component.
44
+ The provider presents available Pulses automatically. It renders them in a
45
+ portal on `document.body`, not inside its children, so it can wrap your app as
46
+ above or sit beside it with no children:
47
+
48
+ ```tsx
49
+ function App() {
50
+ return (
51
+ <>
52
+ <SidebandProvider config={{ apiKey: 'YOUR_API_KEY', appVersion: '1.0.0' }} />
53
+ <AppContent />
54
+ </>
55
+ )
56
+ }
57
+ ```
58
+
59
+ Components that call `useSideband()` must be inside the provider. When the
60
+ provider sits beside your app, connect your app to it as shown in
61
+ [Calls from outside the provider](#calls-from-outside-the-provider).
62
+
63
+ The provider reads `config` once, when it mounts, and keeps it until it
64
+ unmounts. A later change to the `config` prop, such as a new API key or
65
+ `context.language`, does not reach the SDK. Some callbacks are exceptions:
66
+
67
+ - `shouldPresentPulse` and `onRateApp`: the provider calls the latest ones.
68
+ - `onError` is split. Errors from showing a pulse go to the latest `onError`:
69
+ for example a pulse that is not valid, or a `shouldPresentPulse` or
70
+ `onRateApp` that throws. Errors from sending and from the SDK's own checks go
71
+ to the `onError` the provider mounted with: for example a failed or rejected
72
+ send, an answer that cannot be stored, or a `tagUser` ID that is too long.
73
+
74
+ To send every error to one handler, pass an `onError` that does not change
75
+ during the provider's life, or one that calls your current handler through a
76
+ ref. To apply a new config, remount the provider, for example with a React
77
+ `key`:
78
+
79
+ ```tsx
80
+ <SidebandProvider key={language} config={{ apiKey: 'YOUR_API_KEY', appVersion: '1.0.0', context: { language } }} />
81
+ ```
82
+
83
+ A remount stops the SDK, discards events not yet sent, and starts it again,
84
+ which sends a new `system_app_launch`. If the provider wraps your app, a new
85
+ `key` also remounts your app; render the provider beside your app to avoid that.
86
+
87
+ The API key goes to the browser, so anyone can read it in your page or its
88
+ network requests, as with a key in a mobile app. That is expected: the key can
89
+ only send events and pulse answers for its channel and receive that channel's
90
+ pulses.
91
+
92
+ ### Server rendering
93
+
94
+ `SidebandProvider` can render on the server. It starts the SDK in an effect, so
95
+ the server render creates no SDK, sends nothing, and renders no pulse. In the
96
+ browser, the SDK starts after hydration.
97
+
98
+ - In frameworks with React Server Components, such as the Next.js App Router,
99
+ render the provider from a Client Component (a file that starts with
100
+ `'use client'`). The package does not mark its modules as client code.
101
+ - In other server-rendering frameworks, such as React Router or Remix, render it
102
+ like any other component.
47
103
 
48
104
  ## Content Security Policy
49
105
 
@@ -67,20 +123,123 @@ that injects CSS as a `<style>` element, as some do in development, changes
67
123
  this: that element then needs whatever your policy requires for inline styles.
68
124
 
69
125
  A blocked send appears in the browser console as a Content Security Policy
70
- error, and `onError` receives a network error. The React package was not run
71
- under these policies; this section comes from its code.
126
+ error, and `onError` receives a network error.
127
+
128
+ What was checked with the React package, in a production build of a
129
+ server-rendered React 18.3 app: its policy had a nonce in `script-src`,
130
+ `style-src 'self' 'unsafe-inline'`, and `img-src` with `data:` and `https:`.
131
+ Adding the API origin to `connect-src` was the only change it needed, and the
132
+ browser reported no policy violation. The bundler extracted `style.css` into a
133
+ stylesheet file, which the app loaded like its other stylesheets, with the
134
+ page's nonce. A `style-src` without `'unsafe-inline'` has not been checked with
135
+ the React package.
72
136
 
73
137
  ## Consent
74
138
 
75
139
  Your app decides when Sideband runs. If Sideband needs consent on your site,
76
140
  render `SidebandProvider` only after the user gives it, and remove it when the
77
- user withdraws it; unmounting the provider stops the SDK. Which consent category
78
- applies is your decision; this README does not give legal advice.
141
+ user withdraws it. Mounting the provider starts the SDK; unmounting it stops the
142
+ SDK and discards events not yet sent. The SDK has no consent setting of its own,
143
+ and there is no `config` setting that pauses it. Which consent category applies
144
+ is your decision; this README does not give legal advice.
79
145
 
80
- A user who declines sends no events and sees no pulses. `useSideband()` throws
81
- outside `SidebandProvider`, so components that track events while the provider
82
- may be absent should call your own wrapper, which does nothing without consent.
83
- The SDK has no consent setting of its own.
146
+ Gate the provider, not your app. If the provider wraps your app, adding or
147
+ removing it remounts your whole app, which loses its state. Render it beside
148
+ your app instead:
149
+
150
+ ```tsx
151
+ function SidebandWhenAllowed({ allowed }: { allowed: boolean }) {
152
+ if (!allowed) return null
153
+ return (
154
+ <SidebandProvider config={{ apiKey: 'YOUR_API_KEY', appVersion: '1.0.0' }}>
155
+ <SidebandBridge />
156
+ </SidebandProvider>
157
+ )
158
+ }
159
+
160
+ function App() {
161
+ const allowed = useAnalyticsConsent() // your app's consent state
162
+ return (
163
+ <>
164
+ <SidebandWhenAllowed allowed={allowed} />
165
+ <AppContent />
166
+ </>
167
+ )
168
+ }
169
+ ```
170
+
171
+ `SidebandBridge` connects your app's code to the provider; see the next section.
172
+ A user who declines sends no events and sees no pulses.
173
+
174
+ ## Calls from outside the provider
175
+
176
+ `useSideband()` works only inside `SidebandProvider`; outside it, it throws. If
177
+ your app tracks events through its own analytics module, or from code that is
178
+ not a React component, give that module the SDK functions from a small
179
+ component inside the provider:
180
+
181
+ ```tsx
182
+ // analytics.ts: your app's analytics module
183
+ type SidebandApi = {
184
+ track: (name: string, metadata?: Record<string, string>) => void
185
+ tagUser: (userId: string) => void
186
+ untagUser: () => void
187
+ }
188
+
189
+ let sideband: SidebandApi | null = null
190
+ let user: { id: string | null } | null = null // null until your app calls identify
191
+
192
+ export function attachSideband(api: SidebandApi | null) {
193
+ sideband = api
194
+ // Apply the current user when Sideband starts, for example after consent.
195
+ if (api && user) {
196
+ if (user.id !== null) api.tagUser(user.id)
197
+ else api.untagUser()
198
+ }
199
+ }
200
+
201
+ export function track(name: string, metadata?: Record<string, string>) {
202
+ sideband?.track(name, metadata) // dropped while Sideband is not running
203
+ }
204
+
205
+ export function identify(id: string | null) {
206
+ user = { id }
207
+ if (id !== null) sideband?.tagUser(id)
208
+ else sideband?.untagUser()
209
+ }
210
+ ```
211
+
212
+ ```tsx
213
+ // SidebandBridge.tsx
214
+ import { useEffect } from 'react'
215
+ import { useSideband } from '@sidebandai/sideband-react'
216
+ import { attachSideband } from './analytics'
217
+
218
+ export function SidebandBridge() {
219
+ const { track, tagUser, untagUser } = useSideband()
220
+ useEffect(() => {
221
+ attachSideband({ track, tagUser, untagUser })
222
+ return () => attachSideband(null)
223
+ }, [track, tagUser, untagUser])
224
+ return null
225
+ }
226
+ ```
227
+
228
+ Render `<SidebandBridge />` inside the provider, as in the consent example.
229
+
230
+ The SDK does not queue calls made outside the provider. Calls that your module
231
+ makes while the bridge is not attached are dropped: before consent, before the
232
+ bridge's effect runs after hydration, and after the provider unmounts. The
233
+ module above keeps the user, so a user who signed in before Sideband started is
234
+ tagged when it starts. If you need events from before Sideband starts, hold a
235
+ short, capped list in your module after your app decides to start Sideband (not
236
+ before consent), and send it when the bridge attaches. The
237
+ `@sidebandai/sideband-web` README shows this pattern under "Events before
238
+ Sideband starts". Inside the provider, calls made before the SDK has finished
239
+ starting are queued and sent once it starts.
240
+
241
+ Metadata values must be strings: convert numbers and booleans in your module
242
+ (for example with `String(value)`).
84
243
 
85
244
  ## Language
86
245
 
@@ -233,6 +392,12 @@ new page load sends `system_app_launch` about one second after the SDK starts,
233
392
  with the default `flushIntervalSeconds`. Tagging the user right after start
234
393
  sends it sooner.
235
394
 
395
+ Each send gets at most one pulse back. When events that trigger different pulses
396
+ go in the same batch, the server serves at most one of those pulses for that
397
+ batch; the others are not served unless their trigger events come again. So
398
+ pulses whose triggers fire together, for example two events that your app
399
+ tracks for one user action, compete.
400
+
236
401
  `SidebandProviderConfig` in the package's type definitions lists the optional
237
402
  settings, such as `shouldPresentPulse` to hold a Pulse back for a better
238
403
  moment and `onError` for background errors. `useSideband()` also returns
package/dist/index.js CHANGED
@@ -42,7 +42,7 @@ function k(e, t) {
42
42
  }
43
43
  }), a = c({
44
44
  ...e,
45
- libraryVersion: "1.4.6-rc.1",
45
+ libraryVersion: "1.4.6-rc.2",
46
46
  onSyncResponse: (e) => r.handleSyncResponse(e)
47
47
  });
48
48
  return n.current = a, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sidebandai/sideband-react",
3
- "version": "1.4.6-rc.1",
3
+ "version": "1.4.6-rc.2",
4
4
  "description": "React SDK for Sideband client sync surveys and prompts.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -39,7 +39,7 @@
39
39
  "react-dom": "^18.3.0 || ^19.0.0"
40
40
  },
41
41
  "dependencies": {
42
- "@sidebandai/sideband-core": "1.4.6-rc.1"
42
+ "@sidebandai/sideband-core": "1.4.6-rc.2"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "vite build --config ../../vite.react.config.ts && tsc -p tsconfig.lib.json && node ../../scripts/prepare-react-package.mjs",