fondu 0.11.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
@@ -38,12 +38,13 @@ packages/fondu the package
38
38
  packages/create-fondu `bun create fondu`
39
39
  apps/demo Fondu's features, one page each — Workers, D1, Better Auth, coss ui
40
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)
41
42
  ```
42
43
 
43
44
  ```bash
44
45
  bun install # also builds packages/fondu (the examples use the built package, like a real app)
45
46
  bun run test # the package: a small app with app.request(), the routes, the client
46
- bun run typecheck # the package, the demo (with apps/demo/e2e/types.ts) and the site
47
+ bun run typecheck # the package, the demo (with apps/demo/e2e/types.ts), the site and apps/conformance
47
48
  bun run check # format (oxfmt), lint (oxlint), typecheck
48
49
  bun run dev # rebuild packages/fondu on change
49
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">` */
@@ -70,7 +70,8 @@ class Failed extends Component {
70
70
  }
71
71
  /**
72
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.
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.
74
75
  */
75
76
  export function pageAt(pathname) {
76
77
  const parts = pathname.split('/').filter(Boolean);
@@ -134,6 +135,7 @@ function start(settings) {
134
135
  // dev: the stylesheet the server gave the page until its layouts brought their CSS (they have now)
135
136
  document.querySelector('[data-fondu-dev-css]')?.remove();
136
137
  const app = createElement(App, { initial: { page: initial, view, mounts: 0 } });
138
+ const hydrated = root.hasChildNodes(); // rendered on the server
137
139
  // navigations start once the page is on the screen (hydrated)
138
140
  mounted = (set) => {
139
141
  mounted = undefined;
@@ -146,6 +148,8 @@ function start(settings) {
146
148
  if (page)
147
149
  load(page).catch(() => { });
148
150
  },
151
+ isPage: (pathname) => pageAt(pathname) !== undefined,
152
+ hydrated,
149
153
  async show(page, fresh, stale) {
150
154
  const view = await load(page.component);
151
155
  if (stale())
@@ -154,7 +158,7 @@ function start(settings) {
154
158
  },
155
159
  });
156
160
  };
157
- if (root.hasChildNodes())
161
+ if (hydrated)
158
162
  hydrateRoot(root, app);
159
163
  else
160
164
  createRoot(root).render(app);
@@ -19,13 +19,17 @@ 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
28
  * Same-origin links and forms, Back and Forward, through the History API (as Void's client, MIT): the page fetched
25
29
  * as JSON, its URL in the history, the page shown in place, then the scroll position (Fondu's own, per history entry:
26
30
  * the browsers' differ). What isn't for it (another site, a new tab, a download, a modified click) is the browser's.
27
31
  */
28
- 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;
29
33
  /**
30
34
  * `href` loaded as a document. Every other navigation is the client's, links and forms: this is for what a page
31
35
  * can't change in place (`<html lang>`: another language).
@@ -5,7 +5,7 @@ let visit;
5
5
  * as JSON, its URL in the history, the page shown in place, then the scroll position (Fondu's own, per history entry:
6
6
  * the browsers' differ). What isn't for it (another site, a new tab, a download, a modified click) is the browser's.
7
7
  */
8
- export function startNavigation({ initial, show, prerender, pending, preload }) {
8
+ export function startNavigation({ initial, show, prerender, pending, preload, isPage, hydrated, }) {
9
9
  const scroll = scrollPositions();
10
10
  let version = initial.version;
11
11
  let current;
@@ -189,6 +189,9 @@ export function startNavigation({ initial, show, prerender, pending, preload })
189
189
  scroll.save();
190
190
  return;
191
191
  }
192
+ // a URL that is none of the app's pages (a file, /llms.txt; an HTTP route; nothing at all): the browser's
193
+ if (!isPage(url.pathname))
194
+ return;
192
195
  event.preventDefault();
193
196
  visit(url.href, { history: url.href === location.href ? 'replace' : 'push' });
194
197
  });
@@ -236,27 +239,21 @@ export function startNavigation({ initial, show, prerender, pending, preload })
236
239
  kept: kept.get(popped.key ?? ''),
237
240
  });
238
241
  });
239
- // leaving the document (a reload, another site): its position, and the browser's restoration back on for this
240
- // entry, so a reload or Back to it is scrolled at first paint, as any document
241
- window.addEventListener('pagehide', () => {
242
- scroll.save();
243
- history.scrollRestoration = 'auto';
244
- });
245
- window.addEventListener('pageshow', (event) => {
246
- if (event.persisted)
247
- history.scrollRestoration = 'manual'; // back from the back/forward cache
248
- });
242
+ // leaving the document (a reload, another site): its position, put back by the document when it is loaded again.
243
+ // The browser's own restoration stays off: engines restore at times of their own, or not (WebKit, an entry once
244
+ // manual; Firefox, a #fragment), and over the page's own (Firefox, at load, a page rendered in the browser at 0)
245
+ window.addEventListener('pagehide', scroll.save);
249
246
  // the browser's own #fragment navigations: the entry gets its key
250
247
  window.addEventListener('hashchange', scroll.commit);
