@timber-js/app 0.1.22 → 0.1.24
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/dist/_chunks/{ssr-data-B2yikEEB.js → ssr-data-DLnbYpj1.js} +2 -4
- package/dist/_chunks/{ssr-data-B2yikEEB.js.map → ssr-data-DLnbYpj1.js.map} +1 -1
- package/dist/_chunks/{use-cookie-D5aS4slY.js → use-cookie-dDbpCTx-.js} +2 -2
- package/dist/_chunks/{use-cookie-D5aS4slY.js.map → use-cookie-dDbpCTx-.js.map} +1 -1
- package/dist/client/error-boundary.js +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +104 -93
- package/dist/client/index.js.map +1 -1
- package/dist/client/navigation-context.d.ts +50 -0
- package/dist/client/navigation-context.d.ts.map +1 -0
- package/dist/client/router.d.ts.map +1 -1
- package/dist/client/use-params.d.ts +35 -25
- package/dist/client/use-params.d.ts.map +1 -1
- package/dist/client/use-pathname.d.ts +11 -4
- package/dist/client/use-pathname.d.ts.map +1 -1
- package/dist/cookies/index.js +2 -2
- package/package.json +1 -1
- package/src/client/browser-entry.ts +50 -10
- package/src/client/index.ts +4 -0
- package/src/client/navigation-context.ts +88 -0
- package/src/client/router.ts +33 -24
- package/src/client/use-params.ts +50 -54
- package/src/client/use-pathname.ts +31 -24
|
@@ -4,40 +4,47 @@
|
|
|
4
4
|
* Returns the pathname portion of the current URL (e.g. '/dashboard/settings').
|
|
5
5
|
* Updates when client-side navigation changes the URL.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* On the client, reads from NavigationContext which is updated atomically
|
|
8
|
+
* with the RSC tree render. This replaces the previous useSyncExternalStore
|
|
9
|
+
* approach which only subscribed to popstate events — meaning usePathname()
|
|
10
|
+
* did NOT re-render on forward navigation (pushState). The context approach
|
|
11
|
+
* fixes this: pathname updates in the same render pass as the new tree.
|
|
10
12
|
*
|
|
11
13
|
* During SSR, reads the request pathname from the SSR ALS context
|
|
12
14
|
* (populated by ssr-entry.ts) instead of window.location.
|
|
15
|
+
*
|
|
16
|
+
* Compatible with Next.js's `usePathname()` from `next/navigation`.
|
|
13
17
|
*/
|
|
14
18
|
|
|
15
|
-
import { useSyncExternalStore } from 'react';
|
|
16
19
|
import { getSsrData } from './ssr-data.js';
|
|
17
|
-
|
|
18
|
-
function getPathname(): string {
|
|
19
|
-
if (typeof window !== 'undefined') return window.location.pathname;
|
|
20
|
-
return getSsrData()?.pathname ?? '/';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getServerPathname(): string {
|
|
24
|
-
return getSsrData()?.pathname ?? '/';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function subscribe(callback: () => void): () => void {
|
|
28
|
-
// Listen for popstate (back/forward) and timber's custom navigation events.
|
|
29
|
-
// pushState/replaceState don't fire popstate, but timber's router calls
|
|
30
|
-
// onPendingChange listeners after navigation — components re-render
|
|
31
|
-
// naturally via React's tree update from the new RSC payload.
|
|
32
|
-
window.addEventListener('popstate', callback);
|
|
33
|
-
return () => window.removeEventListener('popstate', callback);
|
|
34
|
-
}
|
|
20
|
+
import { useNavigationContext } from './navigation-context.js';
|
|
35
21
|
|
|
36
22
|
/**
|
|
37
23
|
* Read the current URL pathname.
|
|
38
24
|
*
|
|
39
|
-
*
|
|
25
|
+
* On the client, reads from NavigationContext (provided by
|
|
26
|
+
* NavigationProvider in renderRoot). During SSR, reads from the
|
|
27
|
+
* ALS-backed SSR data context. Falls back to window.location.pathname
|
|
28
|
+
* when called outside a React component (e.g., in tests).
|
|
40
29
|
*/
|
|
41
30
|
export function usePathname(): string {
|
|
42
|
-
|
|
31
|
+
// Try reading from NavigationContext (client-side, inside React tree).
|
|
32
|
+
// During SSR, no NavigationProvider is mounted, so this returns null.
|
|
33
|
+
try {
|
|
34
|
+
const navContext = useNavigationContext();
|
|
35
|
+
if (navContext !== null) {
|
|
36
|
+
return navContext.pathname;
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
// No React dispatcher available (called outside a component).
|
|
40
|
+
// Fall through to SSR/fallback below.
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// SSR path: read from ALS-backed SSR data context.
|
|
44
|
+
const ssrData = getSsrData();
|
|
45
|
+
if (ssrData) return ssrData.pathname ?? '/';
|
|
46
|
+
|
|
47
|
+
// Final fallback: window.location (tests, edge cases).
|
|
48
|
+
if (typeof window !== 'undefined') return window.location.pathname;
|
|
49
|
+
return '/';
|
|
43
50
|
}
|