@timber-js/app 0.2.0-alpha.67 → 0.2.0-alpha.69

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 (51) hide show
  1. package/LICENSE +8 -0
  2. package/dist/client/history.d.ts +19 -4
  3. package/dist/client/history.d.ts.map +1 -1
  4. package/dist/client/index.js +321 -167
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/client/link-pending-store.d.ts +3 -3
  7. package/dist/client/link.d.ts.map +1 -1
  8. package/dist/client/nav-link-store.d.ts +36 -0
  9. package/dist/client/nav-link-store.d.ts.map +1 -0
  10. package/dist/client/navigation-api-types.d.ts +90 -0
  11. package/dist/client/navigation-api-types.d.ts.map +1 -0
  12. package/dist/client/navigation-api.d.ts +115 -0
  13. package/dist/client/navigation-api.d.ts.map +1 -0
  14. package/dist/client/navigation-context.d.ts +11 -0
  15. package/dist/client/navigation-context.d.ts.map +1 -1
  16. package/dist/client/{transition-root.d.ts → navigation-root.d.ts} +31 -9
  17. package/dist/client/navigation-root.d.ts.map +1 -0
  18. package/dist/client/nuqs-adapter.d.ts.map +1 -1
  19. package/dist/client/router.d.ts +46 -2
  20. package/dist/client/router.d.ts.map +1 -1
  21. package/dist/client/rsc-fetch.d.ts +1 -1
  22. package/dist/client/rsc-fetch.d.ts.map +1 -1
  23. package/dist/client/top-loader.d.ts +2 -2
  24. package/dist/client/top-loader.d.ts.map +1 -1
  25. package/dist/server/index.js.map +1 -1
  26. package/dist/server/route-element-builder.d.ts +10 -0
  27. package/dist/server/route-element-builder.d.ts.map +1 -1
  28. package/dist/server/slot-resolver.d.ts.map +1 -1
  29. package/dist/server/ssr-wrappers.d.ts +3 -3
  30. package/package.json +6 -7
  31. package/src/cli.ts +0 -0
  32. package/src/client/browser-entry.ts +92 -19
  33. package/src/client/history.ts +26 -4
  34. package/src/client/link-pending-store.ts +3 -3
  35. package/src/client/link.tsx +31 -9
  36. package/src/client/nav-link-store.ts +47 -0
  37. package/src/client/navigation-api-types.ts +112 -0
  38. package/src/client/navigation-api.ts +315 -0
  39. package/src/client/navigation-context.ts +22 -2
  40. package/src/client/navigation-root.tsx +346 -0
  41. package/src/client/nuqs-adapter.tsx +16 -3
  42. package/src/client/router.ts +186 -18
  43. package/src/client/rsc-fetch.ts +4 -3
  44. package/src/client/top-loader.tsx +12 -4
  45. package/src/client/use-navigation-pending.ts +1 -1
  46. package/src/server/route-element-builder.ts +69 -21
  47. package/src/server/slot-resolver.ts +37 -35
  48. package/src/server/ssr-entry.ts +1 -1
  49. package/src/server/ssr-wrappers.tsx +10 -10
  50. package/dist/client/transition-root.d.ts.map +0 -1
  51. package/src/client/transition-root.tsx +0 -205
