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.
Files changed (51) hide show
  1. package/README.md +11 -15
  2. package/dist/client/app.d.ts +40 -0
  3. package/dist/client/app.js +185 -0
  4. package/dist/client/navigation.d.ts +36 -0
  5. package/dist/client/navigation.js +432 -0
  6. package/dist/client/progress.d.ts +16 -0
  7. package/dist/client/progress.js +64 -0
  8. package/dist/client.d.ts +4 -15
  9. package/dist/client.js +3 -47
  10. package/dist/index.d.ts +3 -4
  11. package/dist/index.js +3 -4
  12. package/dist/paths.d.ts +23 -8
  13. package/dist/paths.js +52 -21
  14. package/dist/server/app.d.ts +4 -5
  15. package/dist/server/app.js +81 -16
  16. package/dist/server/document.d.ts +3 -3
  17. package/dist/server/document.js +27 -6
  18. package/dist/server/errors.d.ts +4 -2
  19. package/dist/server/errors.js +23 -10
  20. package/dist/server/flash.d.ts +4 -7
  21. package/dist/server/flash.js +9 -14
  22. package/dist/server/handler.d.ts +1 -1
  23. package/dist/server/handler.js +1 -1
  24. package/dist/server/http.d.ts +7 -5
  25. package/dist/server/http.js +15 -6
  26. package/dist/server/prerender.d.ts +1 -1
  27. package/dist/server/prerender.js +18 -10
  28. package/dist/server/protocol.d.ts +22 -35
  29. package/dist/server/protocol.js +67 -195
  30. package/dist/server/routes.d.ts +1 -1
  31. package/dist/server/validate.d.ts +8 -9
  32. package/dist/server/validate.js +23 -37
  33. package/dist/types.d.ts +10 -6
  34. package/dist/virtual.d.ts +3 -5
  35. package/dist/vite/hmr.d.ts +3 -3
  36. package/dist/vite/hmr.js +5 -5
  37. package/dist/vite/index.d.ts +9 -5
  38. package/dist/vite/index.js +174 -80
  39. package/dist/vite/prerender.d.ts +3 -3
  40. package/dist/vite/prerender.js +17 -12
  41. package/dist/vite/strip.d.ts +21 -0
  42. package/dist/vite/strip.js +306 -0
  43. package/dist/vite/typegen.d.ts +3 -2
  44. package/dist/vite/typegen.js +6 -10
  45. package/package.json +2 -9
  46. package/dist/server/props.d.ts +0 -85
  47. package/dist/server/props.js +0 -44
  48. package/dist/vite/layouts.d.ts +0 -10
  49. package/dist/vite/layouts.js +0 -110
  50. package/dist/vite/vitest-cache.d.ts +0 -19
  51. package/dist/vite/vitest-cache.js +0 -30
