@screenly/edge-apps 1.3.0 → 1.4.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/README.md CHANGED
@@ -140,12 +140,67 @@ signalReady()
140
140
  - `addUTMParams(url, params?)` - Add UTM parameters to URL
141
141
  - `addUTMParamsIf(url, enabled, params?)` - Conditionally add UTM parameters
142
142
 
143
+ ### Edge App Cache
144
+
145
+ - `readEdgeAppCache(namespace, key)` - Read a `localStorage`-backed, namespaced last-known-good value
146
+ - `writeEdgeAppCache(namespace, key, value)` - Write a `localStorage`-backed, namespaced last-known-good value
147
+
143
148
  ### Error Reporting (Sentry)
144
149
 
145
150
  - `setupSentry(app, contexts?)` - Initialize Sentry using the `sentry_dsn` setting; sets the `edge_app` tag, hostname, and any additional contexts. No-ops if `sentry_dsn` is not configured.
146
151
  - `scrubSensitiveData(event)` - Sentry `beforeSend` hook that redacts values of settings keys matching `token`, `secret`, `password`, or `credential` with `[REDACTED]`. Drops the event if it cannot be safely serialized.
147
152
  - `reportError(error, context?)` - Capture an exception via Sentry with optional extra context.
148
153
 
154
+ ## Edge App Cache
155
+
156
+ When an Edge App fetches data from a backend, a failure shouldn't necessarily
157
+ break the display — falling back to the last-known-good value is often better
158
+ than showing an error.
159
+
160
+ ### `readEdgeAppCache(namespace, key)` / `writeEdgeAppCache(namespace, key, value)`
161
+
162
+ Read/write a `localStorage`-backed value under `namespace`, so different
163
+ apps/caches don't collide. There is no TTL: it's meant to store the
164
+ last-known-good value and be consulted only after a genuine fetch failure, not
165
+ as a general-purpose expiring cache — and `localStorage` itself isn't
166
+ guaranteed to survive a device reboot. Both fail silently (return `null` /
167
+ no-op) if storage is unavailable, disabled, or full — so it's safe to use
168
+ without extra error handling around it.
169
+
170
+ `writeEdgeAppCache()` accepts any JSON-serializable value (object, array,
171
+ string, number, etc.) — `WeatherData` below is just a stand-in name for
172
+ "whatever your fetch returns," not a type this library exports.
173
+
174
+ ```typescript
175
+ import {
176
+ readEdgeAppCache,
177
+ writeEdgeAppCache,
178
+ getSettingWithDefault,
179
+ } from '@screenly/edge-apps'
180
+
181
+ async function loadWeather() {
182
+ const displayErrors = getSettingWithDefault('display_errors', false)
183
+
184
+ try {
185
+ const response = await fetch(weatherApiUrl)
186
+ if (!response.ok) throw new Error(`Weather API returned ${response.status}`)
187
+
188
+ // weatherData can be any JSON-serializable shape, e.g.:
189
+ // { temperature: 18, description: 'Cloudy', unit: 'metric' }
190
+ const weatherData: WeatherData = await response.json()
191
+ writeEdgeAppCache('my-edge-app', 'weather', weatherData)
192
+ return weatherData
193
+ } catch (error) {
194
+ // When `display_errors` is on, the raw error always wins, by design, so
195
+ // operators can diagnose real problems instead of seeing stale data.
196
+ if (!displayErrors) {
197
+ return readEdgeAppCache<WeatherData>('my-edge-app', 'weather')
198
+ }
199
+ throw error
200
+ }
201
+ }
202
+ ```
203
+
149
204
  ## Web Components
150
205
 
151
206
  This library includes reusable web components for building consistent Edge Apps. See the [components documentation](https://github.com/Screenly/edge-apps-library/blob/main/docs/components.md) for usage details.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Reads and JSON-parses the value stored under `${namespace}:${key}` in
3
+ * `localStorage`, or `null` if missing, unreadable, or storage is
4
+ * unavailable. Meant for last-known-good fallback data for Edge Apps:
5
+ * consult it only after a genuine fetch failure, not as a general-purpose
6
+ * expiring cache — there is no TTL, and `localStorage` itself isn't
7
+ * guaranteed to survive a device reboot.
8
+ */
9
+ export declare function readEdgeAppCache<T>(namespace: string, key: string): T | null;
10
+ /**
11
+ * JSON-serializes and stores `value` under `${namespace}:${key}` in
12
+ * `localStorage`. Fails silently if storage is unavailable, disabled, or
13
+ * full.
14
+ */
15
+ export declare function writeEdgeAppCache(namespace: string, key: string, value: unknown): void;
16
+ //# sourceMappingURL=edge-app-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edge-app-cache.d.ts","sourceRoot":"","sources":["../../src/utils/edge-app-cache.ts"],"names":[],"mappings":"AASA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,CAU5E;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,OAAO,GACb,IAAI,CAWN"}
@@ -0,0 +1,49 @@
1
+ function getStorage() {
2
+ try {
3
+ if (typeof localStorage === 'undefined')
4
+ return null;
5
+ return localStorage;
6
+ }
7
+ catch {
8
+ return null;
9
+ }
10
+ }
11
+ /**
12
+ * Reads and JSON-parses the value stored under `${namespace}:${key}` in
13
+ * `localStorage`, or `null` if missing, unreadable, or storage is
14
+ * unavailable. Meant for last-known-good fallback data for Edge Apps:
15
+ * consult it only after a genuine fetch failure, not as a general-purpose
16
+ * expiring cache — there is no TTL, and `localStorage` itself isn't
17
+ * guaranteed to survive a device reboot.
18
+ */
19
+ export function readEdgeAppCache(namespace, key) {
20
+ const storage = getStorage();
21
+ if (!storage)
22
+ return null;
23
+ try {
24
+ const raw = storage.getItem(`${namespace}:${key}`);
25
+ return raw ? JSON.parse(raw) : null;
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
31
+ /**
32
+ * JSON-serializes and stores `value` under `${namespace}:${key}` in
33
+ * `localStorage`. Fails silently if storage is unavailable, disabled, or
34
+ * full.
35
+ */
36
+ export function writeEdgeAppCache(namespace, key, value) {
37
+ const storage = getStorage();
38
+ if (!storage)
39
+ return;
40
+ try {
41
+ const serialized = JSON.stringify(value);
42
+ if (serialized === undefined)
43
+ return;
44
+ storage.setItem(`${namespace}:${key}`, serialized);
45
+ }
46
+ catch {
47
+ // Storage disabled or quota exceeded; caller simply has nothing cached.
48
+ }
49
+ }
@@ -1,5 +1,6 @@
1
1
  export * from './calendar.js';
2
2
  export * from './error-handling.js';
3
+ export * from './edge-app-cache.js';
3
4
  export * from './html.js';
4
5
  export * from './theme.js';
5
6
  export * from './locale.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,qBAAqB,CAAA;AACnC,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA"}
@@ -1,5 +1,6 @@
1
1
  export * from './calendar.js';
2
2
  export * from './error-handling.js';
3
+ export * from './edge-app-cache.js';
3
4
  export * from './html.js';
4
5
  export * from './theme.js';
5
6
  export * from './locale.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@screenly/edge-apps",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A TypeScript library for interfacing with Screenly Edge Apps API",
5
5
  "type": "module",
6
6
  "sideEffects": [