fondu 0.9.6 → 0.11.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 +11 -15
- package/dist/client/app.d.ts +40 -0
- package/dist/client/app.js +185 -0
- package/dist/client/navigation.d.ts +36 -0
- package/dist/client/navigation.js +432 -0
- package/dist/client/progress.d.ts +16 -0
- package/dist/client/progress.js +64 -0
- package/dist/client.d.ts +4 -15
- package/dist/client.js +3 -47
- package/dist/index.d.ts +3 -4
- package/dist/index.js +3 -4
- package/dist/paths.d.ts +23 -8
- package/dist/paths.js +52 -21
- package/dist/server/app.d.ts +4 -5
- package/dist/server/app.js +81 -16
- package/dist/server/document.d.ts +3 -3
- package/dist/server/document.js +27 -6
- package/dist/server/errors.d.ts +4 -2
- package/dist/server/errors.js +23 -10
- package/dist/server/flash.d.ts +4 -7
- package/dist/server/flash.js +9 -14
- package/dist/server/handler.d.ts +1 -1
- package/dist/server/handler.js +1 -1
- package/dist/server/http.d.ts +7 -5
- package/dist/server/http.js +15 -6
- package/dist/server/prerender.d.ts +1 -1
- package/dist/server/prerender.js +18 -10
- package/dist/server/protocol.d.ts +22 -35
- package/dist/server/protocol.js +67 -195
- package/dist/server/routes.d.ts +1 -1
- package/dist/server/validate.d.ts +8 -9
- package/dist/server/validate.js +23 -37
- package/dist/types.d.ts +10 -6
- package/dist/virtual.d.ts +3 -5
- package/dist/vite/hmr.d.ts +3 -3
- package/dist/vite/hmr.js +5 -5
- package/dist/vite/index.d.ts +9 -5
- package/dist/vite/index.js +174 -80
- package/dist/vite/prerender.d.ts +3 -3
- package/dist/vite/prerender.js +17 -12
- package/dist/vite/strip.d.ts +21 -0
- package/dist/vite/strip.js +306 -0
- package/dist/vite/typegen.d.ts +3 -2
- package/dist/vite/typegen.js +6 -10
- package/package.json +2 -9
- package/dist/server/props.d.ts +0 -85
- package/dist/server/props.js +0 -44
- package/dist/vite/layouts.d.ts +0 -10
- package/dist/vite/layouts.js +0 -110
- package/dist/vite/vitest-cache.d.ts +0 -19
- package/dist/vite/vitest-cache.js +0 -30
package/README.md
CHANGED
|
@@ -2,17 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://fondu.dev)
|
|
4
4
|
|
|
5
|
-
Fondu is a small meta-framework for full-stack React apps, built on Hono
|
|
6
|
-
|
|
5
|
+
Fondu is a small meta-framework for full-stack React apps, built on Hono and Vite. It runs on Bun, Cloudflare Workers
|
|
6
|
+
and Node.
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Fondu writes only what none of them does: Inertia's server side for Hono (the protocol, forms' errors and flash
|
|
13
|
-
data), file routes, typed pages and URLs, the document and error pages. Everything else is Hono's, Vite's and
|
|
14
|
-
Inertia's. [Fondu and @hono/inertia](https://fondu.dev/docs#fondu-and-honoinertia) says how it compares with Hono's
|
|
15
|
-
own middleware.
|
|
8
|
+
The files in `app/routes/` are the URLs. A page's file holds its Hono handlers, which run on the server, and its React
|
|
9
|
+
component, which the loader renders with props; the browser gets the component alone. Links and forms are plain `<a>`
|
|
10
|
+
and `<form>`: Fondu's small client swaps the pages in place, with no API to write and no client-side router to keep in
|
|
11
|
+
sync.
|
|
16
12
|
|
|
17
13
|
## Quick start
|
|
18
14
|
|
|
@@ -26,27 +22,27 @@ It asks where the app runs (Bun, Cloudflare Workers or Node; or `--target bun|cl
|
|
|
26
22
|
code checks (oxfmt, oxlint, lefthook, commitlint; or `--checks`), and writes a small app: a route, its page, the
|
|
27
23
|
error page, Tailwind CSS, editor snippets and an `AGENTS.md` for coding agents. `--yes` asks nothing more.
|
|
28
24
|
|
|
29
|
-
[demo.fondu.dev](https://demo.fondu.dev) ([apps/demo](https://github.com/fondujs/fondu/tree/main/apps/demo)) shows Fondu's
|
|
25
|
+
[demo.fondu.dev](https://demo.fondu.dev) ([apps/demo](https://github.com/fondujs/fondu/tree/main/apps/demo)) shows Fondu's features, one page each,
|
|
30
26
|
on Cloudflare Workers with D1 and Better Auth.
|
|
31
27
|
|
|
32
28
|
## Docs
|
|
33
29
|
|
|
34
30
|
**[fondu.dev](https://fondu.dev/docs)**: routes, pages and props, layouts, forms and validation, errors, type safety,
|
|
35
|
-
server-side rendering, static pages, MDX pages, internationalization, deploying, and the API.
|
|
36
|
-
([llms.txt](https://fondu.dev/llms.txt)).
|
|
31
|
+
server and client code, server-side rendering, static pages, MDX pages, internationalization, deploying, and the API.
|
|
32
|
+
Every page is also Markdown for coding agents ([llms.txt](https://fondu.dev/llms.txt)).
|
|
37
33
|
|
|
38
34
|
## Contributing
|
|
39
35
|
|
|
40
36
|
```
|
|
41
37
|
packages/fondu the package
|
|
42
38
|
packages/create-fondu `bun create fondu`
|
|
43
|
-
apps/demo Fondu's
|
|
39
|
+
apps/demo Fondu's features, one page each — Workers, D1, Better Auth, coss ui
|
|
44
40
|
apps/site fondu.dev: the docs, MDX and static pages on Workers
|
|
45
41
|
```
|
|
46
42
|
|
|
47
43
|
```bash
|
|
48
44
|
bun install # also builds packages/fondu (the examples use the built package, like a real app)
|
|
49
|
-
bun run test # the
|
|
45
|
+
bun run test # the package: a small app with app.request(), the routes, the client
|
|
50
46
|
bun run typecheck # the package, the demo (with apps/demo/e2e/types.ts) and the site
|
|
51
47
|
bun run check # format (oxfmt), lint (oxlint), typecheck
|
|
52
48
|
bun run dev # rebuild packages/fondu on change
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import type { Flash, Shared } from '../types.js';
|
|
3
|
+
import { type Page } from './navigation.js';
|
|
4
|
+
import { type ProgressOptions } from './progress.js';
|
|
5
|
+
type Loader = () => Promise<unknown>;
|
|
6
|
+
/**
|
|
7
|
+
* @internal the app's entry (the Vite plugin writes it), in the browser and for the server render: `import.meta.glob`
|
|
8
|
+
* of the pages and layouts of `app/routes/`, and `fondu({ prerender, progress })`. In the browser it starts the app:
|
|
9
|
+
* it hydrates the server's page and starts navigating.
|
|
10
|
+
*/
|
|
11
|
+
export declare function __entry(pageModules: Record<string, Loader>, layouts: Record<string, Loader>, settings: {
|
|
12
|
+
prerender: boolean;
|
|
13
|
+
progress: ProgressOptions | false;
|
|
14
|
+
}): void;
|
|
15
|
+
/**
|
|
16
|
+
* @internal The page a URL most likely is, by its route file's patterns (static segments first): its modules load with its
|
|
17
|
+
* data. Only a guess (a param's listed values are the server's), so the page shown is the one the server says.
|
|
18
|
+
*/
|
|
19
|
+
export declare function pageAt(pathname: string): string | undefined;
|
|
20
|
+
/** @internal the server render of a page: the head tags its components render (`<title>`…), and `<div id="app">` */
|
|
21
|
+
export declare function renderPage(page: Page, renderToString: (element: ReactElement) => string): Promise<{
|
|
22
|
+
head: string;
|
|
23
|
+
body: string;
|
|
24
|
+
}>;
|
|
25
|
+
/** a form's field errors, as a page gets them (its `errors` prop): field name → message */
|
|
26
|
+
export type Errors = Record<string, string>;
|
|
27
|
+
/** the page on screen: its props (the shared ones, `errors`), its URL, its flash data */
|
|
28
|
+
export declare function usePage<P = Shared>(): {
|
|
29
|
+
component: string;
|
|
30
|
+
props: P & {
|
|
31
|
+
errors: Errors;
|
|
32
|
+
};
|
|
33
|
+
url: string;
|
|
34
|
+
flash: Partial<Flash>;
|
|
35
|
+
};
|
|
36
|
+
/** true while a navigation or a form's submission is on its way */
|
|
37
|
+
export declare function useNavigating(): boolean;
|
|
38
|
+
/** this page's props asked for again: the page keeps its state and the scroll position */
|
|
39
|
+
export declare function reload(): void;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Component, createContext, createElement, useContext, useLayoutEffect, useState, useSyncExternalStore, } from 'react';
|
|
2
|
+
import { flushSync } from 'react-dom';
|
|
3
|
+
import { createRoot, hydrateRoot } from 'react-dom/client';
|
|
4
|
+
import { layoutsOf, pageId, routePatterns } from '../paths.js';
|
|
5
|
+
import { again, startNavigation } from './navigation.js';
|
|
6
|
+
import { progressBar } from './progress.js';
|
|
7
|
+
const ROUTES = '/app/routes/';
|
|
8
|
+
const ERROR = '_error';
|
|
9
|
+
// set by the app's entry (the Vite plugin's): the pages and layouts of app/routes/, and the build's settings
|
|
10
|
+
let pages = new Map();
|
|
11
|
+
let layoutModules = {};
|
|
12
|
+
/**
|
|
13
|
+
* @internal the app's entry (the Vite plugin writes it), in the browser and for the server render: `import.meta.glob`
|
|
14
|
+
* of the pages and layouts of `app/routes/`, and `fondu({ prerender, progress })`. In the browser it starts the app:
|
|
15
|
+
* it hydrates the server's page and starts navigating.
|
|
16
|
+
*/
|
|
17
|
+
export function __entry(pageModules, layouts, settings) {
|
|
18
|
+
pages = new Map(Object.entries(pageModules).map(([key, loader]) => [pageId(key.slice(ROUTES.length)), loader]));
|
|
19
|
+
layoutModules = layouts;
|
|
20
|
+
if (typeof document !== 'undefined')
|
|
21
|
+
start(settings);
|
|
22
|
+
}
|
|
23
|
+
const defaultOf = async (loader) => (await loader()).default;
|
|
24
|
+
/** a page's component, and the `_layout.tsx` files of its directories it's drawn in, outermost first */
|
|
25
|
+
async function load(component) {
|
|
26
|
+
const page = pages.get(component);
|
|
27
|
+
if (!page)
|
|
28
|
+
throw new Error(`fondu: there is no page "${component}" in app/routes/`);
|
|
29
|
+
const chain = layoutsOf(component)
|
|
30
|
+
.map((dir) => ['tsx', 'jsx']
|
|
31
|
+
.map((ext) => `${ROUTES}${dir ? `${dir}/` : ''}_layout.${ext}`)
|
|
32
|
+
.find((k) => k in layoutModules))
|
|
33
|
+
.filter((k) => k !== undefined);
|
|
34
|
+
const [Component, ...layouts] = await Promise.all([
|
|
35
|
+
defaultOf(page),
|
|
36
|
+
...chain.map((k) => defaultOf(layoutModules[k])),
|
|
37
|
+
]);
|
|
38
|
+
return { Component, layouts };
|
|
39
|
+
}
|
|
40
|
+
// ---- the page on screen, for usePage()
|
|
41
|
+
const PageContext = createContext(null);
|
|
42
|
+
/** the browser's: called once the app is on the screen, with what puts another page there */
|
|
43
|
+
let mounted;
|
|
44
|
+
function App({ initial }) {
|
|
45
|
+
const [{ page, view, mounts }, set] = useState(initial);
|
|
46
|
+
useLayoutEffect(() => mounted?.(set), [set]);
|
|
47
|
+
const { Component, layouts } = view;
|
|
48
|
+
// the page mounted anew by each navigation, its layouts kept (the same components: React keeps them)
|
|
49
|
+
// a page that throws in the browser: the error page in its place, in its layouts; a layout that throws: the error
|
|
50
|
+
// page alone. Until the next page
|
|
51
|
+
const content = layouts.reduceRight((children, L) => createElement(L, null, children), createElement(Failed, { key: mounts, page }, createElement(Component, { ...page.props })));
|
|
52
|
+
return createElement(PageContext.Provider, { value: page }, createElement(Failed, { page }, content));
|
|
53
|
+
}
|
|
54
|
+
/** the app's error page (routes/_error.tsx), loaded once the app starts: a failed render can't wait for it */
|
|
55
|
+
let ErrorPage;
|
|
56
|
+
/** what failed to render, until another page (the layouts it is around are kept: no key) */
|
|
57
|
+
class Failed extends Component {
|
|
58
|
+
state = { failed: false, page: this.props.page };
|
|
59
|
+
static getDerivedStateFromError() {
|
|
60
|
+
return { failed: true };
|
|
61
|
+
}
|
|
62
|
+
static getDerivedStateFromProps(props, state) {
|
|
63
|
+
return props.page === state.page ? null : { failed: false, page: props.page };
|
|
64
|
+
}
|
|
65
|
+
render() {
|
|
66
|
+
if (!this.state.failed)
|
|
67
|
+
return this.props.children;
|
|
68
|
+
return ErrorPage ? createElement(ErrorPage, { status: 500 }) : 'Something went wrong';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @internal The page a URL most likely is, by its route file's patterns (static segments first): its modules load with its
|
|
73
|
+
* data. Only a guess (a param's listed values are the server's), so the page shown is the one the server says.
|
|
74
|
+
*/
|
|
75
|
+
export function pageAt(pathname) {
|
|
76
|
+
const parts = pathname.split('/').filter(Boolean);
|
|
77
|
+
let best;
|
|
78
|
+
for (const id of pages.keys()) {
|
|
79
|
+
if (id.split('/').some((s) => s.startsWith('_')))
|
|
80
|
+
continue;
|
|
81
|
+
for (const pattern of routePatterns(id)) {
|
|
82
|
+
const segments = pattern.split('/').filter(Boolean);
|
|
83
|
+
let score = 0;
|
|
84
|
+
let i = 0;
|
|
85
|
+
for (const s of segments) {
|
|
86
|
+
const part = parts[i];
|
|
87
|
+
if (part === undefined)
|
|
88
|
+
score = -1;
|
|
89
|
+
else if (s.endsWith('{.+}'))
|
|
90
|
+
score += 1; // a catch-all: the rest
|
|
91
|
+
else if (s.startsWith(':'))
|
|
92
|
+
score += 2;
|
|
93
|
+
else
|
|
94
|
+
score += s === part ? 3 : -Infinity;
|
|
95
|
+
i = s.endsWith('{.+}') ? parts.length : i + 1;
|
|
96
|
+
if (score < 0)
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (score >= 0 && i === parts.length && (!best || score > best.score))
|
|
100
|
+
best = { id, score };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return best?.id;
|
|
104
|
+
}
|
|
105
|
+
// ---- pending navigations, for useNavigating()
|
|
106
|
+
let navigating = false;
|
|
107
|
+
let submitting = false;
|
|
108
|
+
const listeners = new Set();
|
|
109
|
+
const subscribe = (listener) => {
|
|
110
|
+
listeners.add(listener);
|
|
111
|
+
return () => listeners.delete(listener);
|
|
112
|
+
};
|
|
113
|
+
function setNavigating(value, form) {
|
|
114
|
+
navigating = value;
|
|
115
|
+
submitting = form;
|
|
116
|
+
for (const l of listeners)
|
|
117
|
+
l();
|
|
118
|
+
}
|
|
119
|
+
// ---- the app
|
|
120
|
+
/** the browser's app: the server's page hydrated, then navigations */
|
|
121
|
+
function start(settings) {
|
|
122
|
+
const progress = settings.progress;
|
|
123
|
+
if (progress)
|
|
124
|
+
subscribe(progressBar(progress, () => navigating && (progress.forms !== false || !submitting)));
|
|
125
|
+
const data = document.querySelector('script[data-page="app"]')?.textContent;
|
|
126
|
+
const root = document.getElementById('app');
|
|
127
|
+
if (!data || !root)
|
|
128
|
+
throw new Error('fondu: the document has no page (script[data-page="app"], #app)');
|
|
129
|
+
const initial = JSON.parse(data);
|
|
130
|
+
void pages
|
|
131
|
+
.get(ERROR)?.()
|
|
132
|
+
.then((m) => (ErrorPage = m.default));
|
|
133
|
+
void load(initial.component).then((view) => {
|
|
134
|
+
// dev: the stylesheet the server gave the page until its layouts brought their CSS (they have now)
|
|
135
|
+
document.querySelector('[data-fondu-dev-css]')?.remove();
|
|
136
|
+
const app = createElement(App, { initial: { page: initial, view, mounts: 0 } });
|
|
137
|
+
// navigations start once the page is on the screen (hydrated)
|
|
138
|
+
mounted = (set) => {
|
|
139
|
+
mounted = undefined;
|
|
140
|
+
startNavigation({
|
|
141
|
+
initial,
|
|
142
|
+
prerender: settings.prerender,
|
|
143
|
+
pending: setNavigating,
|
|
144
|
+
preload(pathname) {
|
|
145
|
+
const page = pageAt(pathname);
|
|
146
|
+
if (page)
|
|
147
|
+
load(page).catch(() => { });
|
|
148
|
+
},
|
|
149
|
+
async show(page, fresh, stale) {
|
|
150
|
+
const view = await load(page.component);
|
|
151
|
+
if (stale())
|
|
152
|
+
return;
|
|
153
|
+
flushSync(() => set((now) => ({ page, view, mounts: now.mounts + Number(fresh) })));
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
if (root.hasChildNodes())
|
|
158
|
+
hydrateRoot(root, app);
|
|
159
|
+
else
|
|
160
|
+
createRoot(root).render(app);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/** @internal the server render of a page: the head tags its components render (`<title>`…), and `<div id="app">` */
|
|
164
|
+
export async function renderPage(page, renderToString) {
|
|
165
|
+
const view = await load(page.component);
|
|
166
|
+
const html = renderToString(createElement('div', { id: 'app' }, createElement(App, { initial: { page, view, mounts: 0 } })));
|
|
167
|
+
// React puts the tags it hoists (`<title>`, `<meta>`, `<link>`) before the root's markup
|
|
168
|
+
const at = html.indexOf('<div id="app"');
|
|
169
|
+
return { head: html.slice(0, at), body: html.slice(at) };
|
|
170
|
+
}
|
|
171
|
+
/** the page on screen: its props (the shared ones, `errors`), its URL, its flash data */
|
|
172
|
+
export function usePage() {
|
|
173
|
+
const page = useContext(PageContext);
|
|
174
|
+
if (!page)
|
|
175
|
+
throw new Error('fondu: usePage() is for components of a page');
|
|
176
|
+
return page;
|
|
177
|
+
}
|
|
178
|
+
/** true while a navigation or a form's submission is on its way */
|
|
179
|
+
export function useNavigating() {
|
|
180
|
+
return useSyncExternalStore(subscribe, () => navigating, () => false);
|
|
181
|
+
}
|
|
182
|
+
/** this page's props asked for again: the page keeps its state and the scroll position */
|
|
183
|
+
export function reload() {
|
|
184
|
+
again();
|
|
185
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** what the server sends for a page: its id (its route file, `users/[id]`), its props, its URL, the build's version */
|
|
2
|
+
export interface Page {
|
|
3
|
+
component: string;
|
|
4
|
+
props: Record<string, unknown>;
|
|
5
|
+
url: string;
|
|
6
|
+
version: string | null;
|
|
7
|
+
flash: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface Options {
|
|
10
|
+
initial: Page;
|
|
11
|
+
/**
|
|
12
|
+
* puts the page on the screen (in the DOM once it resolves); `fresh`: mounted anew, else it keeps its state (a form
|
|
13
|
+
* back with its errors, reload()); not once `stale()` (superseded while its modules loaded)
|
|
14
|
+
*/
|
|
15
|
+
show: (page: Page, fresh: boolean, stale: () => boolean) => Promise<void>;
|
|
16
|
+
/** `fondu({ prerender: true })`: GET navigations read `/_fondu/pages/<path>.json` */
|
|
17
|
+
prerender: boolean;
|
|
18
|
+
/** a navigation started (true; `form`: a form's submission) or ended (false) */
|
|
19
|
+
pending: (pending: boolean, form: boolean) => void;
|
|
20
|
+
/** a GET navigation to `pathname` started: its page's modules may load with its data */
|
|
21
|
+
preload: (pathname: string) => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Same-origin links and forms, Back and Forward, through the History API (as Void's client, MIT): the page fetched
|
|
25
|
+
* as JSON, its URL in the history, the page shown in place, then the scroll position (Fondu's own, per history entry:
|
|
26
|
+
* the browsers' differ). What isn't for it (another site, a new tab, a download, a modified click) is the browser's.
|
|
27
|
+
*/
|
|
28
|
+
export declare function startNavigation({ initial, show, prerender, pending, preload }: Options): void;
|
|
29
|
+
/**
|
|
30
|
+
* `href` loaded as a document. Every other navigation is the client's, links and forms: this is for what a page
|
|
31
|
+
* can't change in place (`<html lang>`: another language).
|
|
32
|
+
*/
|
|
33
|
+
export declare function loadDocument(href: string): void;
|
|
34
|
+
/** the page asked for again, its state and the scroll position kept */
|
|
35
|
+
export declare function again(): void;
|
|
36
|
+
export {};
|