@@ -0,0 +1,432 @@
1
+ import { pageDataPath, SCROLL_KEY, SCROLL_STORAGE } from '../paths.js';
2
+ let visit;
3
+ /**
4
+ * Same-origin links and forms, Back and Forward, through the History API (as Void's client, MIT): the page fetched
5
+ * as JSON, its URL in the history, the page shown in place, then the scroll position (Fondu's own, per history entry:
6
+ * the browsers' differ). What isn't for it (another site, a new tab, a download, a modified click) is the browser's.
7
+ */
8
+ export function startNavigation({ initial, show, prerender, pending, preload }) {
9
+ const scroll = scrollPositions();
10
+ let version = initial.version;
11
+ let current;
12
+ // the page each entry showed, for Back and Forward: in memory only (gone on reload), the last 50; all of them
13
+ // forgotten once a form is sent (what it changed, a signed-out user's pages), its flash data not shown again
14
+ const kept = new Map();
15
+ const keep = (page) => {
16
+ const key = scroll.key();
17
+ if (!key)
18
+ return;
19
+ kept.delete(key);
20
+ kept.set(key, { ...page, flash: {} });
21
+ if (kept.size > 50)
22
+ kept.delete(kept.keys().next().value);
23
+ };
24
+ const headers = () => ({ 'X-Fondu': 'true', ...(version && { 'X-Fondu-Version': version }) });
25
+ /** a document load: in place of this entry, or after it */
26
+ const documentLoad = (url, replace) => {
27
+ // http(s) only: a header's URL never runs as `javascript:` (an app's redirect to a URL it was given)
28
+ if (!isWebUrl(url))
29
+ return console.error(`fondu: not loading ${url}: not an http(s) URL`);
30
+ if (replace)
31
+ location.replace(url);
32
+ else
33
+ location.assign(url);
34
+ };
35
+ visit = (href, options) => {
36
+ current?.abort();
37
+ const controller = (current = new AbortController());
38
+ const { signal } = controller;
39
+ const url = new URL(href, location.href);
40
+ if (url.origin !== location.origin)
41
+ return documentLoad(url.href, options.history !== 'push');
42
+ const post = options.body !== undefined;
43
+ if (options.history !== 'none')
44
+ scroll.save();
45
+ const form = options.form ?? null;
46
+ // a kept page is on the screen at once: the server's comes quietly, no pending state (the navigation it
47
+ // interrupted, aborted, ends here)
48
+ const shown = options.kept;
49
+ // the form being sent is aria-busy: for its submit buttons' pending state (CSS, or useNavigating()), and for
50
+ // assistive technology
51
+ form?.setAttribute('aria-busy', 'true');
52
+ pending(!shown, form !== null);
53
+ const done = () => {
54
+ form?.removeAttribute('aria-busy');
55
+ if (!shown && current === controller)
56
+ pending(false, false);
57
+ };
58
+ if (!post)
59
+ preload(url.pathname);
60
+ const from = !post && prerender ? pageDataPath(url.pathname) + url.search : url.pathname + url.search;
61
+ void (async () => {
62
+ try {
63
+ if (shown) {
64
+ scroll.commit();
65
+ await show(shown, true, () => signal.aborted);
66
+ if (signal.aborted)
67
+ return;
68
+ announce();
69
+ restore(options.scroll, url.hash);
70
+ }
71
+ const response = await fetch(from, {
72
+ method: post ? 'POST' : 'GET',
73
+ body: options.body,
74
+ headers: headers(),
75
+ signal,
76
+ });
77
+ if (signal.aborted)
78
+ return;
79
+ if (post)
80
+ kept.clear();
81
+ // a new build, another site: the document
82
+ const to = response.headers.get('X-Fondu-Location');
83
+ if (response.status === 409 && to)
84
+ return documentLoad(to, options.history !== 'push');
85
+ // a redirect to a #fragment, followed here: fetch() would lose it
86
+ const redirect = response.headers.get('X-Fondu-Redirect');
87
+ if (redirect) {
88
+ if (isWebUrl(redirect))
89
+ visit(redirect, { history: options.history === 'push' ? 'push' : 'replace' });
90
+ return;
91
+ }
92
+ if (!response.headers.has('X-Fondu')) {
93
+ // a proxy's error page, a file: the URL as a document (a POST's redirect: its target), this answer unread
94
+ if (!post || response.redirected) {
95
+ void response.body?.cancel();
96
+ return documentLoad(post ? response.url : url.href, options.history !== 'push');
97
+ }
98
+ // the answer to a POST itself, which can't be asked for again: as the browser would show it
99
+ const disposition = response.headers.get('Content-Disposition') ?? '';
100
+ if (/^\s*attachment/i.test(disposition)) {
101
+ // a file: downloaded, the page stays
102
+ const link = document.createElement('a'); // with `download`: let through by the click listener
103
+ link.href = URL.createObjectURL(await response.blob());
104
+ link.download = fileName(disposition);
105
+ link.click();
106
+ setTimeout(() => URL.revokeObjectURL(link.href), 60_000); // once the download has it
107
+ return;
108
+ }
109
+ const html = /^\s*text\/html\b/i.test(response.headers.get('Content-Type') ?? '');
110
+ const text = await response.text();
111
+ document.open(); // the page's listeners gone with it, popstate's too
112
+ // anything else as text, never as HTML (an action's `c.text()` with what a user typed)
113
+ document.write(html ? text : '<!doctype html><meta charset="utf-8"><pre></pre>');
114
+ document.close();
115
+ if (!html)
116
+ document.querySelector('pre').textContent = text;
117
+ // Back and Forward stay in this document (the entries before it are its own): their URL as a document
118
+ addEventListener('popstate', () => location.reload());
119
+ return;
120
+ }
121
+ const page = (await response.json());
122
+ if (signal.aborted)
123
+ return;
124
+ if (version && page.version && page.version !== version)
125
+ return documentLoad(page.url, options.history !== 'push');
126
+ version = page.version;
127
+ // the answer to a form at the URL it was sent from: in place, its scroll position kept; back with errors,
128
+ // the form and what was typed stay too
129
+ const here = location.pathname + location.search;
130
+ const same = post && page.url === here;
131
+ const errors = Object.keys(page.props.errors ?? {}).length > 0;
132
+ const asked = url.pathname + url.search;
133
+ // the kept page's entry: the server's page in its place, its state kept; redirected (signed out), anew
134
+ const fresh = shown ? asked !== page.url : !options.keep && !(same && errors);
135
+ // its URL first, as the browser's own navigations: a page's render reads it (`location`, a URL's language);
136
+ // a redirect's, the fragment asked for if it is still this page's
137
+ const target = page.url + (asked === page.url ? url.hash : '');
138
+ // a form's answer at its URL replaces the entry; Back's or reload()'s page redirected elsewhere too
139
+ const history = options.history === 'push' && same
140
+ ? 'replace'
141
+ : options.history === 'none' && asked !== page.url
142
+ ? 'replace'
143
+ : options.history;
144
+ if (history !== 'none')
145
+ scroll.write(target, history === 'replace');
146
+ scroll.commit();
147
+ await show(page, fresh, () => signal.aborted);
148
+ if (signal.aborted)
149
+ return;
150
+ keep(page);
151
+ if (shown && !fresh)
152
+ return;
153
+ if (!options.keep)
154
+ announce();
155
+ // a page asked for again stays where it is: reload(), a form's answer at its URL
156
+ if (options.keep || same)
157
+ return;
158
+ // at once, the page in the DOM (show() renders it synchronously): no frame drawn where it doesn't belong
159
+ restore(options.history === 'none' ? options.scroll : null, url.hash);
160
+ }
161
+ catch (error) {
162
+ if (error.name === 'AbortError' || shown)
163
+ return; // offline, Back: the kept page stays
164
+ if (post)
165
+ throw error; // the form stays, to send again
166
+ documentLoad(url.href, options.history !== 'push'); // offline: the browser says so
167
+ }
168
+ finally {
169
+ done();
170
+ }
171
+ })();
172
+ };
173
+ // links: a plain click on a same-origin <a href>, which the page let through
174
+ document.addEventListener('click', (event) => {
175
+ if (event.defaultPrevented || event.button !== 0)
176
+ return;
177
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
178
+ return;
179
+ const link = event.target?.closest?.('a[href]');
180
+ if (!(link instanceof HTMLAnchorElement))
181
+ return;
182
+ if ((link.target && link.target !== '_self') || link.hasAttribute('download'))
183
+ return;
184
+ const url = new URL(link.href);
185
+ if (url.origin !== location.origin || !isWebUrl(url.href))
186
+ return;
187
+ // a #fragment of this page: the browser scrolls to it (the position left saved for Back)
188
+ if (url.pathname + url.search === location.pathname + location.search && url.hash) {
189
+ scroll.save();
190
+ return;
191
+ }
192
+ event.preventDefault();
193
+ visit(url.href, { history: url.href === location.href ? 'replace' : 'push' });
194
+ });
195
+ // forms: GET and POST to this origin, in this window, which the page let through
196
+ document.addEventListener('submit', (event) => {
197
+ if (event.defaultPrevented)
198
+ return;
199
+ const form = event.target;
200
+ if (!(form instanceof HTMLFormElement))
201
+ return;
202
+ const submitter = event.submitter;
203
+ const attr = (name, own) => submitter?.getAttribute(own) ?? form.getAttribute(name) ?? '';
204
+ const method = attr('method', 'formmethod').toLowerCase() || 'get';
205
+ const target = attr('target', 'formtarget');
206
+ if ((method !== 'get' && method !== 'post') || (target && target !== '_self'))
207
+ return;
208
+ const url = new URL(attr('action', 'formaction') || location.href, location.href);
209
+ if (url.origin !== location.origin || !isWebUrl(url.href))
210
+ return;
211
+ event.preventDefault();
212
+ const data = new FormData(form, submitter);
213
+ // as the browser would send them: a file by its name in a URL or plain text, line breaks as CRLF (a <textarea>'s)
214
+ const crlf = (text) => text.replace(/\r\n|\r|\n/g, '\r\n');
215
+ const fields = [...data].map(([k, v]) => [crlf(k), crlf(typeof v === 'string' ? v : v.name)]);
216
+ if (method === 'post') {
217
+ const enctype = attr('enctype', 'formenctype').toLowerCase();
218
+ const body = enctype === 'multipart/form-data'
219
+ ? data
220
+ : enctype === 'text/plain'
221
+ ? fields.map(([k, v]) => `${k}=${v}\r\n`).join('')
222
+ : new URLSearchParams(fields);
223
+ return visit(url.href, { history: 'push', body, form });
224
+ }
225
+ url.search = new URLSearchParams(fields).toString();
226
+ visit(url.href, { history: 'push', form });
227
+ });
228
+ // Back and Forward: the entry's page, where it was scrolled
229
+ window.addEventListener('popstate', (event) => {
230
+ const popped = scroll.pop(event.state);
231
+ if (popped.fragment)
232
+ return restore(popped.position, location.hash);
233
+ visit(location.href, {
234
+ history: 'none',
235
+ scroll: popped.position,
236
+ kept: kept.get(popped.key ?? ''),
237
+ });
238
+ });
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
+ });
249
+ // the browser's own #fragment navigations: the entry gets its key
250
+ 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);
257
+ }
258
+ else
259
+ restore(null, location.hash, true);
260
+ keep(initial);
261
+ }
262
+ /**
263
+ * Scroll positions, per history entry (a key in `history.state`), in memory and in sessionStorage (a reload, a page
264
+ * of another site and back): the browser's restoration is off. Each belongs to the entry on the screen, even once
265
+ * the browser has started a traversal away from it.
266
+ */
267
+ function scrollPositions() {
268
+ const KEY = SCROLL_KEY;
269
+ const STORAGE = SCROLL_STORAGE;
270
+ const positions = new Map();
271
+ const keyOf = (state) => state &&
272
+ typeof state === 'object' &&
273
+ typeof state[KEY] === 'string'
274
+ ? state[KEY]
275
+ : null;
276
+ const newKey = () => Math.random().toString(36).slice(2, 10);
277
+ let rendered = keyOf(history.state);
278
+ let renderedUrl = location.pathname + location.search;
279
+ const read = (key) => {
280
+ if (!key)
281
+ return null;
282
+ const kept = positions.get(key);
283
+ if (kept)
284
+ return kept;
285
+ try {
286
+ const saved = JSON.parse(sessionStorage.getItem(STORAGE + key) ?? 'null');
287
+ return typeof saved?.x === 'number' && typeof saved.y === 'number' ? saved : null;
288
+ }
289
+ catch {
290
+ return null;
291
+ }
292
+ };
293
+ const save = () => {
294
+ if (!rendered)
295
+ return;
296
+ const position = { x: scrollX, y: scrollY };
297
+ positions.set(rendered, position);
298
+ try {
299
+ sessionStorage.setItem(STORAGE + rendered, JSON.stringify(position));
300
+ }
301
+ catch { }
302
+ };
303
+ // the entry on the screen follows its scrolling (once a frame, in memory): its position is the one it was left at,
304
+ // whenever the browser starts a traversal away from it
305
+ let frame = 0;
306
+ addEventListener('scroll', () => {
307
+ const key = rendered; // the entry scrolled, before a popstate makes another one rendered
308
+ if (frame || !key)
309
+ return;
310
+ frame = requestAnimationFrame(() => {
311
+ frame = 0;
312
+ if (rendered === key)
313
+ positions.set(key, { x: scrollX, y: scrollY });
314
+ });
315
+ }, { passive: true });
316
+ /** this entry's key, given one if it has none (a first visit, a #fragment the browser navigated to) */
317
+ const commit = () => {
318
+ let key = keyOf(history.state);
319
+ if (!key)
320
+ history.replaceState({ ...history.state, [KEY]: (key = newKey()) }, '');
321
+ rendered = key;
322
+ renderedUrl = location.pathname + location.search;
323
+ };
324
+ return {
325
+ save,
326
+ commit,
327
+ /** the key of the entry on the screen */
328
+ key: () => rendered,
329
+ /**
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
332
+ */
333
+ 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
+ history.scrollRestoration = 'manual';
338
+ commit();
339
+ return { position: read(rendered), restored };
340
+ },
341
+ /** the page on the screen, at `url`: a new entry or this one */
342
+ write(url, replace) {
343
+ const state = { ...(replace ? history.state : {}), [KEY]: newKey() };
344
+ if (replace)
345
+ history.replaceState(state, '', url);
346
+ else
347
+ history.pushState(state, '', url);
348
+ },
349
+ /** Back or Forward to `state`'s entry: where it was; `fragment`: the same page, another #fragment */
350
+ pop(state) {
351
+ // nothing saved here: the browser may have scrolled already (Firefox and Safari to a #fragment, before its
352
+ // popstate); the entry left has its position from its scroll events
353
+ const key = keyOf(state);
354
+ const fragment = location.pathname + location.search === renderedUrl;
355
+ const position = read(key);
356
+ if (fragment)
357
+ commit();
358
+ return { fragment, position, key };
359
+ },
360
+ };
361
+ }
362
+ /** an entry's position, or its #fragment, or the top */
363
+ function restore(position, hash, first = false) {
364
+ if (position)
365
+ return window.scrollTo(position.x, position.y);
366
+ const target = hash && fragmentTarget(hash.slice(1));
367
+ if (target)
368
+ return target.scrollIntoView();
369
+ if (!first)
370
+ window.scrollTo(0, 0);
371
+ }
372
+ /** the element a #fragment names: decoded, or as it is (`#100%` doesn't decode); null for none */
373
+ function fragmentTarget(fragment) {
374
+ try {
375
+ return (document.getElementById(decodeURIComponent(fragment)) ?? document.getElementById(fragment));
376
+ }
377
+ catch {
378
+ return document.getElementById(fragment);
379
+ }
380
+ }
381
+ /** the new page's title, for screen readers: a navigation in place is otherwise silent */
382
+ let announcer;
383
+ function announce() {
384
+ if (!announcer) {
385
+ announcer = document.createElement('div');
386
+ announcer.setAttribute('aria-live', 'assertive');
387
+ announcer.setAttribute('aria-atomic', 'true');
388
+ Object.assign(announcer.style, {
389
+ position: 'absolute',
390
+ width: '1px',
391
+ height: '1px',
392
+ overflow: 'hidden',
393
+ clip: 'rect(0 0 0 0)',
394
+ whiteSpace: 'nowrap',
395
+ });
396
+ document.body.append(announcer);
397
+ }
398
+ announcer.textContent = document.title;
399
+ }
400
+ const isWebUrl = (url) => {
401
+ try {
402
+ return /^https?:$/.test(new URL(url, location.href).protocol);
403
+ }
404
+ catch {
405
+ return false;
406
+ }
407
+ };
408
+ /** a Content-Disposition's file name: `filename*=UTF-8''…` (RFC 6266), else `filename="…"`; '' for none */
409
+ function fileName(disposition) {
410
+ const encoded = /filename\*\s*=\s*UTF-8''([^;\s]+)/i.exec(disposition)?.[1];
411
+ if (encoded)
412
+ try {
413
+ return decodeURIComponent(encoded);
414
+ }
415
+ catch { }
416
+ const m = /filename\s*=\s*(?:"([^"]*)"|([^;\s]+))/i.exec(disposition);
417
+ return m?.[1] ?? m?.[2] ?? '';
418
+ }
419
+ /**
420
+ * `href` loaded as a document. Every other navigation is the client's, links and forms: this is for what a page
421
+ * can't change in place (`<html lang>`: another language).
422
+ */
423
+ export function loadDocument(href) {
424
+ location.assign(href);
425
+ }
426
+ /** the page asked for again, its state and the scroll position kept */
427
+ export function again() {
428
+ if (visit)
429
+ visit(location.href, { history: 'none', keep: true });
430
+ else
431
+ location.reload();
432
+ }
@@ -0,0 +1,16 @@
1
+ /** `fondu({ progress })`: the bar at the top of the page while a navigation or a form's submission is on its way */
2
+ export interface ProgressOptions {
3
+ /** how long a navigation runs before the bar shows, in ms (a quick one shows none); default 350 */
4
+ delay?: number;
5
+ /** the bar's color, any CSS color (`var(--brand)`); default the page's text color (`currentColor`) */
6
+ color?: string;
7
+ /** for forms' submissions too; default true (`false`: their buttons show it, with the form's `aria-busy`) */
8
+ forms?: boolean;
9
+ }
10
+ /**
11
+ * The bar: shown once a navigation has run for `delay`, growing towards the end while it runs (one long CSS
12
+ * transition, slower as it gets there), filled, then faded out, when it is done. Drawn with CSSOM styles (no
13
+ * stylesheet, nothing for a Content Security Policy to refuse), above the page, out of the accessibility tree:
14
+ * `useNavigating()` is the app's way to say it in its own words. With reduced motion, a still bar.
15
+ */
16
+ export declare function progressBar({ delay, color }: ProgressOptions, pending: () => boolean): () => void;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The bar: shown once a navigation has run for `delay`, growing towards the end while it runs (one long CSS
3
+ * transition, slower as it gets there), filled, then faded out, when it is done. Drawn with CSSOM styles (no
4
+ * stylesheet, nothing for a Content Security Policy to refuse), above the page, out of the accessibility tree:
5
+ * `useNavigating()` is the app's way to say it in its own words. With reduced motion, a still bar.
6
+ */
7
+ export function progressBar({ delay = 350, color = 'currentColor' }, pending) {
8
+ const still = matchMedia('(prefers-reduced-motion: reduce)');
9
+ let bar;
10
+ let wait;
11
+ const bars = new Set(); // on the screen, the one fading out included
12
+ // leaving the document: no bar kept in the back/forward cache, to be seen again on Back
13
+ addEventListener('pagehide', () => {
14
+ clearTimeout(wait);
15
+ wait = bar = undefined;
16
+ for (const el of bars)
17
+ el.remove();
18
+ bars.clear();
19
+ });
20
+ const show = () => {
21
+ wait = undefined;
22
+ const el = (bar = document.createElement('div')); // a new one: the last may still be fading out
23
+ bars.add(el);
24
+ Object.assign(el.style, {
25
+ position: 'fixed',
26
+ inset: '0 0 auto',
27
+ zIndex: '2147483647',
28
+ height: '2px',
29
+ background: color,
30
+ transformOrigin: 'left',
31
+ transform: still.matches ? 'scaleX(1)' : 'scaleX(0)',
32
+ pointerEvents: 'none',
33
+ });
34
+ el.setAttribute('aria-hidden', 'true');
35
+ document.body.append(el);
36
+ if (still.matches)
37
+ return;
38
+ el.getBoundingClientRect(); // its start computed: the transition runs from 0
39
+ el.style.transition = 'transform 10s cubic-bezier(0.05, 0.7, 0.1, 1)';
40
+ el.style.transform = 'scaleX(0.95)';
41
+ };
42
+ const done = () => {
43
+ clearTimeout(wait);
44
+ wait = undefined;
45
+ const el = bar;
46
+ bar = undefined;
47
+ if (!el)
48
+ return;
49
+ // filled from where it is (a transition starts from the value on the screen), then faded out
50
+ el.style.transition = 'transform 200ms ease-out, opacity 200ms 200ms';
51
+ el.style.transform = 'scaleX(1)';
52
+ el.style.opacity = '0';
53
+ setTimeout(() => {
54
+ el.remove();
55
+ bars.delete(el);
56
+ }, 450);
57
+ };
58
+ return () => {
59
+ if (!pending())
60
+ return done();
61
+ if (wait === undefined && bar === undefined)
62
+ wait = setTimeout(show, delay);
63
+ };
64
+ }
package/dist/client.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { Routes } from './types.js';
2
2
  /**
3
3
  * Typed URLs from `app/routes/` (as React Router's `href`): `href('/users/:id', { id })` → `'/users/7'`;
4
4
  * one of a file's `actions`: `href('/users/:id?delete', { id })`. Only patterns that exist; params are required.
5
- * The method is Inertia's: `<Link href={href('/logout')} method="post" as="button">`, `<Form action=… method="post">`.
5
+ * A form's action too: `<form method="post" action={href('/logout')}>`.
6
6
  */
