@kiln-js/client 0.1.3 → 0.2.0
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 +51 -0
- package/dist/client.d.ts +12 -3
- package/dist/error-boundary.d.ts +15 -0
- package/dist/hydration-viewer.d.ts +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +291 -103
- package/dist/types.d.ts +2 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @kiln-js/client
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@kiln-js/client)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
Part of the Kiln framework.
|
|
7
|
+
|
|
8
|
+
Part of [Kiln](https://github.com/kiln-js/kiln), a file-based server-side rendering framework for React and Vite.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bun add @kiln-js/client
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import { createClient } from "@kiln-js/client";
|
|
20
|
+
|
|
21
|
+
createClient().then((client) => client.start());
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { Link } from "@kiln-js/client";
|
|
26
|
+
|
|
27
|
+
export default function Layout({ children }: { children: React.ReactNode }) {
|
|
28
|
+
return (
|
|
29
|
+
<>
|
|
30
|
+
<nav><Link href="/">Home</Link> <Link href="/about">About</Link></nav>
|
|
31
|
+
{children}
|
|
32
|
+
</>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Navigation
|
|
38
|
+
|
|
39
|
+
`Link` navigates without a full reload. Modifier clicks (Ctrl, Cmd, Shift, middle button) keep the default browser behavior.
|
|
40
|
+
|
|
41
|
+
## Documentation
|
|
42
|
+
|
|
43
|
+
See the [Kiln repository](https://github.com/kiln-js/kiln) for the full API reference and guides.
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
This repository is generated from the Kiln monorepo. Make changes there, then run `scripts/publish-repo.ps1` to publish them here. Report issues in this repository, and send pull requests to the monorepo.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT
|
package/dist/client.d.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import { type AnchorHTMLAttributes } from "react";
|
|
2
2
|
import type { ClientRoute } from "./types";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
interface NavigateOptions {
|
|
4
|
+
push?: boolean;
|
|
5
|
+
scroll?: boolean;
|
|
6
|
+
restoreY?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function navigate(to: string, options?: NavigateOptions | boolean): Promise<void>;
|
|
9
|
+
export declare function prefetch(to: string): void;
|
|
10
|
+
export declare function invalidate(path?: string): void;
|
|
11
|
+
export interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
|
|
5
12
|
href: string;
|
|
6
|
-
}
|
|
13
|
+
}
|
|
14
|
+
export declare function Link({ href, onClick, onMouseEnter, onFocus, target, download, ...rest }: LinkProps): import("react").JSX.Element;
|
|
7
15
|
export declare function createClient(options?: {
|
|
8
16
|
routes?: ClientRoute[];
|
|
9
17
|
}): Promise<{
|
|
10
18
|
start(): Promise<void>;
|
|
11
19
|
}>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Component, type ReactNode } from "react";
|
|
2
|
+
interface Props {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
}
|
|
5
|
+
interface State {
|
|
6
|
+
error: Error | null;
|
|
7
|
+
}
|
|
8
|
+
export declare class ErrorBoundary extends Component<Props, State> {
|
|
9
|
+
state: State;
|
|
10
|
+
static getDerivedStateFromError(error: Error): State;
|
|
11
|
+
componentDidCatch(error: Error): void;
|
|
12
|
+
private reset;
|
|
13
|
+
render(): string | number | boolean | Iterable<ReactNode> | import("react").JSX.Element | null | undefined;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function reportHydrationMismatch(serverHtml: string, container: Element, error: unknown): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export { createClient, navigate, Link } from "./client";
|
|
2
|
-
export {
|
|
1
|
+
export { createClient, navigate, prefetch, invalidate, Link } from "./client";
|
|
2
|
+
export type { LinkProps } from "./client";
|
|
3
|
+
export { ErrorBoundary } from "./error-boundary";
|
|
4
|
+
export { usePathname, useParams, ClientOnly, useIsClient, Await, defer } from "@kiln-js/core";
|
|
3
5
|
export type { ClientRoute, KilnBoot, RenderMode } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,79 +1,142 @@
|
|
|
1
|
-
// src/client.tsx
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
// packages/client/src/client.tsx
|
|
2
|
+
import { createElement, useState } from "react";
|
|
3
|
+
import { createRoot, hydrateRoot } from "react-dom/client";
|
|
4
|
+
import { compose, matchRoute, RouteProvider } from "@kiln-js/core";
|
|
4
5
|
|
|
5
|
-
//
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
else if (seg !== parts[i]) {
|
|
31
|
-
ok = false;
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
i++;
|
|
35
|
-
}
|
|
36
|
-
if (ok && i === parts.length)
|
|
37
|
-
return { route, params };
|
|
6
|
+
// packages/client/src/error-boundary.tsx
|
|
7
|
+
import { Component } from "react";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
var ErrorBoundary = class extends Component {
|
|
10
|
+
state = { error: null };
|
|
11
|
+
static getDerivedStateFromError(error) {
|
|
12
|
+
return { error };
|
|
13
|
+
}
|
|
14
|
+
componentDidCatch(error) {
|
|
15
|
+
console.error("[kiln] render error:", error);
|
|
16
|
+
}
|
|
17
|
+
reset = () => this.setState({ error: null });
|
|
18
|
+
render() {
|
|
19
|
+
if (!this.state.error) return this.props.children;
|
|
20
|
+
return /* @__PURE__ */ jsxs("div", { role: "alert", style: { padding: "2rem", fontFamily: "system-ui, sans-serif", maxWidth: "40rem" }, children: [
|
|
21
|
+
/* @__PURE__ */ jsx("h1", { style: { fontSize: "1.5rem", margin: "0 0 0.5rem" }, children: "Something went wrong on this page" }),
|
|
22
|
+
/* @__PURE__ */ jsx("p", { style: { opacity: 0.75 }, children: "The rest of the site still works. Try again, or go back to the home page." }),
|
|
23
|
+
/* @__PURE__ */ jsxs("details", { style: { margin: "1rem 0" }, children: [
|
|
24
|
+
/* @__PURE__ */ jsx("summary", { children: "Details" }),
|
|
25
|
+
/* @__PURE__ */ jsx("pre", { style: { whiteSpace: "pre-wrap" }, children: this.state.error.message })
|
|
26
|
+
] }),
|
|
27
|
+
/* @__PURE__ */ jsx("button", { type: "button", onClick: this.reset, children: "Try again" }),
|
|
28
|
+
" ",
|
|
29
|
+
/* @__PURE__ */ jsx("a", { href: "/", children: "Home" })
|
|
30
|
+
] });
|
|
38
31
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// packages/client/src/hydration-viewer.ts
|
|
35
|
+
var DEV = import.meta.env?.DEV === true;
|
|
36
|
+
var shown = false;
|
|
37
|
+
function reportHydrationMismatch(serverHtml, container, error) {
|
|
38
|
+
if (!DEV) return;
|
|
39
|
+
console.warn("[kiln] hydration mismatch", error);
|
|
40
|
+
if (shown) return;
|
|
41
|
+
shown = true;
|
|
42
|
+
setTimeout(() => showPanel(serverHtml, container.innerHTML, error), 0);
|
|
43
|
+
}
|
|
44
|
+
function lines(html) {
|
|
45
|
+
return html.replace(/></g, ">\n<").split("\n");
|
|
46
|
+
}
|
|
47
|
+
function el(tag, text, css) {
|
|
48
|
+
const node = document.createElement(tag);
|
|
49
|
+
if (text !== void 0) node.textContent = text;
|
|
50
|
+
if (css) Object.assign(node.style, css);
|
|
51
|
+
return node;
|
|
52
|
+
}
|
|
53
|
+
function column(title, rows, other) {
|
|
54
|
+
const wrap = el("div", void 0, { minWidth: "0" });
|
|
55
|
+
wrap.append(el("div", title, { color: "#8b949e", marginBottom: "6px", fontWeight: "600" }));
|
|
56
|
+
const body = el("div", void 0, { whiteSpace: "pre-wrap", wordBreak: "break-all" });
|
|
57
|
+
rows.forEach((row, i) => {
|
|
58
|
+
const differs = row !== other[i];
|
|
59
|
+
body.append(el("div", row || " ", differs ? { background: "rgba(248,81,73,0.22)" } : void 0));
|
|
54
60
|
});
|
|
61
|
+
wrap.append(body);
|
|
62
|
+
return wrap;
|
|
55
63
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
function showPanel(serverHtml, clientHtml, error) {
|
|
65
|
+
document.querySelector("[data-kiln-hydration]")?.remove();
|
|
66
|
+
const server = lines(serverHtml);
|
|
67
|
+
const client = lines(clientHtml);
|
|
68
|
+
const panel = el("div", void 0, {
|
|
69
|
+
position: "fixed",
|
|
70
|
+
left: "0",
|
|
71
|
+
right: "0",
|
|
72
|
+
bottom: "0",
|
|
73
|
+
maxHeight: "50vh",
|
|
74
|
+
overflow: "auto",
|
|
75
|
+
zIndex: "2147483647",
|
|
76
|
+
background: "#0e1116",
|
|
77
|
+
color: "#e6edf3",
|
|
78
|
+
font: "12px/1.5 ui-monospace, monospace",
|
|
79
|
+
padding: "14px 18px",
|
|
80
|
+
borderTop: "2px solid #f85149"
|
|
81
|
+
});
|
|
82
|
+
panel.setAttribute("data-kiln-hydration", "");
|
|
83
|
+
const header = el("div", void 0, { display: "flex", gap: "12px", alignItems: "baseline", marginBottom: "8px" });
|
|
84
|
+
header.append(el("strong", "Hydration mismatch", { color: "#ff7b72" }));
|
|
85
|
+
header.append(el("span", error instanceof Error ? error.message : String(error), { color: "#8b949e", flex: "1" }));
|
|
86
|
+
const close = el("button", "Close", { cursor: "pointer", background: "transparent", color: "#e6edf3", border: "1px solid #30363d", borderRadius: "6px", padding: "2px 10px" });
|
|
87
|
+
close.onclick = () => panel.remove();
|
|
88
|
+
header.append(close);
|
|
89
|
+
panel.append(header);
|
|
90
|
+
panel.append(
|
|
91
|
+
el(
|
|
92
|
+
"p",
|
|
93
|
+
"Highlighted lines differ. The page shows the browser version. Common causes: Date.now(), Math.random(), toLocaleString(), or window checks during render.",
|
|
94
|
+
{ margin: "0 0 10px", color: "#8b949e" }
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
const grid = el("div", void 0, { display: "grid", gridTemplateColumns: "1fr 1fr", gap: "16px" });
|
|
98
|
+
grid.append(column("Server HTML", server, client), column("Browser HTML", client, server));
|
|
99
|
+
panel.append(grid);
|
|
100
|
+
document.body.append(panel);
|
|
67
101
|
}
|
|
68
102
|
|
|
69
|
-
// src/client.tsx
|
|
70
|
-
import { jsx as
|
|
103
|
+
// packages/client/src/client.tsx
|
|
104
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
71
105
|
var routes = [];
|
|
72
|
-
var setCurrent = () => {
|
|
106
|
+
var setCurrent = () => {
|
|
107
|
+
};
|
|
108
|
+
var mounted = false;
|
|
109
|
+
var latestNavigation = 0;
|
|
110
|
+
var currentPath = "";
|
|
111
|
+
var defaultTitle = "";
|
|
112
|
+
var dataCache = /* @__PURE__ */ new Map();
|
|
113
|
+
var isRedirect = (v) => typeof v === "object" && v !== null && typeof v.redirect === "string";
|
|
114
|
+
function fetchData(path) {
|
|
115
|
+
const request = fetch(`/__kiln/data?path=${encodeURIComponent(path)}`, {
|
|
116
|
+
headers: { accept: "application/json" }
|
|
117
|
+
}).then(async (res) => {
|
|
118
|
+
if (!res.ok) throw new Error(`Loader request for ${path} failed with ${res.status}`);
|
|
119
|
+
return res.json();
|
|
120
|
+
});
|
|
121
|
+
request.catch(() => dataCache.delete(path));
|
|
122
|
+
return request;
|
|
123
|
+
}
|
|
124
|
+
function takeData(path) {
|
|
125
|
+
const cached = dataCache.get(path);
|
|
126
|
+
if (cached) {
|
|
127
|
+
dataCache.delete(path);
|
|
128
|
+
return cached;
|
|
129
|
+
}
|
|
130
|
+
return fetchData(path);
|
|
131
|
+
}
|
|
132
|
+
function sameSiteUrl(to) {
|
|
133
|
+
const url = new URL(to, location.href);
|
|
134
|
+
return url.origin === location.origin ? url : null;
|
|
135
|
+
}
|
|
136
|
+
var isNavigable = (route) => !!route && !!route.load && route.mode !== "server";
|
|
73
137
|
async function resolve(url, data) {
|
|
74
138
|
const match = matchRoute(routes, url.pathname);
|
|
75
|
-
if (!match
|
|
76
|
-
throw new Error(`No client route for ${url.pathname}`);
|
|
139
|
+
if (!match || !isNavigable(match.route)) return null;
|
|
77
140
|
const [mod, ...layouts] = await Promise.all([
|
|
78
141
|
match.route.load(),
|
|
79
142
|
...match.route.layouts.map((l) => l())
|
|
@@ -89,62 +152,187 @@ async function resolve(url, data) {
|
|
|
89
152
|
function App({ initial }) {
|
|
90
153
|
const [current, set] = useState(initial);
|
|
91
154
|
setCurrent = set;
|
|
155
|
+
mounted = true;
|
|
92
156
|
const { Page, layouts, params, pathname, data } = current;
|
|
93
|
-
return /* @__PURE__ */
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
157
|
+
return /* @__PURE__ */ jsx2(ErrorBoundary, { children: /* @__PURE__ */ jsx2(RouteProvider, { value: { pathname, params }, children: compose(
|
|
158
|
+
/* @__PURE__ */ jsx2(ErrorBoundary, { children: /* @__PURE__ */ jsx2(Page, { params, data }) }, pathname),
|
|
159
|
+
layouts
|
|
160
|
+
) }) });
|
|
161
|
+
}
|
|
162
|
+
function scrollToHash(hash) {
|
|
163
|
+
if (!hash) return;
|
|
164
|
+
requestAnimationFrame(() => {
|
|
165
|
+
document.getElementById(decodeURIComponent(hash.slice(1)))?.scrollIntoView();
|
|
99
166
|
});
|
|
100
167
|
}
|
|
101
|
-
async function navigate(to,
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
168
|
+
async function navigate(to, options = {}) {
|
|
169
|
+
const opts = typeof options === "boolean" ? { push: options } : options;
|
|
170
|
+
const { push = true, scroll = true, restoreY } = opts;
|
|
171
|
+
const url = sameSiteUrl(to);
|
|
172
|
+
if (!url) {
|
|
105
173
|
location.href = to;
|
|
106
174
|
return;
|
|
107
175
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
176
|
+
if (!mounted) {
|
|
177
|
+
location.href = url.href;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const id = ++latestNavigation;
|
|
181
|
+
const match = matchRoute(routes, url.pathname);
|
|
182
|
+
if (!match || !isNavigable(match.route)) {
|
|
183
|
+
location.href = url.href;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const path = url.pathname + url.search;
|
|
187
|
+
try {
|
|
188
|
+
const data = match.route.mode === "client" ? null : await takeData(path);
|
|
189
|
+
if (isRedirect(data)) {
|
|
190
|
+
location.assign(data.redirect);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (id !== latestNavigation) return;
|
|
194
|
+
const next = await resolve(url, data);
|
|
195
|
+
if (!next || id !== latestNavigation) return;
|
|
196
|
+
if (push) {
|
|
197
|
+
history.replaceState({ ...history.state, y: window.scrollY }, "");
|
|
198
|
+
history.pushState({ y: 0 }, "", path + url.hash);
|
|
122
199
|
}
|
|
200
|
+
document.title = defaultTitle;
|
|
201
|
+
setCurrent(next);
|
|
202
|
+
currentPath = path;
|
|
203
|
+
if (!push && restoreY !== void 0) window.scrollTo(0, restoreY);
|
|
204
|
+
else if (scroll) window.scrollTo(0, 0);
|
|
205
|
+
scrollToHash(url.hash);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
console.error("[kiln] navigation failed, loading the page normally", err);
|
|
208
|
+
location.href = url.href;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function onPopState() {
|
|
212
|
+
const path = location.pathname + location.search;
|
|
213
|
+
if (path === currentPath) return;
|
|
214
|
+
const y = history.state?.y ?? 0;
|
|
215
|
+
navigate(path, { push: false, restoreY: y });
|
|
216
|
+
}
|
|
217
|
+
function prefetch(to) {
|
|
218
|
+
const url = sameSiteUrl(to);
|
|
219
|
+
if (!url) return;
|
|
220
|
+
const match = matchRoute(routes, url.pathname);
|
|
221
|
+
if (!match || !isNavigable(match.route)) return;
|
|
222
|
+
void match.route.load().catch(() => {
|
|
223
|
+
});
|
|
224
|
+
const path = url.pathname + url.search;
|
|
225
|
+
if (match.route.mode !== "client" && !dataCache.has(path)) {
|
|
226
|
+
dataCache.set(path, fetchData(path));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function invalidate(path) {
|
|
230
|
+
if (path) dataCache.delete(path);
|
|
231
|
+
else dataCache.clear();
|
|
232
|
+
}
|
|
233
|
+
function leavesApp(e, href, target, download) {
|
|
234
|
+
if (e.defaultPrevented || e.button !== 0) return true;
|
|
235
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return true;
|
|
236
|
+
if (target && target !== "_self") return true;
|
|
237
|
+
if (download) return true;
|
|
238
|
+
const url = sameSiteUrl(href);
|
|
239
|
+
if (!url) return true;
|
|
240
|
+
if (url.pathname === location.pathname && url.search === location.search && url.hash) return true;
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
function Link({ href, onClick, onMouseEnter, onFocus, target, download, ...rest }) {
|
|
244
|
+
return /* @__PURE__ */ jsx2(
|
|
245
|
+
"a",
|
|
246
|
+
{
|
|
247
|
+
...rest,
|
|
248
|
+
href,
|
|
249
|
+
target,
|
|
250
|
+
download,
|
|
251
|
+
onMouseEnter: (e) => {
|
|
252
|
+
onMouseEnter?.(e);
|
|
253
|
+
prefetch(href);
|
|
254
|
+
},
|
|
255
|
+
onFocus: (e) => {
|
|
256
|
+
onFocus?.(e);
|
|
257
|
+
prefetch(href);
|
|
258
|
+
},
|
|
259
|
+
onClick: (e) => {
|
|
260
|
+
onClick?.(e);
|
|
261
|
+
if (leavesApp(e, href, target, download)) return;
|
|
262
|
+
e.preventDefault();
|
|
263
|
+
navigate(href);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
function injectedRoutes() {
|
|
269
|
+
const injected = globalThis.__KILN_ROUTES__;
|
|
270
|
+
if (!injected) {
|
|
271
|
+
throw new Error("No routes found. Pass createClient({ routes }) or run through the Kiln plugin.");
|
|
272
|
+
}
|
|
273
|
+
return injected;
|
|
274
|
+
}
|
|
275
|
+
function hydrateIslands() {
|
|
276
|
+
const registry = globalThis.__KILN_ISLANDS__ ?? {};
|
|
277
|
+
document.querySelectorAll("[data-kiln-island]").forEach((node) => {
|
|
278
|
+
const id = node.dataset.kilnIsland ?? "";
|
|
279
|
+
const load = registry[id];
|
|
280
|
+
if (!load) {
|
|
281
|
+
console.warn(`[kiln] no client code for island ${id}`);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const serverHtml = node.innerHTML;
|
|
285
|
+
load().then(({ default: Component2 }) => {
|
|
286
|
+
const props = JSON.parse(node.dataset.props || "{}");
|
|
287
|
+
hydrateRoot(node, createElement(Component2, props), {
|
|
288
|
+
onRecoverableError: (error) => reportHydrationMismatch(serverHtml, node, error)
|
|
289
|
+
});
|
|
290
|
+
}).catch((err) => console.error(`[kiln] island ${id} failed to load`, err));
|
|
123
291
|
});
|
|
124
292
|
}
|
|
125
293
|
async function createClient(options = {}) {
|
|
126
|
-
routes = options.routes ??
|
|
294
|
+
routes = options.routes ?? injectedRoutes();
|
|
127
295
|
return {
|
|
128
296
|
async start() {
|
|
129
|
-
|
|
130
|
-
|
|
297
|
+
defaultTitle = document.title;
|
|
298
|
+
history.scrollRestoration = "manual";
|
|
299
|
+
window.addEventListener("popstate", onPopState);
|
|
300
|
+
const boot = window.__KILN__ ?? { mode: "universal", pattern: "", data: null, islands: false };
|
|
301
|
+
if (boot.islands) {
|
|
302
|
+
hydrateIslands();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const url = new URL(location.href);
|
|
306
|
+
currentPath = url.pathname + url.search;
|
|
307
|
+
const initial = await resolve(url, boot.data);
|
|
308
|
+
if (!initial) throw new Error(`No client page for ${url.pathname}`);
|
|
131
309
|
const container = document.getElementById("root");
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
310
|
+
const serverHtml = container.innerHTML;
|
|
311
|
+
const tree = /* @__PURE__ */ jsx2(App, { initial });
|
|
312
|
+
if (boot.mode === "client") {
|
|
313
|
+
createRoot(container).render(tree);
|
|
314
|
+
} else {
|
|
315
|
+
hydrateRoot(container, tree, {
|
|
316
|
+
onRecoverableError: (error) => reportHydrationMismatch(serverHtml, container, error)
|
|
317
|
+
});
|
|
318
|
+
}
|
|
141
319
|
}
|
|
142
320
|
};
|
|
143
321
|
}
|
|
322
|
+
|
|
323
|
+
// packages/client/src/index.ts
|
|
324
|
+
import { usePathname, useParams, ClientOnly, useIsClient, Await, defer } from "@kiln-js/core";
|
|
144
325
|
export {
|
|
326
|
+
Await,
|
|
327
|
+
ClientOnly,
|
|
328
|
+
ErrorBoundary,
|
|
145
329
|
Link,
|
|
146
330
|
createClient,
|
|
331
|
+
defer,
|
|
332
|
+
invalidate,
|
|
147
333
|
navigate,
|
|
334
|
+
prefetch,
|
|
335
|
+
useIsClient,
|
|
148
336
|
useParams,
|
|
149
337
|
usePathname
|
|
150
338
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface ClientRoute {
|
|
|
5
5
|
mode: RenderMode;
|
|
6
6
|
load: (() => Promise<{
|
|
7
7
|
default: ComponentType<any>;
|
|
8
|
-
loader?:
|
|
8
|
+
loader?: unknown;
|
|
9
9
|
}>) | null;
|
|
10
10
|
layouts: (() => Promise<{
|
|
11
11
|
default: ComponentType<any>;
|
|
@@ -15,6 +15,7 @@ export interface KilnBoot {
|
|
|
15
15
|
mode: RenderMode;
|
|
16
16
|
pattern: string;
|
|
17
17
|
data: unknown;
|
|
18
|
+
islands?: boolean;
|
|
18
19
|
}
|
|
19
20
|
declare global {
|
|
20
21
|
interface Window {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiln-js/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@kiln-js/core": "^0.
|
|
20
|
+
"@kiln-js/core": "^0.2.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": "\u003e=18",
|