@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.3
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/LICENSE +21 -21
- package/README.md +33 -33
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/index.js +1724 -687
- package/dist/build/route-types.d.ts +3 -1
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/build/shared.d.ts +1 -0
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/cli/index.js +1673 -658
- package/dist/client/link.js +883 -89
- package/dist/client/router/index.d.ts +4 -0
- package/dist/client/router/link-utils.d.ts +3 -1
- package/dist/client/router/navigation.d.ts +10 -0
- package/dist/client/router/provider.d.ts +1 -1
- package/dist/client/router/search/index.d.ts +18 -0
- package/dist/client/router/search-store.d.ts +21 -0
- package/dist/client/router/types.d.ts +14 -1
- package/dist/client/sync.d.ts +25 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +44 -0
- package/dist/furin.d.ts +9 -1
- package/dist/furin.js +1578 -258
- package/dist/server/auto-invalidate/runtime.d.ts +1 -1
- package/dist/server/dev-page-plugin.d.ts +3 -0
- package/dist/server/render/assemble.d.ts +2 -0
- package/dist/server/render/isr.d.ts +2 -1
- package/dist/server/render/ssg.d.ts +3 -2
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/router/hmr.d.ts +4 -3
- package/dist/server/router/index.d.ts +1 -1
- package/dist/server/router/index.js +1 -2
- package/dist/server/router/index.ts +1 -2
- package/dist/server/router/plugin.d.ts +2 -1
- package/dist/server/router/schemas.d.ts +25 -17
- package/dist/server/sync/config.d.ts +6 -0
- package/dist/server/sync/index.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +369 -0
- package/dist/server/sync/stream.d.ts +47 -0
- package/dist/shared/search-params.d.ts +17 -0
- package/package.json +10 -5
- package/src/adapter/README.md +13 -13
- package/src/adapter/static.ts +23 -6
- package/src/build/client.ts +152 -149
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -331
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -115
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -78
- package/src/build/ssg-cache.ts +32 -31
- package/src/build/types.ts +89 -89
- package/src/client/link.tsx +13 -4
- package/src/client/router/context.ts +2 -0
- package/src/client/router/index.ts +9 -0
- package/src/client/router/link-utils.ts +184 -180
- package/src/client/router/navigation.ts +61 -0
- package/src/client/router/provider.tsx +804 -723
- package/src/client/router/search/index.ts +99 -0
- package/src/client/router/search-store.ts +86 -0
- package/src/client/router/types.ts +276 -263
- package/src/client/sync.ts +98 -0
- package/src/client.ts +369 -355
- package/src/furin.ts +465 -437
- package/src/server/auto-invalidate/runtime.ts +134 -133
- package/src/server/dev-page-plugin.ts +454 -449
- package/src/server/render/assemble.ts +160 -133
- package/src/server/render/isr.ts +323 -295
- package/src/server/render/not-found.ts +2 -0
- package/src/server/render/ssg.ts +15 -4
- package/src/server/render/ssr.ts +585 -545
- package/src/server/router/hmr.ts +14 -10
- package/src/server/router/index.ts +1 -2
- package/src/server/router/patterns.ts +5 -0
- package/src/server/router/plugin.ts +384 -401
- package/src/server/router/schemas.ts +172 -57
- package/src/server/sync/config.ts +29 -0
- package/src/server/sync/index.ts +14 -0
- package/src/server/sync/plugin.ts +108 -0
- package/src/server/sync/stream.ts +144 -0
- package/src/shared/search-params.ts +182 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { RouteManifest as LinkRouteManifest, RouteSearch, RouteTo } from "@teyik0/furin/link";
|
|
2
|
+
import { useCallback, useContext, useRef, useSyncExternalStore } from "react";
|
|
3
|
+
import {
|
|
4
|
+
findSearchDefaultsForRouteTarget,
|
|
5
|
+
type SearchParamsInput,
|
|
6
|
+
} from "../../../shared/search-params.ts";
|
|
7
|
+
import { buildHref } from "../link-utils.ts";
|
|
8
|
+
import { FALLBACK_SEARCH_STORE, SearchStoreContext } from "../search-store.ts";
|
|
9
|
+
|
|
10
|
+
export interface RouteManifest extends LinkRouteManifest {}
|
|
11
|
+
|
|
12
|
+
export type SearchRouteTo = keyof LinkRouteManifest extends never
|
|
13
|
+
? RouteTo
|
|
14
|
+
: (string & {}) | keyof LinkRouteManifest;
|
|
15
|
+
|
|
16
|
+
type SearchRouteSearch<To extends SearchRouteTo> = keyof LinkRouteManifest extends never
|
|
17
|
+
? RouteSearch<To & RouteTo>
|
|
18
|
+
: To extends keyof LinkRouteManifest
|
|
19
|
+
? LinkRouteManifest[To] extends { search?: infer S }
|
|
20
|
+
? S
|
|
21
|
+
: undefined
|
|
22
|
+
: undefined;
|
|
23
|
+
|
|
24
|
+
export type EmptyRouteSearch = Record<PropertyKey, never>;
|
|
25
|
+
|
|
26
|
+
export type ResolvedRouteSearch<To extends SearchRouteTo> =
|
|
27
|
+
SearchRouteSearch<To> extends never | undefined
|
|
28
|
+
? EmptyRouteSearch
|
|
29
|
+
: NonNullable<SearchRouteSearch<To>>;
|
|
30
|
+
|
|
31
|
+
export type SetSearchInput<To extends SearchRouteTo> =
|
|
32
|
+
| Partial<ResolvedRouteSearch<To>>
|
|
33
|
+
| ((prev: ResolvedRouteSearch<To>) => Partial<ResolvedRouteSearch<To>>);
|
|
34
|
+
|
|
35
|
+
export interface SetSearchOptions {
|
|
36
|
+
replace?: boolean;
|
|
37
|
+
resetScroll?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type SetSearch<To extends SearchRouteTo> = (
|
|
41
|
+
next: SetSearchInput<To>,
|
|
42
|
+
opts?: SetSearchOptions
|
|
43
|
+
) => Promise<void>;
|
|
44
|
+
|
|
45
|
+
function pathnameFromLogicalHref(logicalHref: string): string {
|
|
46
|
+
return new URL(logicalHref, "http://furin.local").pathname;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function useSearchSelection<To extends SearchRouteTo, TSelected>(
|
|
50
|
+
selector: (search: ResolvedRouteSearch<To>) => TSelected
|
|
51
|
+
): TSelected {
|
|
52
|
+
const store = useContext(SearchStoreContext) ?? FALLBACK_SEARCH_STORE;
|
|
53
|
+
const selectorRef = useRef(selector);
|
|
54
|
+
selectorRef.current = selector;
|
|
55
|
+
|
|
56
|
+
const getSnapshot = useCallback(
|
|
57
|
+
() => selectorRef.current(store.getSnapshot().search as ResolvedRouteSearch<To>),
|
|
58
|
+
[store]
|
|
59
|
+
);
|
|
60
|
+
const getServerSnapshot = useCallback(
|
|
61
|
+
() => selectorRef.current(store.getServerSnapshot().search as ResolvedRouteSearch<To>),
|
|
62
|
+
[store]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
return useSyncExternalStore(store.subscribe, getSnapshot, getServerSnapshot);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useSearch<To extends SearchRouteTo>(
|
|
69
|
+
_from: To
|
|
70
|
+
): [ResolvedRouteSearch<To>, SetSearch<To>];
|
|
71
|
+
export function useSearch<To extends SearchRouteTo, TSelected>(
|
|
72
|
+
_from: To,
|
|
73
|
+
selector: (search: ResolvedRouteSearch<To>) => TSelected
|
|
74
|
+
): [TSelected, SetSearch<To>];
|
|
75
|
+
export function useSearch<To extends SearchRouteTo, TSelected>(
|
|
76
|
+
_from: To,
|
|
77
|
+
selector?: (search: ResolvedRouteSearch<To>) => TSelected
|
|
78
|
+
): [ResolvedRouteSearch<To> | TSelected, SetSearch<To>] {
|
|
79
|
+
const store = useContext(SearchStoreContext) ?? FALLBACK_SEARCH_STORE;
|
|
80
|
+
const selected = useSearchSelection<To, ResolvedRouteSearch<To> | TSelected>((search) =>
|
|
81
|
+
selector ? selector(search) : search
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const setSearch = useCallback<SetSearch<To>>(
|
|
85
|
+
(next, opts) => {
|
|
86
|
+
const snapshot = store.getSnapshot();
|
|
87
|
+
const search = snapshot.search as ResolvedRouteSearch<To>;
|
|
88
|
+
const patch = typeof next === "function" ? next(search) : next;
|
|
89
|
+
const merged = { ...search, ...patch } as SearchParamsInput;
|
|
90
|
+
const pathname = pathnameFromLogicalHref(snapshot.currentHref);
|
|
91
|
+
const searchDefaults = findSearchDefaultsForRouteTarget(pathname, snapshot.searchRoutes);
|
|
92
|
+
const href = buildHref(pathname, merged, undefined, searchDefaults);
|
|
93
|
+
return snapshot.navigate(href, opts);
|
|
94
|
+
},
|
|
95
|
+
[store]
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return [selected, setSearch];
|
|
99
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import type { SearchParamsInput, SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
3
|
+
import type { RouterContextValue } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
export interface SearchStoreSnapshot {
|
|
6
|
+
currentHref: string;
|
|
7
|
+
navigate: RouterContextValue["navigate"];
|
|
8
|
+
search: SearchParamsInput;
|
|
9
|
+
searchRoutes: SearchRouteMetadata[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type SearchStoreListener = () => void;
|
|
13
|
+
|
|
14
|
+
export interface SearchStore {
|
|
15
|
+
flush: () => void;
|
|
16
|
+
getServerSnapshot: () => SearchStoreSnapshot;
|
|
17
|
+
getSnapshot: () => SearchStoreSnapshot;
|
|
18
|
+
setSnapshot: (next: SearchStoreSnapshot) => void;
|
|
19
|
+
subscribe: (listener: SearchStoreListener) => () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function searchSnapshotChanged(a: SearchStoreSnapshot, b: SearchStoreSnapshot): boolean {
|
|
23
|
+
return (
|
|
24
|
+
a.currentHref !== b.currentHref ||
|
|
25
|
+
a.navigate !== b.navigate ||
|
|
26
|
+
a.search !== b.search ||
|
|
27
|
+
a.searchRoutes !== b.searchRoutes
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createSearchStore(initialSnapshot: SearchStoreSnapshot): SearchStore {
|
|
32
|
+
let snapshot = initialSnapshot;
|
|
33
|
+
let version = 0;
|
|
34
|
+
let flushedVersion = 0;
|
|
35
|
+
const listeners = new Set<SearchStoreListener>();
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
flush: () => {
|
|
39
|
+
if (flushedVersion === version) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
flushedVersion = version;
|
|
43
|
+
for (const listener of [...listeners]) {
|
|
44
|
+
listener();
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
getServerSnapshot: () => snapshot,
|
|
48
|
+
getSnapshot: () => snapshot,
|
|
49
|
+
setSnapshot: (next) => {
|
|
50
|
+
if (!searchSnapshotChanged(snapshot, next)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
snapshot = next;
|
|
54
|
+
version++;
|
|
55
|
+
},
|
|
56
|
+
subscribe: (listener) => {
|
|
57
|
+
listeners.add(listener);
|
|
58
|
+
return () => {
|
|
59
|
+
listeners.delete(listener);
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function searchSnapshotFromRouterContext(context: RouterContextValue): SearchStoreSnapshot {
|
|
66
|
+
return {
|
|
67
|
+
currentHref: context.currentHref,
|
|
68
|
+
navigate: context.navigate,
|
|
69
|
+
search: context.search,
|
|
70
|
+
searchRoutes: context.searchRoutes,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const FALLBACK_SEARCH_STORE = createSearchStore({
|
|
75
|
+
currentHref: "/",
|
|
76
|
+
navigate: (href, _opts) => {
|
|
77
|
+
if (typeof window !== "undefined") {
|
|
78
|
+
window.location.href = href;
|
|
79
|
+
}
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
},
|
|
82
|
+
search: {},
|
|
83
|
+
searchRoutes: [],
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const SearchStoreContext = createContext<SearchStore | null>(null);
|