@tschk/moonshine-waku 0.4.0 → 0.4.4

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
@@ -31,8 +31,9 @@ import { json } from "@tschk/moonshine-waku/server";
31
31
  - `Link` — `to` as string or `{ pathname, search }`, `scroll`,
32
32
  `unstable_instant`, `unstable_prefetchOnEnter`, `unstable_prefetchOnView`
33
33
  (IntersectionObserver at 0.1 threshold), `unstable_startTransition`.
34
- Modifier-clicks fall through to the browser; navigation runs inside
35
- `startTransition`.
34
+ Modifier-clicks, external URLs, fragments, non-self targets and downloads
35
+ fall through to the browser without route prefetch. Internal navigation runs
36
+ inside `startTransition`.
36
37
  - `useRouter_UNSTABLE` / `useRouter` — spreads the current `{ path, query, hash }`
37
38
  route plus `push`, `replace`, `reload`, `back`, `forward`, `prefetch`.
38
39
  - `useRoute` — Waku's route shape, with the trailing-slash `path` convention.
@@ -60,7 +61,13 @@ Waku's remaining surface is bound to its RSC pipeline and Vite plugin, and is
60
61
  `<link rel="prefetch">`; it does not fetch and cache an RSC payload, so
61
62
  `unstable_instant` navigation is a plain synchronous navigation.
62
63
  - **Base path**: `WAKU_CONFIG_BASE_PATH` is not applied to hrefs.
63
- - Search and hash are read from `window.location`; on the server they are empty
64
- because moonshine's router signal carries the pathname only.
64
+ - Search comes from the scoped pathname-plus-query signal. Hash follows browser
65
+ hash/history events and owning-router navigation; its SSR snapshot is empty.
65
66
 
66
67
  See [Moonshine README](../../README.md).
68
+
69
+ Route hashes come from the owning Moonshine runtime's fragment signal.
70
+ Unrelated renders and remounts retain that runtime's fragment after another
71
+ router navigates. Global browser hash/pop events still update mounted
72
+ consumers. SSR hash snapshots stay empty. Chromium checks cover independent
73
+ navigation, remounts and real back/forward behavior.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-waku",
3
- "version": "0.4.0",
3
+ "version": "0.4.4",
4
4
  "description": "Waku client router API reimplemented on moonshine — drop waku, keep the imports",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -15,8 +15,8 @@
15
15
  "./aliases": "./src/aliases.ts"
16
16
  },
17
17
  "dependencies": {
18
- "@tschk/moonshine": "^0.3.6",
19
- "@tschk/moonshine-router": "^0.3.6"
18
+ "@tschk/moonshine": "^0.4.4",
19
+ "@tschk/moonshine-router": "^0.4.4"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": ">=18",
@@ -36,5 +36,13 @@
36
36
  "lint": "bunx oxlint .",
37
37
  "typecheck": "bunx tsc --noEmit",
38
38
  "test": "bun test"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "react": {
42
+ "optional": true
43
+ },
44
+ "react-dom": {
45
+ "optional": true
46
+ }
39
47
  }
40
48
  }
package/src/link.ts CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  type MouseEvent,
19
19
  type ReactNode,
20
20
  } from "react";
