fondu 0.10.0 → 0.11.1

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
@@ -5,9 +5,10 @@
5
5
  Fondu is a small meta-framework for full-stack React apps, built on Hono and Vite. It runs on Bun, Cloudflare Workers
6
6
  and Node.
7
7
 
8
- The files in `app/routes/` are the URLs: Hono handlers that run on the server, and for a page, the React component its
9
- loader renders with props, in the same file (the browser gets the component alone). In the browser, links and forms are plain `<a>` and `<form>`: Fondu's small client, on the Navigation API, swaps
10
- the pages in place, without an API to write or a client-side router to keep in sync.
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.
11
12
 
12
13
  ## Quick start
13
14
 
@@ -27,8 +28,8 @@ on Cloudflare Workers with D1 and Better Auth.
27
28
  ## Docs
28
29
 
29
30
  **[fondu.dev](https://fondu.dev/docs)**: routes, pages and props, layouts, forms and validation, errors, type safety,
30
- server-side rendering, static pages, MDX pages, internationalization, deploying, and the API. Every page is also Markdown for coding agents
31
- ([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)).
32
33
 
33
34
  ## Contributing
34
35
 
@@ -37,12 +38,13 @@ packages/fondu the package
37
38
  packages/create-fondu `bun create fondu`
38
39
  apps/demo Fondu's features, one page each — Workers, D1, Better Auth, coss ui
39
40
  apps/site fondu.dev: the docs, MDX and static pages on Workers
41
+ apps/conformance fondu/client against the browsers without JavaScript (Playwright: Chromium, Firefox, WebKit)
40
42
  ```
41
43
 
42
44
  ```bash
43
45
  bun install # also builds packages/fondu (the examples use the built package, like a real app)
44
- bun run test # the protocol, on a small app with app.request()
45
- bun run typecheck # the package, the demo (with apps/demo/e2e/types.ts) and the site
46
+ bun run test # the package: a small app with app.request(), the routes, the client
47
+ bun run typecheck # the package, the demo (with apps/demo/e2e/types.ts), the site and apps/conformance
46
48
  bun run check # format (oxfmt), lint (oxlint), typecheck
47
49
  bun run dev # rebuild packages/fondu on change
48
50
  ```
@@ -14,7 +14,8 @@ export declare function __entry(pageModules: Record<string, Loader>, layouts: Re
14
14
  }): void;
15
15
  /**
16
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.
17
+ * data, and a link to a URL that is none (a file, an HTTP route) is left to the browser. Only a guess (a param's
18
+ * listed values are the server's), so the page shown is the one the server says.
18
19
  */
19
20
  export declare function pageAt(pathname: string): string | undefined;
20
21
  /** @internal the server render of a page: the head tags its components render (`<title>`…), and `<div id="app">` */
@@ -1,10 +1,11 @@
1
- import { createContext, createElement, useContext, useLayoutEffect, useState, useSyncExternalStore, } from 'react';
1
+ import { Component, createContext, createElement, useContext, useLayoutEffect, useState, useSyncExternalStore, } from 'react';
2
2
  import { flushSync } from 'react-dom';
3
3
  import { createRoot, hydrateRoot } from 'react-dom/client';
4
4
  import { layoutsOf, pageId, routePatterns } from '../paths.js';
5
5
  import { again, startNavigation } from './navigation.js';
6
6
  import { progressBar } from './progress.js';
7
7
  const ROUTES = '/app/routes/';
8
+ const ERROR = '_error';
8
9
  // set by the app's entry (the Vite plugin's): the pages and layouts of app/routes/, and the build's settings
9
10
  let pages = new Map();
10
11
  let layoutModules = {};
@@ -45,12 +46,32 @@ function App({ initial }) {
45
46
  useLayoutEffect(() => mounted?.(set), [set]);
46
47
  const { Component, layouts } = view;
47
48
  // the page mounted anew by each navigation, its layouts kept (the same components: React keeps them)
48
- const content = layouts.reduceRight((children, L) => createElement(L, null, children), createElement(Component, { key: mounts, ...page.props }));
49
- return createElement(PageContext.Provider, { value: page }, content);
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
+ }
50
70
  }
51
71
  /**
52
72
  * @internal The page a URL most likely is, by its route file's patterns (static segments first): its modules load with its
53
- * data. Only a guess (a param's listed values are the server's), so the page shown is the one the server says.
73
+ * data, and a link to a URL that is none (a file, an HTTP route) is left to the browser. Only a guess (a param's
74
+ * listed values are the server's), so the page shown is the one the server says.
54
75
  */
55
76
  export function pageAt(pathname) {
56
77
  const parts = pathname.split('/').filter(Boolean);
@@ -107,10 +128,14 @@ function start(settings) {
107
128
  if (!data || !root)
108
129
  throw new Error('fondu: the document has no page (script[data-page="app"], #app)');
109
130
  const initial = JSON.parse(data);
131
+ void pages
132
+ .get(ERROR)?.()
133
+ .then((m) => (ErrorPage = m.default));
110
134
  void load(initial.component).then((view) => {
111
135
  // dev: the stylesheet the server gave the page until its layouts brought their CSS (they have now)
112
136
  document.querySelector('[data-fondu-dev-css]')?.remove();
113
137
  const app = createElement(App, { initial: { page: initial, view, mounts: 0 } });
138
+ const hydrated = root.hasChildNodes(); // rendered on the server
114
139
  // navigations start once the page is on the screen (hydrated)
115
140
  mounted = (set) => {
116
141
  mounted = undefined;
@@ -123,6 +148,8 @@ function start(settings) {
123
148
  if (page)
124
149
  load(page).catch(() => { });
125
150
  },
151
+ isPage: (pathname) => pageAt(pathname) !== undefined,
152
+ hydrated,
126
153
  async show(page, fresh, stale) {
127
154
  const view = await load(page.component);
128
155
  if (stale())
@@ -131,7 +158,7 @@ function start(settings) {
131
158
  },
132
159
  });
133
160
  };
134
- if (root.hasChildNodes())
161
+ if (hydrated)
135
162
  hydrateRoot(root, app);
136
163
  else
137
164
  createRoot(root).render(app);
@@ -19,15 +19,20 @@ interface Options {
19
19
  pending: (pending: boolean, form: boolean) => void;
20
20
  /** a GET navigation to `pathname` started: its page's modules may load with its data */
21
21
  preload: (pathname: string) => void;
22
+ /** a URL path is one of the app's pages (by the route files' patterns): its links are the client's */
23
+ isPage: (pathname: string) => boolean;
24
+ /** the page was rendered on the server (hydrated), not in the browser */
25
+ hydrated: boolean;
22
26
  }
23
27
  /**
24
- * Same-origin links, GET and POST forms, Back and Forward, through the Navigation API: the page fetched as JSON and
25
- * shown in place. Without the Navigation API, links and forms load documents.
28
+ * Same-origin links and forms, Back and Forward, through the History API (as Void's client, MIT): the page fetched
29
+ * as JSON, its URL in the history, the page shown in place, then the scroll position (Fondu's own, per history entry:
30
+ * the browsers' differ). What isn't for it (another site, a new tab, a download, a modified click) is the browser's.
26
31
  */
27
- export declare function startNavigation({ initial, show, prerender, pending, preload }: Options): void;
32
+ export declare function startNavigation({ initial, show, prerender, pending, preload, isPage, hydrated, }: Options): void;
28
33
  /**
29
- * `href` loaded as a document. Every other navigation is the client's, `location.assign()` and `location.href = …`
30
- * too: this is for what a page can't change in place (`<html lang>`: another language).
34
+ * `href` loaded as a document. Every other navigation is the client's, links and forms: this is for what a page
35
+ * can't change in place (`<html lang>`: another language).
31
36
  */
32
37
  export declare function loadDocument(href: string): void;
33
38
  /** the page asked for again, its state and the scroll position kept */