@scalemule/nextjs 0.1.47 → 0.1.49

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.
@@ -0,0 +1,218 @@
1
+ # Persistent article and network audio
2
+
3
+ `@scalemule/nextjs/audio` exports a shared listening queue, a bottom player bar,
4
+ article controls and a separate network player host. The audio entry uses only
5
+ React at runtime. Existing `AudioPlayer` and `NarrationPlayer` APIs are unchanged.
6
+
7
+ ## Playback boundaries
8
+
9
+ | Navigation | Behavior |
10
+ | --- | --- |
11
+ | Client-side navigation under the same provider | The same audio element continues playing. |
12
+ | Leaving the narrated article | Highlighting is destroyed; audio continues. |
13
+ | Returning to that article | Highlighting resumes at the current clock if enabled. |
14
+ | Moving the reader tab to another participating domain | A previously opened network player window keeps playing. The destination bar reconnects. |
15
+ | Full navigation/reload without a network window | The old audio stops. An optional checkpoint restores the queue and position paused. |
16
+ | Closing the network window | The reader restores the latest reported queue and position paused, with a Play prompt. |
17
+ | Closing the reader tab | The network window can continue independently. |
18
+
19
+ There is no seamless same-tab, cross-origin audio transfer on the web. An iframe,
20
+ service worker, localStorage or BroadcastChannel cannot preserve a destroyed
21
+ document's audio element. The separate window is opt-in and must be opened from
22
+ a user click. Its initial play may require another click under browser autoplay
23
+ policy. Once playing, navigation of its original reader tab does not replace its
24
+ audio element. Browser/OS suspension can still interrupt media, especially on
25
+ mobile. Test supported browsers before promising uninterrupted listening.
26
+
27
+ This implementation reconnects the **original reader tab** across navigation.
28
+ It does not discover unrelated tabs or synchronize devices. COOP headers that
29
+ sever `window.opener`, sandbox restrictions and popup blockers can prevent the
30
+ connection. Failed initial connections preserve local playback and show a notice.
31
+ An unresponsive remote player never triggers automatic local playback, which
32
+ could produce two recordings playing together.
33
+
34
+ ## Mount once in the reader layout
35
+
36
+ Create a client wrapper beneath the persistent app layout. Do not key it by
37
+ pathname, put it in a `template.tsx`, or mount it inside an article page.
38
+
39
+ ```tsx
40
+ 'use client'
41
+
42
+ import Link from 'next/link'
43
+ import { NetworkAudioProvider, NetworkAudioPlayer } from '@scalemule/nextjs/audio'
44
+ import '@scalemule/nextjs/audio.css'
45
+ import { resolveReaderAuthorizedAudio } from './reader-audio'
46
+
47
+ export function ListeningLayout({ children }: { children: React.ReactNode }) {
48
+ return (
49
+ <NetworkAudioProvider
50
+ resolveAudio={resolveReaderAuthorizedAudio}
51
+ connection={{
52
+ networkId: 'bay-area-chronicle',
53
+ playerUrl: 'https://bayareachronicle.com/listen',
54
+ }}
55
+ >
56
+ {children}
57
+ <NetworkAudioPlayer
58
+ networkName="Bay Area Chronicle"
59
+ renderArticleLink={track => <Link href={track.articleUrl}>{track.title}</Link>}
60
+ />
61
+ </NetworkAudioProvider>
62
+ )
63
+ }
64
+ ```
65
+
66
+ `reader-audio` is your application's integration, described below; the SDK does
67
+ not provide that module. Define/import the resolver in the client wrapper:
68
+ ordinary functions cannot cross from server components to client components.
69
+ The existing server layout can render this wrapper around `children`.
70
+
71
+ Use the router's `Link` for navigation within the site. The player's default
72
+ article link opens another tab to avoid unloading local audio. The dedicated
73
+ network host always opens article links separately.
74
+
75
+ ## Article controls and highlighting
76
+
77
+ Replace the page-owned player with shared queue controls. Do not render both
78
+ players for the same article.
79
+
80
+ ```tsx
81
+ import { ArticleAudioControls } from '@scalemule/nextjs/audio'
82
+
83
+ <ArticleAudioControls
84
+ track={{
85
+ id: article.id,
86
+ publicationId,
87
+ title: article.title,
88
+ publicationName,
89
+ articleUrl: `${canonical}/news/${encodeURIComponent(article.slug)}`,
90
+ }}
91
+ narration={article.audio?.has_word_timings ? {
92
+ timingsUrl: `/api/narration/${encodeURIComponent(article.slug)}`,
93
+ targetId: 'article-narration-body',
94
+ } : undefined}
95
+ />
96
+ ```
97
+
98
+ Keep the existing article body with `id="article-narration-body"`. Mounting an
99
+ article never selects or starts its audio. Listen selects it; Add to queue
100
+ appends without interrupting playback. Identity is `(publicationId, id)` so IDs
101
+ in different publications do not collide. The queue deduplicates entries and
102
+ holds up to 100 articles. Finishing an article advances to the next one.
103
+ Unavailable audio stops with a retry/choose-another message.
104
+
105
+ `useArticleNarration(track, narration)` attaches the same behavior to custom
106
+ controls. Highlighting is opt-in, applies only to the active article, clears
107
+ while the reader document is hidden, and detaches on unmount. The preference
108
+ remains in the provider during page navigation; it is not silently stored across
109
+ sessions. Missing timings or mismatched article text disable painting without
110
+ interrupting audio.
111
+
112
+ ## Resolve audio in the correct publication
113
+
114
+ `resolveAudio(track, signal)` returns an `AudioPlayerSource`. The provider calls
115
+ it when playing, when a signed URL is near expiry, or once during media recovery.
116
+ Honor the signal and return a fresh reader-authorized source:
117
+
118
+ ```ts
119
+ type ResolveNetworkAudio = (
120
+ track: NetworkAudioTrack,
121
+ signal: AbortSignal,
122
+ ) => Promise<{
123
+ url: string | null
124
+ duration_ms?: number | null
125
+ expires_at?: string | null
126
+ }>
127
+ ```
128
+
129
+ Connect this callback to the application's existing public article-read API.
130
+ Include publication identity and verify it server-side against the application's
131
+ permitted publications. A browser-supplied publication ID is not authorization.
132
+ All database/API access must retain the existing application/tenant boundary.
133
+
134
+ For different customer applications, resolve through each publication's reader
135
+ API using that application's server-side `SCALEMULE_API_KEY`. Allow only configured
136
+ publication origins and construct a known reader route; never proxy arbitrary
137
+ browser-provided URLs. Cross-origin public reader routes must allow the network
138
+ host's exact origin via CORS. Protected articles need an authorized integration;
139
+ do not depend on third-party cookies. Return only authorized media, never internal
140
+ platform tokens. The SDK does not invent an API endpoint or generate narration.
141
+
142
+ Signed URLs are resolved again as needed and never enter queue checkpoints or
143
+ cross-window messages. Those contain only publication/article IDs, public
144
+ titles/links and playback state. Requests time out after 15 seconds; stale
145
+ responses cannot replace a newer article. Media recovery is bounded to one retry
146
+ per play attempt. Autoplay denial presents a Play prompt.
147
+
148
+ ## Host `/listen` on the network site
149
+
150
+ This route is an application integration requirement: the SDK does not create or
151
+ deploy routes. Mount a **host provider instead of the reader provider** here.
152
+ Do not nest a host inside an already mounted reader provider. Use separate layout
153
+ groups, or switch the root provider's mode for the dedicated player route.
154
+
155
+ ```tsx
156
+ <NetworkAudioProvider
157
+ resolveAudio={resolveReaderAuthorizedNetworkAudio}
158
+ host={{
159
+ networkId: 'bay-area-chronicle',
160
+ allowedOrigins: [
161
+ 'https://bayareachronicle.com',
162
+ 'https://walnutcreektimes.com',
163
+ 'https://lamorindapost.com',
164
+ ],
165
+ }}
166
+ >
167
+ <h1>Your listening queue</h1>
168
+ <p>Keep this window open while you browse the network.</p>
169
+ <NetworkAudioPlayer fixed={false} networkName="Bay Area Chronicle" />
170
+ </NetworkAudioProvider>
171
+ ```
172
+
173
+ Add every participating reader origin explicitly, including `www` only if it
174
+ actually serves reader pages. The host checks exact origin, opener identity,
175
+ network namespace, payload schema and article URL origin. The reader checks the
176
+ host origin and pins its window reference. No wildcard `postMessage` targets
177
+ are used. Every reader site needs the provider and connection configuration;
178
+ the host allowlist alone does not install a player on those sites.
179
+
180
+ ## Ads, customization and saved progress
181
+
182
+ Pass an actual creative or ad component through `advertisement`. The expanded
183
+ slot is labeled “Advertisement”; no advertiser or impression is invented. Ad
184
+ selection, reader consent and impression tracking stay in the host's existing
185
+ ad integration. This is a visual slot, not audio ad insertion.
186
+
187
+ The bar measures its height and reserves bottom space. It supports narrow
188
+ screens, keyboard controls, volume, speed, seek, skipping forward/backward,
189
+ queue selection/removal and clearing the queue. Media Session play/pause/seek is
190
+ registered where supported. Customize `--sm-player-paper`, `--sm-player-ink`,
191
+ `--sm-player-muted`, `--sm-player-line`, `--sm-player-accent`, `--sm-player-soft`,
192
+ and `--sm-player-z-index`. Typography inherits `--font-editorial` and
193
+ `--font-inter`. Supply an appropriate palette for dark themes.
194
+
195
+ Persistence is off by default. Where reader preferences permit it, set
196
+ `checkpointStorageKey` to an application/network **and user-scoped** key.
197
+ Session storage retains a bounded metadata-only queue and position in that tab.
198
+ It restores paused. Clear/change the key on account changes. This is not an
199
+ account playlist or a podcast RSS feed: it provides continuous article listening
200
+ within the browser session.
201
+
202
+ ## Verify before a network release
203
+
204
+ The automated suite covers article unmounts, highlighting cleanup/return, queue
205
+ advancement, publication identity, stale/aborted requests, signed URL refresh,
206
+ autoplay denial, bounded recovery, blocked popups, exact origin/source checks,
207
+ remote reconnect and host closure. Also test real media in supported browsers:
208
+ start in the reader, open the network window, navigate the original tab to another
209
+ allowed origin, control playback from the destination, and close the host.
210
+ Verify ad consent and publication authorization in the actual applications.
211
+ Release the SDK, integrate layouts/routes/resolvers in the participating sites,
212
+ then deploy through their existing release process. These SDK changes alone do
213
+ not change live publications.
214
+
215
+ Browser references: [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open),
216
+ [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage),
217
+ [BroadcastChannel/storage partitioning](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API),
218
+ [autoplay policy](https://developer.mozilla.org/en-US/docs/Web/Media/Guides/Autoplay).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/nextjs",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "description": "ScaleMule SDK for Next.js applications - authentication, storage, and user management",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -51,6 +51,7 @@
51
51
  "files": [
52
52
  "dist",
53
53
  "README.md",
54
+ "docs/network-audio.md",
54
55
  "LICENSE"
55
56
  ],
56
57
  "scripts": {