21
- import { navigate } from "@tschk/moonshine/router";
21
+ import { isExternal, useNavigate } from "@tschk/moonshine/router";
22
22
  import {
23
23
  NavigationStatusContext,
24
24
  prefetchRoute,
@@ -63,16 +63,21 @@ export function Link({
63
63
  onMouseEnter,
64
64
  ...rest
65
65
  }: LinkProps): ReactNode {
66
+ const navigate = useNavigate();
66
67
  const href = toHref(to);
68
+ const browserOwned = isExternal(href, rest.target, rest.download);
67
69
  const ref = useRef<HTMLAnchorElement>(null);
68
70
  const [isPending, startTransition] = useTransition();
69
71
  const run = unstable_startTransition ?? startTransition;
70
72
 
71
- const warm = useCallback(() => prefetchRoute(href), [href]);
73
+ const warm = useCallback(() => {
74
+ if (!browserOwned) prefetchRoute(href);
75
+ }, [href, browserOwned]);
72
76
 
73
77
  useEffect(() => {
74
78
  const node = ref.current;
75
79
  if (
80
+ browserOwned ||
76
81
  !unstable_prefetchOnView ||
77
82
  !node ||
78
83
  typeof IntersectionObserver === "undefined"
@@ -92,7 +97,7 @@ export function Link({
92
97
  );
93
98
  observer.observe(node);
94
99
  return () => observer.disconnect();
95
- }, [unstable_prefetchOnView, warm]);
100
+ }, [unstable_prefetchOnView, warm, browserOwned]);
96
101
 
97
102
  const status = useMemo(() => ({ pending: isPending }), [isPending]);
98
103
 
@@ -108,7 +113,7 @@ export function Link({
108
113
  },
109
114
  onClick: (event: MouseEvent<HTMLAnchorElement>) => {
110
115
  onClick?.(event);
111
- if (event.defaultPrevented || isAltClick(event)) return;
116
+ if (browserOwned || event.defaultPrevented || isAltClick(event)) return;
112
117
  event.preventDefault();
113
118
  warm();
114
119
  const go = () => {
package/src/router.ts CHANGED
@@ -15,8 +15,13 @@ import {
15
15
  useContext,
16
16
  useMemo,
17
17
  useReducer,
18
+ useSyncExternalStore,
18
19
  } from "react";
19
- import { navigate, useLocation } from "@tschk/moonshine/router";
20
+ import {
21
+ useNavigate,
22
+ useLocation,
23
+ useRouter as useMoonshineRouter,
24
+ } from "@tschk/moonshine/router";
20
25
 
21
26
  export type RouteProps = {
22
27
  /** Pathname, always with a trailing slash — Waku's convention. */
@@ -49,9 +54,34 @@ export function toHref(to: LinkTo): string {
49
54
  return `${to.pathname}?${params.toString()}`;
50
55
  }
51
56
 
52
- function browserPart(part: "search" | "hash"): string {
53
- if (typeof window === "undefined") return "";
54
- return window.location[part];
57
+ function readHash(): string {
58
+ return typeof window === "undefined" ? "" : window.location.hash;
59
+ }
60
+ function serverHash(): string {
61
+ return "";
62
+ }
63
+
64
+ function useHash(): string {
65
+ const runtime = useMoonshineRouter();
66
+ const subscribe = useCallback(
67
+ (notify: () => void) => {
68
+ const unsubscribe = runtime.fragment.subscribe(notify);
69
+ const update = () => runtime.fragment.set(readHash());
70
+ window.addEventListener("hashchange", update);
71
+ window.addEventListener("popstate", update);
72
+ return () => {
73
+ unsubscribe();
74
+ window.removeEventListener("hashchange", update);
75
+ window.removeEventListener("popstate", update);
76
+ };
77
+ },
78
+ [runtime],
79
+ );
80
+ return useSyncExternalStore(
81
+ subscribe,
82
+ () => runtime.fragment.peek(),
83
+ serverHash,
84
+ );
55
85
  }
56
86
 
57
87
  /** Forces a re-render; used after navigations that only change the query. */
@@ -62,9 +92,11 @@ function useRerender(): () => void {
62
92
 
63
93
  export function useRoute(): RouteProps {
64
94
  // The location signal carries the query too; the path is its first half.
65
- const pathname = useLocation().split("?")[0];
66
- const search = browserPart("search");
67
- const hash = browserPart("hash");
95
+ const location = useLocation();
96
+ const index = location.indexOf("?");
97
+ const pathname = index === -1 ? location : location.slice(0, index);
98
+ const search = index === -1 ? "" : location.slice(index + 1);
99
+ const hash = useHash();
68
100
  return useMemo(
69
101
  () => ({
70
102
  path: normalizePath(pathname),
@@ -85,6 +117,7 @@ export type RouterInstance = RouteProps & {
85
117
  };
86
118
 
87
119
  export function useRouter_UNSTABLE(): RouterInstance {
120
+ const navigate = useNavigate();
88
121
  const route = useRoute();
89
122
  const rerender = useRerender();
90
123
 
@@ -92,11 +125,10 @@ export function useRouter_UNSTABLE(): RouterInstance {
92
125
  (to: LinkTo, replace: boolean, scroll?: boolean) => {
93
126
  navigate(toHref(to), { replace });
94
127
  if (scroll !== false) globalThis.scrollTo?.(0, 0);
95
- // A query-only navigation leaves the pathname signal unchanged.
96
128
  rerender();
97
129
  return Promise.resolve();
98
130
  },
99
- [rerender],
131
+ [navigate, rerender],
100
132
  );
101
133
 
102
134
  return useMemo(
@@ -186,6 +218,7 @@ export type SetSearch = (
186
218
  ) => void;
187
219
 
188
220
  export function useSetSearch_UNSTABLE({ from }: { from: string }): SetSearch {
221
+ const navigate = useNavigate();
189
222
  const { path, query } = useRoute();
190
223
  const rerender = useRerender();
191
224
  return useCallback(
@@ -202,7 +235,7 @@ export function useSetSearch_UNSTABLE({ from }: { from: string }): SetSearch {
202
235
  if (options?.scroll === true) globalThis.scrollTo?.(0, 0);
203
236
  rerender();
204
237
  },
205
- [from, path, query, rerender],
238
+ [navigate, from, path, query, rerender],
206
239
  );
207
240
  }
208
241