@@ -0,0 +1,315 @@
1
+ /**
2
+ * Navigation API integration — progressive enhancement for client navigation.
3
+ *
4
+ * When the Navigation API (`window.navigation`) is available, this module
5
+ * provides an intercept-based navigation model that replaces the separate
6
+ * popstate + click handler approach with a single navigate event listener.
7
+ *
8
+ * Key benefits:
9
+ * - Intercepts ALL navigations (link clicks, form submissions, back/forward)
10
+ * - Built-in AbortSignal per navigation (auto-aborts in-flight fetches)
11
+ * - Per-entry state via NavigationHistoryEntry.getState()
12
+ * - navigation.transition for progress tracking
13
+ *
14
+ * When unavailable, all functions are no-ops and the History API fallback
15
+ * in browser-entry.ts handles navigation.
16
+ *
17
+ * See design/19-client-navigation.md
18
+ */
19
+
20
+ import type { NavigationApi, NavigateEvent } from './navigation-api-types.js';
21
+ import { consumeNavLinkMetadata } from './nav-link-store.js';
22
+ import { isHardNavigating } from './navigation-root.js';
23
+
24
+ // ─── Feature Detection ───────────────────────────────────────────
25
+
26
+ /**
27
+ * Returns true if the Navigation API is available in the current environment.
28
+ * Feature-detected at runtime — no polyfill.
29
+ */
30
+ export function hasNavigationApi(): boolean {
31
+ return (
32
+ typeof window !== 'undefined' &&
33
+ 'navigation' in window &&
34
+ (window as unknown as { navigation: unknown }).navigation != null
35
+ );
36
+ }
37
+
38
+ /**
39
+ * Get the Navigation API instance. Returns null if unavailable.
40
+ * Uses type assertion — we never import Navigation API types unconditionally.
41
+ */
42
+ export function getNavigationApi(): NavigationApi | null {
43
+ if (!hasNavigationApi()) return null;
44
+ return (window as unknown as { navigation: NavigationApi }).navigation;
45
+ }
46
+
47
+ // ─── Navigation API Controller ───────────────────────────────────
48
+
49
+ /**
50
+ * Callbacks for the Navigation API event handler.
51
+ *
52
+ * When the Navigation API intercepts a navigation, it delegates to these
53
+ * callbacks which run the RSC fetch + render pipeline.
54
+ */
55
+ export interface NavigationApiCallbacks {
56
+ /**
57
+ * Handle a push/replace navigation intercepted by the Navigation API.
58
+ * This covers both Link <a> clicks (user-initiated, with metadata from
59
+ * nav-link-store) and external navigations (plain <a> tags, programmatic).
60
+ * The Navigation API handles the URL update via event.intercept().
61
+ */
62
+ onExternalNavigate: (
63
+ url: string,
64
+ options: { replace: boolean; signal: AbortSignal; scroll?: boolean }
65
+ ) => Promise<void>;
66
+
67
+ /**
68
+ * Handle a traversal (back/forward button). The Navigation API intercepts
69
+ * the traversal and delegates to us for RSC replay/fetch.
70
+ */
71
+ onTraverse: (url: string, scrollY: number, signal: AbortSignal) => Promise<void>;
72
+ }
73
+
74
+ /**
75
+ * Controller returned by setupNavigationApi. Provides methods to
76
+ * coordinate between the router and the navigate event listener.
77
+ */
78
+ export interface NavigationApiController {
79
+ /**
80
+ * Set the router-navigating flag. When `true`, the next navigate event
81
+ * (from pushState/replaceState) is recognized as router-initiated. The
82
+ * handler still intercepts it — but ties the browser's native loading
83
+ * state to a deferred promise instead of running the RSC pipeline again.
84
+ *
85
+ * This means `navigation.transition` is active for the full duration of
86
+ * every router-initiated navigation, giving the browser a native loading
87
+ * indicator (tab spinner, address bar) aligned with the TopLoader.
88
+ *
89
+ * Must be called synchronously around pushState/replaceState:
90
+ * controller.setRouterNavigating(true);
91
+ * history.pushState(...); // navigate event fires, intercepted
92
+ * controller.setRouterNavigating(false); // flag off, deferred stays open
93
+ */
94
+ setRouterNavigating: (value: boolean) => void;
95
+
96
+ /**
97
+ * Resolve the deferred promise created by setRouterNavigating(true),
98
+ * clearing the browser's native loading state. Call this when the
99
+ * navigation fully completes — aligned with when the TopLoader's
100
+ * pendingUrl clears (same finally block in router.navigate).
101
+ */
102
+ completeRouterNavigation: () => void;
103
+
104
+ /**
105
+ * Initiate a navigation via the Navigation API (`navigation.navigate()`).
106
+ * Unlike `history.pushState()`, this fires the navigate event BEFORE
107
+ * committing the URL — allowing Chrome to show its native loading
108
+ * indicator while the intercept handler runs.
109
+ *
110
+ * Must be called with setRouterNavigating(true) active so the handler
111
+ * recognizes it as router-initiated and uses the deferred promise.
112
+ */
113
+ navigate: (url: string, replace: boolean) => void;
114
+
115
+ /**
116
+ * Save scroll position into the current navigation entry's state.
117
+ * Uses navigation.updateCurrentEntry() for per-entry scroll storage.
118
+ */
119
+ saveScrollPosition: (scrollY: number) => void;
120
+
121
+ /**
122
+ * Check if the Navigation API has an active transition.
123
+ * Returns the transition object if available, null otherwise.
124
+ */
125
+ hasActiveTransition: () => boolean;
126
+
127
+ /** Remove the navigate event listener. */
128
+ cleanup: () => void;
129
+ }
130
+
131
+ /**
132
+ * Set up the Navigation API navigate event listener.
133
+ *
134
+ * Intercepts same-origin navigations and delegates to the provided callbacks.
135
+ * Router-initiated navigations (pushState from router.navigate) are detected
136
+ * via a synchronous flag and NOT intercepted — the router already handles them.
137
+ *
138
+ * Returns a controller for coordinating with the router.
139
+ */
140
+ export function setupNavigationApi(callbacks: NavigationApiCallbacks): NavigationApiController {
141
+ const nav = getNavigationApi()!;
142
+
143
+ let routerNavigating = false;
144
+
145
+ // Deferred promise for router-initiated navigations. Created when
146
+ // setRouterNavigating(true) is called, resolved by completeRouterNavigation().
147
+ // The navigate event handler intercepts with this promise so the browser's
148
+ // native loading state (tab spinner) stays active until the navigation
149
+ // completes — aligned with TopLoader's pendingUrl lifecycle.
150
+ let routerNavDeferred: { promise: Promise<void>; resolve: () => void } | null = null;
151
+
152
+ function handleNavigate(event: NavigateEvent): void {
153
+ // Skip non-interceptable navigations (cross-origin, etc.)
154
+ if (!event.canIntercept) return;
155
+
156
+ // Hard navigation guard: when the router has triggered a full page
157
+ // load (500 error, version skew), skip interception entirely so the
158
+ // browser performs the MPA navigation. Without this guard, setting
159
+ // window.location.href fires a navigate event that we'd intercept,
160
+ // running the RSC pipeline again → 500 → window.location.href →
161
+ // navigate event → infinite loop.
162
+ // See design/19-client-navigation.md §"Hard Navigation Guard"
163
+ if (isHardNavigating()) return;
164
+
165
+ // Skip download requests
166
+ if (event.downloadRequest) return;
167
+
168
+ // Skip hash-only changes — let the browser handle scroll-to-anchor
169
+ if (event.hashChange) return;
170
+
171
+ // Shallow URL updates (e.g., nuqs search param changes). The navigation
172
+ // only changes the URL — no server round trip needed. Intercept with a
173
+ // no-op handler so the Navigation API commits the URL change without
174
+ // triggering a full page navigation (which is the default if we don't
175
+ // intercept). The info property is the Navigation API's built-in
176
+ // per-navigation metadata — no side-channel flags needed.
177
+ const info = event.info as { shallow?: boolean } | null | undefined;
178
+ if (info?.shallow) {
179
+ event.intercept({
180
+ handler: () => Promise.resolve(),
181
+ focusReset: 'manual',
182
+ scroll: 'manual',
183
+ });
184
+ return;
185
+ }
186
+
187
+ // Skip form submissions with a body (POST/PUT/etc.). These need the
188
+ // browser's native form handling to send the request body to the server.
189
+ // Intercepting would convert them into GET RSC navigations, dropping
190
+ // the form data. Server actions use fetch() directly (not form navigation),
191
+ // so they are unaffected by this check.
192
+ if (event.formData) return;
193
+
194
+ // Skip cross-origin (defense-in-depth — canIntercept covers this)
195
+ const destUrl = new URL(event.destination.url);
196
+ if (destUrl.origin !== location.origin) return;
197
+
198
+ // Router-initiated navigation (Link click → router.navigate → pushState).
199
+ // The router is already running the RSC pipeline — don't run it again.
200
+ // Instead, intercept with the deferred promise so the browser's native
201
+ // loading state tracks the navigation's full lifecycle. This aligns the
202
+ // tab spinner / address bar indicator with the TopLoader.
203
+ if (routerNavigating && routerNavDeferred) {
204
+ event.intercept({
205
+ scroll: 'manual',
206
+ focusReset: 'manual',
207
+ handler: () => routerNavDeferred!.promise,
208
+ });
209
+ return;
210
+ }
211
+
212
+ // Skip reload navigations — let the browser handle full page reload
213
+ if (event.navigationType === 'reload') return;
214
+
215
+ const url = destUrl.pathname + destUrl.search;
216
+
217
+ if (event.navigationType === 'traverse') {
218
+ // Back/forward button — intercept and delegate to router.
219
+ // Read scroll position from the destination entry's state.
220
+ const entryState = event.destination.getState() as
221
+ | { scrollY?: number; timber?: boolean }
222
+ | null
223
+ | undefined;
224
+ const scrollY = entryState && typeof entryState.scrollY === 'number' ? entryState.scrollY : 0;
225
+
226
+ event.intercept({
227
+ // Manual scroll — we handle scroll restoration ourselves
228
+ // via afterPaint (same as the History API path).
229
+ scroll: 'manual',
230
+ focusReset: 'manual',
231
+ async handler() {
232
+ await callbacks.onTraverse(url, scrollY, event.signal);
233
+ },
234
+ });
235
+ } else if (event.navigationType === 'push' || event.navigationType === 'replace') {
236
+ // Push/replace — either a Link <a> click (with metadata in
237
+ // nav-link-store) or an external navigation (plain <a>, programmatic).
238
+ // Consume link metadata if present — tells us scroll preference
239
+ // and which Link component to track pending state for.
240
+ const linkMeta = consumeNavLinkMetadata();
241
+
242
+ event.intercept({
243
+ scroll: 'manual',
244
+ focusReset: 'manual',
245
+ async handler() {
246
+ await callbacks.onExternalNavigate(url, {
247
+ replace: event.navigationType === 'replace',
248
+ signal: event.signal,
249
+ scroll: linkMeta?.scroll,
250
+ });
251
+ },
252
+ });
253
+ }
254
+ }
255
+
256
+ nav.addEventListener('navigate', handleNavigate as EventListener);
257
+
258
+ return {
259
+ setRouterNavigating(value: boolean): void {
260
+ routerNavigating = value;
261
+ if (value) {
262
+ // Create a new deferred promise. The navigate event handler will
263
+ // intercept and tie the browser's loading state to this promise.
264
+ let resolve!: () => void;
265
+ const promise = new Promise<void>((r) => {
266
+ resolve = r;
267
+ });
268
+ routerNavDeferred = { promise, resolve };
269
+ } else {
270
+ // Flag off — but DON'T resolve the deferred here. The navigation
271
+ // is still in flight (RSC fetch + render). completeRouterNavigation()
272
+ // resolves it when the navigation fully completes.
273
+ routerNavigating = false;
274
+ }
275
+ },
276
+
277
+ completeRouterNavigation(): void {
278
+ if (routerNavDeferred) {
279
+ routerNavDeferred.resolve();
280
+ routerNavDeferred = null;
281
+ }
282
+ },
283
+
284
+ navigate(url: string, replace: boolean): void {
285
+ // Use navigation.navigate() instead of history.pushState().
286
+ // This fires the navigate event BEFORE committing the URL,
287
+ // which lets Chrome show its native loading indicator while
288
+ // the intercept handler (deferred promise) is pending.
289
+ // history.pushState() commits the URL synchronously, so Chrome
290
+ // sees the navigation as already complete and skips the indicator.
291
+ nav.navigate(url, {
292
+ history: replace ? 'replace' : 'push',
293
+ });
294
+ },
295
+
296
+ saveScrollPosition(scrollY: number): void {
297
+ try {
298
+ const currentState = (nav.currentEntry?.getState() ?? {}) as Record<string, unknown>;
299
+ nav.updateCurrentEntry({
300
+ state: { ...currentState, timber: true, scrollY },
301
+ });
302
+ } catch {
303
+ // Ignore errors — updateCurrentEntry may throw if entry is disposed
304
+ }
305
+ },
306
+
307
+ hasActiveTransition(): boolean {
308
+ return nav.transition != null;
309
+ },
310
+
311
+ cleanup(): void {
312
+ nav.removeEventListener('navigate', handleNavigate as EventListener);
313
+ },
314
+ };
315
+ }
@@ -62,7 +62,7 @@ export interface NavigationState {
62
62
  * Context instances are stored on globalThis (NOT in module-level
63
63
  * variables) because the ESM bundler can duplicate this module across
64
64
  * chunks. Module-level variables would create separate instances per
65
- * chunk — the provider in TransitionRoot (index chunk) would use
65
+ * chunk — the provider in NavigationRoot (index chunk) would use
66
66
  * context A while the consumer in useNavigationPending (shared chunk)
67
67
  * reads from context B. globalThis guarantees a single instance.
68
68
  *
@@ -168,7 +168,7 @@ export function getNavigationState(): NavigationState {
168
168
 
169
169
  /**
170
170
  * Separate context for the in-flight navigation URL. Provided by
171
- * TransitionRoot (urgent useState), consumed by useNavigationPending
171
+ * NavigationRoot (urgent useState), consumed by useNavigationPending
172
172
  * and TopLoader. Per-link pending state uses useOptimistic instead
173
173
  * (see link-pending-store.ts).
174
174
  *
@@ -218,3 +218,23 @@ export function PendingNavigationProvider({
218
218
  }
219
219
  return createElement(ctx.Provider, { value }, children);
220
220
  }
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // Navigation API transition state (optional progressive enhancement)
224
+ // ---------------------------------------------------------------------------
225
+
226
+ /**
227
+ * Check if the browser's Navigation API has an active transition.
228
+ *
229
+ * When the Navigation API is available and a navigation has been intercepted
230
+ * via event.intercept(), `navigation.transition` is non-null until the
231
+ * handler resolves. This provides browser-native progress tracking that
232
+ * can be used alongside the existing pendingUrl mechanism.
233
+ *
234
+ * Returns false when Navigation API is unavailable or no transition is active.
235
+ */
236
+ export function hasNativeNavigationTransition(): boolean {
237
+ if (typeof window === 'undefined') return false;
238
+ const nav = (window as unknown as { navigation?: { transition?: unknown } }).navigation;
239
+ return nav?.transition != null;
240
+ }