@tschk/moonshine-waku 0.3.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 +66 -0
- package/package.json +40 -0
- package/src/aliases.ts +53 -0
- package/src/client.ts +12 -0
- package/src/index.ts +35 -0
- package/src/link.ts +132 -0
- package/src/router.ts +215 -0
- package/src/server.ts +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# `@tschk/moonshine-waku`
|
|
2
|
+
|
|
3
|
+
Waku's **client router API** reimplemented on moonshine. It imports no Waku code
|
|
4
|
+
and lists none as a dependency: `Link` and the `*_UNSTABLE` hooks are ordinary
|
|
5
|
+
React written against `@tschk/moonshine/router`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// bundler alias
|
|
9
|
+
"waku/router/client" -> "@tschk/moonshine-waku";
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import {
|
|
14
|
+
Link,
|
|
15
|
+
useRouter_UNSTABLE,
|
|
16
|
+
useParams_UNSTABLE,
|
|
17
|
+
} from "@tschk/moonshine-waku";
|
|
18
|
+
import { json } from "@tschk/moonshine-waku/server";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
| Subpath | Contents |
|
|
22
|
+
| ---------- | -------------------------------------- |
|
|
23
|
+
| `.` | client router API |
|
|
24
|
+
| `./router` | hooks, route matching, `prefetchRoute` |
|
|
25
|
+
| `./link` | `Link` |
|
|
26
|
+
| `./server` | `json` + moonshine server handling |
|
|
27
|
+
| `./client` | moonshine signals + resources |
|
|
28
|
+
|
|
29
|
+
## Supported
|
|
30
|
+
|
|
31
|
+
- `Link` — `to` as string or `{ pathname, search }`, `scroll`,
|
|
32
|
+
`unstable_instant`, `unstable_prefetchOnEnter`, `unstable_prefetchOnView`
|
|
33
|
+
(IntersectionObserver at 0.1 threshold), `unstable_startTransition`.
|
|
34
|
+
Modifier-clicks fall through to the browser; navigation runs inside
|
|
35
|
+
`startTransition`.
|
|
36
|
+
- `useRouter_UNSTABLE` / `useRouter` — spreads the current `{ path, query, hash }`
|
|
37
|
+
route plus `push`, `replace`, `reload`, `back`, `forward`, `prefetch`.
|
|
38
|
+
- `useRoute` — Waku's route shape, with the trailing-slash `path` convention.
|
|
39
|
+
- `useParams_UNSTABLE({ from })` — `[slug]` and `[...rest]` matching, `null` when
|
|
40
|
+
the current path does not match.
|
|
41
|
+
- `useSearch_UNSTABLE({ from })`, `useSetSearch_UNSTABLE({ from })`.
|
|
42
|
+
- `useNavigationStatus_UNSTABLE()` — `{ pending }` inside a `Link`, `{}` outside.
|
|
43
|
+
- `matchRouteParams`, `normalizePath`, `toHref`, `prefetchRoute`.
|
|
44
|
+
|
|
45
|
+
## Not supported
|
|
46
|
+
|
|
47
|
+
Waku's remaining surface is bound to its RSC pipeline and Vite plugin, and is
|
|
48
|
+
**not** provided:
|
|
49
|
+
|
|
50
|
+
- **RSC**: `Slice`, `Children`, `unstable_defineRouter`, `createPages`,
|
|
51
|
+
`fsRouter`, `INTERNAL_ServerRouter`, `Router`, `ServerRoot`, the RSC wire
|
|
52
|
+
format and its element/slot streaming. Moonshine renders on its own React
|
|
53
|
+
renderer; there is no server-component payload to fetch.
|
|
54
|
+
- **Server APIs**: `waku/server` (`getContext`, `unstable_getPlatformObject`),
|
|
55
|
+
`waku/config`, middleware, `unstable_cache`.
|
|
56
|
+
- **Typed search codecs**: `Unstable_SearchCodecsProvider` and route-level codec
|
|
57
|
+
registration. `useSearch_UNSTABLE` returns a plain `URLSearchParams` instead of
|
|
58
|
+
a codec-parsed object, and `useSetSearch_UNSTABLE` takes a plain string record.
|
|
59
|
+
- **Route prefetch**: `prefetch` warms the browser cache with
|
|
60
|
+
`<link rel="prefetch">`; it does not fetch and cache an RSC payload, so
|
|
61
|
+
`unstable_instant` navigation is a plain synchronous navigation.
|
|
62
|
+
- **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.
|
|
65
|
+
|
|
66
|
+
See [Moonshine README](../../README.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tschk/moonshine-waku",
|
|
3
|
+
"version": "0.3.4",
|
|
4
|
+
"description": "Waku client router API reimplemented on moonshine — drop waku, keep the imports",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.ts",
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.ts",
|
|
11
|
+
"./client": "./src/client.ts",
|
|
12
|
+
"./server": "./src/server.ts",
|
|
13
|
+
"./router": "./src/router.ts",
|
|
14
|
+
"./link": "./src/link.ts",
|
|
15
|
+
"./aliases": "./src/aliases.ts"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@tschk/moonshine": "^0.3.4",
|
|
19
|
+
"@tschk/moonshine-router": "^0.3.4"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": ">=18",
|
|
23
|
+
"react-dom": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"moonshine",
|
|
31
|
+
"waku",
|
|
32
|
+
"adapter"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"format": "bunx prettier --check .",
|
|
36
|
+
"lint": "bunx oxlint .",
|
|
37
|
+
"typecheck": "bunx tsc --noEmit",
|
|
38
|
+
"test": "bun test"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/aliases.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drop-in mapping from Waku's client router specifiers onto this package.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* import { moonshineWakuPlugin } from "@tschk/moonshine-waku/aliases";
|
|
6
|
+
* await Bun.build({ entrypoints: ["./app.tsx"], plugins: [moonshineWakuPlugin()] });
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import type { BunPlugin } from "bun";
|
|
11
|
+
|
|
12
|
+
/** Bare specifier each replaced module is served by. */
|
|
13
|
+
export const wakuAliases: Readonly<Record<string, string>> = {
|
|
14
|
+
"waku/router/client": "@tschk/moonshine-waku",
|
|
15
|
+
"waku/router": "@tschk/moonshine-waku",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** File in this package backing each alias, relative to `src/`. */
|
|
19
|
+
const files: Readonly<Record<string, string>> = {
|
|
20
|
+
"waku/router/client": "index.ts",
|
|
21
|
+
"waku/router": "index.ts",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* `compilerOptions.paths` block for an app's tsconfig, so the type checker
|
|
26
|
+
* follows the same mapping the bundler applies.
|
|
27
|
+
*/
|
|
28
|
+
export function tsconfigPaths(): Record<string, string[]> {
|
|
29
|
+
return Object.fromEntries(
|
|
30
|
+
Object.entries(wakuAliases).map(([from, to]) => [from, [to]]),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Absolute path this package serves `specifier` from, if it is aliased. */
|
|
35
|
+
export function resolveAlias(specifier: string): string | undefined {
|
|
36
|
+
const file = files[specifier];
|
|
37
|
+
return file === undefined ? undefined : join(import.meta.dir, file);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const filter = /^waku\/router(\/client)?$/;
|
|
41
|
+
|
|
42
|
+
/** Bun bundler/runtime plugin rewriting Waku router imports to this package. */
|
|
43
|
+
export function moonshineWakuPlugin(): BunPlugin {
|
|
44
|
+
return {
|
|
45
|
+
name: "moonshine-waku-aliases",
|
|
46
|
+
setup(build) {
|
|
47
|
+
build.onResolve({ filter }, (args) => {
|
|
48
|
+
const path = resolveAlias(args.path);
|
|
49
|
+
return path === undefined ? undefined : { path };
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
package/src/client.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @tschk/moonshine-waku
|
|
5
|
+
*
|
|
6
|
+
* Waku's client router API reimplemented on moonshine's router. Alias
|
|
7
|
+
* `waku/router/client` to this package to drop the dependency.
|
|
8
|
+
*
|
|
9
|
+
* Waku's RSC pieces (`Slice`, `Children`, `unstable_defineRouter`) are not
|
|
10
|
+
* provided — see the README.
|
|
11
|
+
*/
|
|
12
|
+
export { Link, default as LinkDefault } from "./link";
|
|
13
|
+
export type { LinkProps } from "./link";
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
NavigationStatusContext,
|
|
17
|
+
matchRouteParams,
|
|
18
|
+
normalizePath,
|
|
19
|
+
prefetchRoute,
|
|
20
|
+
toHref,
|
|
21
|
+
useNavigationStatus_UNSTABLE,
|
|
22
|
+
useParams_UNSTABLE,
|
|
23
|
+
useRoute,
|
|
24
|
+
useRouter,
|
|
25
|
+
useRouter_UNSTABLE,
|
|
26
|
+
useSearch_UNSTABLE,
|
|
27
|
+
useSetSearch_UNSTABLE,
|
|
28
|
+
} from "./router";
|
|
29
|
+
export type {
|
|
30
|
+
LinkTo,
|
|
31
|
+
NavigationStatus,
|
|
32
|
+
RouteProps,
|
|
33
|
+
RouterInstance,
|
|
34
|
+
SetSearch,
|
|
35
|
+
} from "./router";
|
package/src/link.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Waku's `<Link>` on the moonshine router.
|
|
5
|
+
*
|
|
6
|
+
* ```tsx
|
|
7
|
+
* import { Link } from "@tschk/moonshine-waku/link";
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
createElement,
|
|
12
|
+
useCallback,
|
|
13
|
+
useEffect,
|
|
14
|
+
useMemo,
|
|
15
|
+
useRef,
|
|
16
|
+
useTransition,
|
|
17
|
+
type AnchorHTMLAttributes,
|
|
18
|
+
type MouseEvent,
|
|
19
|
+
type ReactNode,
|
|
20
|
+
} from "react";
|
|
21
|
+
import { navigate } from "@tschk/moonshine/router";
|
|
22
|
+
import {
|
|
23
|
+
NavigationStatusContext,
|
|
24
|
+
prefetchRoute,
|
|
25
|
+
toHref,
|
|
26
|
+
type LinkTo,
|
|
27
|
+
} from "./router";
|
|
28
|
+
|
|
29
|
+
export type LinkProps = Omit<
|
|
30
|
+
AnchorHTMLAttributes<HTMLAnchorElement>,
|
|
31
|
+
"href"
|
|
32
|
+
> & {
|
|
33
|
+
to: LinkTo;
|
|
34
|
+
/** `false` keeps the scroll position after navigating. */
|
|
35
|
+
scroll?: boolean;
|
|
36
|
+
/** Skips the transition and navigates synchronously. */
|
|
37
|
+
unstable_instant?: boolean;
|
|
38
|
+
unstable_prefetchOnEnter?: boolean;
|
|
39
|
+
unstable_prefetchOnView?: boolean;
|
|
40
|
+
unstable_startTransition?: (fn: () => void) => void;
|
|
41
|
+
children?: ReactNode;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function isAltClick(event: MouseEvent<HTMLAnchorElement>): boolean {
|
|
45
|
+
return (
|
|
46
|
+
event.button !== 0 ||
|
|
47
|
+
event.metaKey ||
|
|
48
|
+
event.altKey ||
|
|
49
|
+
event.ctrlKey ||
|
|
50
|
+
event.shiftKey
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function Link({
|
|
55
|
+
to,
|
|
56
|
+
scroll,
|
|
57
|
+
unstable_instant,
|
|
58
|
+
unstable_prefetchOnEnter,
|
|
59
|
+
unstable_prefetchOnView,
|
|
60
|
+
unstable_startTransition,
|
|
61
|
+
children,
|
|
62
|
+
onClick,
|
|
63
|
+
onMouseEnter,
|
|
64
|
+
...rest
|
|
65
|
+
}: LinkProps): ReactNode {
|
|
66
|
+
const href = toHref(to);
|
|
67
|
+
const ref = useRef<HTMLAnchorElement>(null);
|
|
68
|
+
const [isPending, startTransition] = useTransition();
|
|
69
|
+
const run = unstable_startTransition ?? startTransition;
|
|
70
|
+
|
|
71
|
+
const warm = useCallback(() => prefetchRoute(href), [href]);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
const node = ref.current;
|
|
75
|
+
if (
|
|
76
|
+
!unstable_prefetchOnView ||
|
|
77
|
+
!node ||
|
|
78
|
+
typeof IntersectionObserver === "undefined"
|
|
79
|
+
) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const observer = new IntersectionObserver(
|
|
83
|
+
(entries) => {
|
|
84
|
+
for (const entry of entries) {
|
|
85
|
+
if (entry.isIntersecting) {
|
|
86
|
+
warm();
|
|
87
|
+
observer.disconnect();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
{ threshold: 0.1 },
|
|
92
|
+
);
|
|
93
|
+
observer.observe(node);
|
|
94
|
+
return () => observer.disconnect();
|
|
95
|
+
}, [unstable_prefetchOnView, warm]);
|
|
96
|
+
|
|
97
|
+
const status = useMemo(() => ({ pending: isPending }), [isPending]);
|
|
98
|
+
|
|
99
|
+
const anchor = createElement(
|
|
100
|
+
"a",
|
|
101
|
+
{
|
|
102
|
+
...rest,
|
|
103
|
+
ref,
|
|
104
|
+
href,
|
|
105
|
+
onMouseEnter: (event: MouseEvent<HTMLAnchorElement>) => {
|
|
106
|
+
if (unstable_prefetchOnEnter) warm();
|
|
107
|
+
onMouseEnter?.(event);
|
|
108
|
+
},
|
|
109
|
+
onClick: (event: MouseEvent<HTMLAnchorElement>) => {
|
|
110
|
+
onClick?.(event);
|
|
111
|
+
if (event.defaultPrevented || isAltClick(event)) return;
|
|
112
|
+
event.preventDefault();
|
|
113
|
+
warm();
|
|
114
|
+
const go = () => {
|
|
115
|
+
navigate(href);
|
|
116
|
+
if (scroll !== false) globalThis.scrollTo?.(0, 0);
|
|
117
|
+
};
|
|
118
|
+
if (unstable_instant) go();
|
|
119
|
+
else run(go);
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
children,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return createElement(
|
|
126
|
+
NavigationStatusContext.Provider,
|
|
127
|
+
{ value: status },
|
|
128
|
+
anchor,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export default Link;
|
package/src/router.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Waku's `waku/router/client` hooks implemented on the moonshine router.
|
|
5
|
+
*
|
|
6
|
+
* The route shape mirrors Waku's own `parseRoute`: `{ path, query, hash }`.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { useRouter_UNSTABLE } from "@tschk/moonshine-waku/router";
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
createContext,
|
|
14
|
+
useCallback,
|
|
15
|
+
useContext,
|
|
16
|
+
useMemo,
|
|
17
|
+
useReducer,
|
|
18
|
+
} from "react";
|
|
19
|
+
import { navigate, useLocation } from "@tschk/moonshine/router";
|
|
20
|
+
|
|
21
|
+
export type RouteProps = {
|
|
22
|
+
/** Pathname, always with a trailing slash — Waku's convention. */
|
|
23
|
+
path: string;
|
|
24
|
+
/** Query string without the leading `?`. */
|
|
25
|
+
query: string;
|
|
26
|
+
/** Hash including the leading `#`, or an empty string. */
|
|
27
|
+
hash: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type LinkTo =
|
|
31
|
+
string | { pathname: string; search?: Record<string, unknown> };
|
|
32
|
+
|
|
33
|
+
/** Waku normalises every route path to a trailing slash. */
|
|
34
|
+
export function normalizePath(pathname: string): string {
|
|
35
|
+
if (!pathname) return "/";
|
|
36
|
+
return pathname.endsWith("/") ? pathname : `${pathname}/`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function toHref(to: LinkTo): string {
|
|
40
|
+
if (typeof to === "string") return to;
|
|
41
|
+
const entries = Object.entries(to.search ?? {}).filter(
|
|
42
|
+
([, value]) => value !== undefined,
|
|
43
|
+
);
|
|
44
|
+
if (entries.length === 0) return to.pathname;
|
|
45
|
+
const params = new URLSearchParams();
|
|
46
|
+
for (const [key, value] of entries) {
|
|
47
|
+
params.set(key, typeof value === "string" ? value : JSON.stringify(value));
|
|
48
|
+
}
|
|
49
|
+
return `${to.pathname}?${params.toString()}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function browserPart(part: "search" | "hash"): string {
|
|
53
|
+
if (typeof window === "undefined") return "";
|
|
54
|
+
return window.location[part];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Forces a re-render; used after navigations that only change the query. */
|
|
58
|
+
function useRerender(): () => void {
|
|
59
|
+
const [, bump] = useReducer((n: number) => n + 1, 0);
|
|
60
|
+
return bump;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function useRoute(): RouteProps {
|
|
64
|
+
const pathname = useLocation();
|
|
65
|
+
const search = browserPart("search");
|
|
66
|
+
const hash = browserPart("hash");
|
|
67
|
+
return useMemo(
|
|
68
|
+
() => ({
|
|
69
|
+
path: normalizePath(pathname),
|
|
70
|
+
query: search.startsWith("?") ? search.slice(1) : search,
|
|
71
|
+
hash,
|
|
72
|
+
}),
|
|
73
|
+
[pathname, search, hash],
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type RouterInstance = RouteProps & {
|
|
78
|
+
push(to: LinkTo, options?: { scroll?: boolean }): Promise<void>;
|
|
79
|
+
replace(to: LinkTo, options?: { scroll?: boolean }): Promise<void>;
|
|
80
|
+
reload(): Promise<void>;
|
|
81
|
+
back(): void;
|
|
82
|
+
forward(): void;
|
|
83
|
+
prefetch(to: LinkTo): void;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export function useRouter_UNSTABLE(): RouterInstance {
|
|
87
|
+
const route = useRoute();
|
|
88
|
+
const rerender = useRerender();
|
|
89
|
+
|
|
90
|
+
const go = useCallback(
|
|
91
|
+
(to: LinkTo, replace: boolean, scroll?: boolean) => {
|
|
92
|
+
navigate(toHref(to), { replace });
|
|
93
|
+
if (scroll !== false) globalThis.scrollTo?.(0, 0);
|
|
94
|
+
// A query-only navigation leaves the pathname signal unchanged.
|
|
95
|
+
rerender();
|
|
96
|
+
return Promise.resolve();
|
|
97
|
+
},
|
|
98
|
+
[rerender],
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return useMemo(
|
|
102
|
+
() => ({
|
|
103
|
+
...route,
|
|
104
|
+
push: (to, options) => go(to, false, options?.scroll),
|
|
105
|
+
replace: (to, options) => go(to, true, options?.scroll),
|
|
106
|
+
reload: () => {
|
|
107
|
+
globalThis.location?.reload();
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
},
|
|
110
|
+
back: () => globalThis.history?.back(),
|
|
111
|
+
forward: () => globalThis.history?.forward(),
|
|
112
|
+
prefetch: (to) => prefetchRoute(toHref(to)),
|
|
113
|
+
}),
|
|
114
|
+
[route, go],
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Alias — Waku ships the hook under both names. */
|
|
119
|
+
export const useRouter = useRouter_UNSTABLE;
|
|
120
|
+
|
|
121
|
+
const prefetched = new Set<string>();
|
|
122
|
+
|
|
123
|
+
/** Warms the browser cache for `url` once, the way Waku prefetches a route. */
|
|
124
|
+
export function prefetchRoute(url: string): void {
|
|
125
|
+
if (prefetched.has(url) || typeof document === "undefined") return;
|
|
126
|
+
prefetched.add(url);
|
|
127
|
+
const tag = document.createElement("link");
|
|
128
|
+
tag.rel = "prefetch";
|
|
129
|
+
tag.href = url;
|
|
130
|
+
document.head.append(tag);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Matches a Waku route template such as `/posts/[slug]` against a path. */
|
|
134
|
+
export function matchRouteParams(
|
|
135
|
+
from: string,
|
|
136
|
+
path: string,
|
|
137
|
+
): Record<string, string> | null {
|
|
138
|
+
const template = normalizePath(from).split("/").filter(Boolean);
|
|
139
|
+
const actual = normalizePath(path).split("/").filter(Boolean);
|
|
140
|
+
const params: Record<string, string> = {};
|
|
141
|
+
for (let i = 0; i < template.length; i++) {
|
|
142
|
+
const segment = template[i]!;
|
|
143
|
+
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
144
|
+
params[segment.slice(4, -1)] = actual.slice(i).join("/");
|
|
145
|
+
return params;
|
|
146
|
+
}
|
|
147
|
+
const value = actual[i];
|
|
148
|
+
if (value === undefined) return null;
|
|
149
|
+
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
150
|
+
params[segment.slice(1, -1)] = value;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (segment !== value) return null;
|
|
154
|
+
}
|
|
155
|
+
return actual.length === template.length ? params : null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function useParams_UNSTABLE({
|
|
159
|
+
from,
|
|
160
|
+
}: {
|
|
161
|
+
from: string;
|
|
162
|
+
}): Record<string, string> | null {
|
|
163
|
+
const { path } = useRoute();
|
|
164
|
+
return useMemo(() => matchRouteParams(from, path), [from, path]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function useSearch_UNSTABLE({
|
|
168
|
+
from,
|
|
169
|
+
}: {
|
|
170
|
+
from: string;
|
|
171
|
+
}): URLSearchParams | null {
|
|
172
|
+
const { path, query } = useRoute();
|
|
173
|
+
return useMemo(
|
|
174
|
+
() =>
|
|
175
|
+
matchRouteParams(from, path) === null ? null : new URLSearchParams(query),
|
|
176
|
+
[from, path, query],
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type SetSearch = (
|
|
181
|
+
update:
|
|
182
|
+
| Record<string, string>
|
|
183
|
+
| ((current: URLSearchParams) => Record<string, string>),
|
|
184
|
+
options?: { scroll?: boolean; history?: "push" | "replace" },
|
|
185
|
+
) => void;
|
|
186
|
+
|
|
187
|
+
export function useSetSearch_UNSTABLE({ from }: { from: string }): SetSearch {
|
|
188
|
+
const { path, query } = useRoute();
|
|
189
|
+
const rerender = useRerender();
|
|
190
|
+
return useCallback(
|
|
191
|
+
(update, options) => {
|
|
192
|
+
if (matchRouteParams(from, path) === null) return;
|
|
193
|
+
const current = new URLSearchParams(query);
|
|
194
|
+
const partial = typeof update === "function" ? update(current) : update;
|
|
195
|
+
for (const [key, value] of Object.entries(partial))
|
|
196
|
+
current.set(key, value);
|
|
197
|
+
const next = current.toString();
|
|
198
|
+
navigate(`${path}${next ? `?${next}` : ""}`, {
|
|
199
|
+
replace: options?.history === "replace",
|
|
200
|
+
});
|
|
201
|
+
if (options?.scroll === true) globalThis.scrollTo?.(0, 0);
|
|
202
|
+
rerender();
|
|
203
|
+
},
|
|
204
|
+
[from, path, query, rerender],
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export type NavigationStatus = { pending?: boolean };
|
|
209
|
+
|
|
210
|
+
export const NavigationStatusContext = createContext<NavigationStatus>({});
|
|
211
|
+
|
|
212
|
+
/** Navigation status of the enclosing `Link`, like React's `useFormStatus`. */
|
|
213
|
+
export function useNavigationStatus_UNSTABLE(): NavigationStatus {
|
|
214
|
+
return useContext(NavigationStatusContext);
|
|
215
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server surface for Waku apps on moonshine.
|
|
3
|
+
*
|
|
4
|
+
* Waku's `waku/server` (`getContext`, `unstable_getPlatformObject`) is tied to
|
|
5
|
+
* its RSC pipeline and is not reimplemented — see the README.
|
|
6
|
+
*/
|
|
7
|
+
export {
|
|
8
|
+
createMoonshineServer,
|
|
9
|
+
definePage,
|
|
10
|
+
handleMoonshineRequest,
|
|
11
|
+
resolvePage,
|
|
12
|
+
toMoonshineRequest,
|
|
13
|
+
} from "@tschk/moonshine/server";
|
|
14
|
+
export type {
|
|
15
|
+
MoonshinePageModule,
|
|
16
|
+
MoonshineRequest,
|
|
17
|
+
MoonshineServer,
|
|
18
|
+
} from "@tschk/moonshine/server";
|
|
19
|
+
|
|
20
|
+
/** JSON `Response` helper for API routes. */
|
|
21
|
+
export function json<T>(data: T, init: ResponseInit = {}): Response {
|
|
22
|
+
const headers = new Headers(init.headers);
|
|
23
|
+
if (!headers.has("content-type")) {
|
|
24
|
+
headers.set("content-type", "application/json; charset=utf-8");
|
|
25
|
+
}
|
|
26
|
+
return new Response(JSON.stringify(data), { ...init, headers });
|
|
27
|
+
}
|