7
7
  type Keys = keyof Routes & string;
8
8
  type Actions = {
@@ -16,17 +16,6 @@ type Args<P extends string> = [ParamNames<P>] extends [never] ? [] : [params: {
16
16
  [K in ParamNames<P>]: string | number;
17
17
  }];
18
18
  export declare function href<P extends Href>(path: P, ...[params]: Args<P>): string;
19
- /**
20
- * `fondu({ prerender: true })` adds this to the client entry: visits ask for the page object at
21
- * `/_fondu/pages/<path>.json`, a static file for a prerendered page, the server's answer for any other. A page
22
- * object of another build reloads the page as a document, as the server's 409 would.
23
- */
24
- export declare function prerendered(): void;
25
- /**
26
- * The build adds this to the client entry: in production, a GET visit answered with something that isn't an Inertia
27
- * page (a proxy's 502, a CDN's 524) loads the URL as a document, as a site without Inertia would, instead of showing
28
- * that answer in Inertia's dialog. Fondu's own answers to visits are pages, errors included. Other methods keep the
29
- * dialog (loading the URL would drop the form); a visit's own `onHttpException` comes first.
30
- */
31
- export declare function documentFallback(): void;
32
- export {};
19
+ export { usePage, useNavigating, reload, __entry, renderPage, type Errors } from './client/app.js';
20
+ export { loadDocument, type Page } from './client/navigation.js';
21
+ export type { ProgressOptions } from './client/progress.js';
package/dist/client.js CHANGED
@@ -1,50 +1,6 @@
1
- import { http, router } from '@inertiajs/react';
2
- import { fillPattern, pageDataPath } from './paths.js';
1
+ import { fillPattern } from './paths.js';
3
2
  export function href(path, ...[params]) {
4
3
  return fillPattern(path, params);
5
4
  }
6
- /**
7
- * `fondu({ prerender: true })` adds this to the client entry: visits ask for the page object at
8
- * `/_fondu/pages/<path>.json`, a static file for a prerendered page, the server's answer for any other. A page
9
- * object of another build reloads the page as a document, as the server's 409 would.
10
- */
11
- export function prerendered() {
12
- const loaded = document.querySelector('script[data-page]')?.textContent;
13
- const version = loaded ? JSON.parse(loaded).version : null;
14
- http.onRequest((config) => {
15
- const url = new URL(config.url, location.href);
16
- if (config.method !== 'get' || !config.headers?.['X-Inertia'] || url.origin !== location.origin)
17
- return config;
18
- return { ...config, url: pageDataPath(url.pathname) + url.search };
19
- });
20
- http.onResponse((response) => {
21
- if (!version || response.status !== 200 || !response.headers['x-inertia'])
22
- return response;
23
- const page = JSON.parse(response.data);
24
- if (!page.version || page.version === version)
25
- return response;
26
- return {
27
- status: 409,
28
- data: '',
29
- headers: { 'x-inertia-location': page.url, 'x-inertia-version': page.version },
30
- };
31
- });
32
- }
33
- /**
34
- * The build adds this to the client entry: in production, a GET visit answered with something that isn't an Inertia
35
- * page (a proxy's 502, a CDN's 524) loads the URL as a document, as a site without Inertia would, instead of showing
36
- * that answer in Inertia's dialog. Fondu's own answers to visits are pages, errors included. Other methods keep the
37
- * dialog (loading the URL would drop the form); a visit's own `onHttpException` comes first.
38
- */
39
- export function documentFallback() {
40
- let visit;
41
- router.on('start', (event) => {
42
- visit = event.detail.visit;
43
- });
44
- router.on('httpException', (event) => {
45
- if (event.detail.response.headers['x-inertia'] || visit?.method !== 'get')
46
- return;
47
- event.preventDefault();
48
- window.location.href = visit.url.href;
49
- });
50
- }
5
+ export { usePage, useNavigating, reload, __entry, renderPage } from './client/app.js';
6
+ export { loadDocument } from './client/navigation.js';
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  export { defineHandler, defineMiddleware } from './server/handler.js';
2
- export { render, share, encryptHistory, type RenderOptions } from './server/protocol.js';
3
- export { back, location, isInertia, wantsPage } from './server/http.js';
4
- export { optional, always, defer, once, merge, prepend, deepMerge, scroll, type Prop, type Paginated, } from './server/props.js';
5
- export { flash, clearHistory } from './server/flash.js';
2
+ export { render, share, type RenderOptions } from './server/protocol.js';
3
+ export { back } from './server/http.js';
4
+ export { flash } from './server/flash.js';
6
5
  export { ValidationError, type StandardSchemaV1 } from './server/validate.js';
7
6
  export { withoutSsr, type DocumentParts, type DocumentTemplate } from './server/document.js';
8
7
  export type { Register } from './types.js';
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  export { defineHandler, defineMiddleware } from './server/handler.js';
2
- export { render, share, encryptHistory } from './server/protocol.js';
3
- export { back, location, isInertia, wantsPage } from './server/http.js';
4
- export { optional, always, defer, once, merge, prepend, deepMerge, scroll, } from './server/props.js';
5
- export { flash, clearHistory } from './server/flash.js';
2
+ export { render, share } from './server/protocol.js';
3
+ export { back } from './server/http.js';
4
+ export { flash } from './server/flash.js';
6
5
  export { ValidationError } from './server/validate.js';
7
6
  export { withoutSsr } from './server/document.js';
8
7
  // last: createApp imports the app's routes, which import this module (the exports above are ready by then)