251
- // an entry with a position (a reload, Back from another site): that position, the #fragment's target or not, as
252
- // the browser restores it; otherwise, a first visit, the #fragment's target (rendered by now)
253
- const { position, restored } = scroll.start();
254
- if (position) {
255
- if (!restored)
256
- window.scrollTo(position.x, position.y);
248
+ // a page rendered on the server is where it belongs already, before its first paint: at its entry's position (its
249
+ // document's script: a reload, Back from another site), else at its #fragment (the browser). One rendered here has
250
+ // its content only now: its entry's position, or its #fragment's target
251
+ const position = scroll.start();
252
+ if (!hydrated) {
253
+ restore(position, location.hash, true);
254
+ if (position)
255
+ holdUntilLoaded(position);
257
256
  }
258
- else
259
- restore(null, location.hash, true);
260
257
  keep(initial);
261
258
  }
262
259
  /**
@@ -327,16 +324,13 @@ function scrollPositions() {
327
324
  /** the key of the entry on the screen */
328
325
  key: () => rendered,
329
326
  /**
330
- * The document's entry: its key; where it was scrolled, if it was (an entry this client pushed, loaded again: a
331
- * reload, Back from another site past it), and whether the browser or the document's script put it back already
327
+ * The document's entry: its key, and where it was scrolled, if it was (an entry this client pushed, loaded again:
328
+ * a reload, Back from another site past it)
332
329
  */
333
330
  start() {
334
- // restored already, by the browser or the document's own script: the page is scrolled (one rendered in the
335
- // browser was empty for them, it has its content only now)
336
- const restored = scrollX !== 0 || scrollY !== 0;
337
331
  history.scrollRestoration = 'manual';
338
332
  commit();
339
- return { position: read(rendered), restored };
333
+ return read(rendered);
340
334
  },
341
335
  /** the page on the screen, at `url`: a new entry or this one */
342
336
  write(url, replace) {
@@ -369,6 +363,20 @@ function restore(position, hash, first = false) {
369
363
  if (!first)
370
364
  window.scrollTo(0, 0);
371
365
  }
366
+ /**
367
+ * A position kept until the page has loaded: browsers scroll on their own until then (Firefox, a page rendered in
368
+ * the browser back to the top), as the document's script does it for a page rendered on the server. Not once the
369
+ * visitor has scrolled, typed or clicked.
370
+ */
371
+ function holdUntilLoaded(position) {
372
+ if (document.readyState === 'complete')
373
+ return;
374
+ let held = true;
375
+ const off = () => (held = false);
376
+ for (const type of ['wheel', 'touchstart', 'keydown', 'mousedown'])
377
+ addEventListener(type, off, { once: true, passive: true });
378
+ addEventListener('load', () => setTimeout(() => held && window.scrollTo(position.x, position.y)));
379
+ }
372
380
  /** the element a #fragment names: decoded, or as it is (`#100%` doesn't decode); null for none */
373
381
  function fragmentTarget(fragment) {
374
382
  try {
@@ -18,10 +18,11 @@ export const withoutSsr = () => async (c, next) => {
18
18
  const pageScript = (page) => `<script data-page="app" type="application/json">${JSON.stringify(page).replace(/\//g, '\\/').replace(/</g, '\\u003c')}</script>`;
19
19
  /**
20
20
  * A reload, Back to a page of another site: this entry's position, where fondu/client left it, before the first
21
- * paint (the stylesheets are in: the page is laid out). The browser may not restore it itself (Safari: the client's
22
- * own restoration, off until the page is left, is still off for it), and the client would only once it runs.
21
+ * paint (the stylesheets are in: the page is laid out), and again once the page has loaded: browsers scroll on
22
+ * their own until then (to the #fragment, WebKit and Firefox), fondu/client keeping their restoration off. Not once
23
+ * the visitor has scrolled, typed or clicked: their own scrolling wins.
23
24
  */
24
- const restoreScroll = (nonce) => `<script${nonce ? ` nonce="${nonce}"` : ''}>try{var k=history.state&&history.state[${JSON.stringify(SCROLL_KEY)}],p=k&&JSON.parse(sessionStorage.getItem(${JSON.stringify(SCROLL_STORAGE)}+k));p&&!scrollY&&scrollTo(+p.x||0,+p.y||0)}catch(e){}</script>`;
25
+ const restoreScroll = (nonce) => `<script${nonce ? ` nonce="${nonce}"` : ''}>try{var k=history.state&&history.state[${JSON.stringify(SCROLL_KEY)}],p=k&&JSON.parse(sessionStorage.getItem(${JSON.stringify(SCROLL_STORAGE)}+k));if(p){var go=function(){scrollTo(+p.x||0,+p.y||0)},off=function(){go=0};go();["wheel","touchstart","keydown","mousedown"].forEach(function(t){addEventListener(t,off,{once:!0,passive:!0})});addEventListener("load",function(){setTimeout(function(){go&&go()})})}}catch(e){}</script>`;
25
26
  // where the page goes, in the document a template returns: replaced before the page is in, so a page's content is
26
27
  // never read as one
27
28
  const HEAD = 'fondu:document-head';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "A small meta-framework for full-stack React apps, built on Hono and Vite. It runs on Bun, Cloudflare Workers and Node.",
5
5
  "keywords": [
6
6
  "bun",
@@ -54,6 +54,7 @@
54
54
  "@types/node": "26.6.5",
55
55
  "@types/react": "19.3.0",
56
56
  "@types/react-dom": "19.3.0",
57
+ "fast-check": "4.3.0",
57
58
  "hono": "4.13.13",
58
59
  "typescript": "7.0.2",
59
60
  "vite": "8.3.4",