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 +9 -7
- package/dist/client/app.d.ts +2 -1
- package/dist/client/app.js +32 -5
- package/dist/client/navigation.d.ts +10 -5
- package/dist/client/navigation.js +403 -173
- package/dist/client/progress.d.ts +3 -3
- package/dist/client/progress.js +19 -6
- package/dist/paths.d.ts +13 -1
- package/dist/paths.js +38 -7
- package/dist/server/document.js +23 -2
- package/dist/server/errors.js +2 -0
- package/dist/server/prerender.js +12 -4
- package/dist/server/protocol.js +10 -8
- package/dist/server/validate.d.ts +4 -3
- package/dist/server/validate.js +18 -19
- package/dist/vite/index.js +16 -1
- package/dist/vite/strip.d.ts +1 -1
- package/dist/vite/strip.js +26 -21
- package/package.json +2 -1
|
@@ -1,169 +1,409 @@
|
|
|
1
|
-
import { pageDataPath } from '../paths.js';
|
|
2
|
-
|
|
3
|
-
const navigation = () => globalThis.navigation;
|
|
1
|
+
import { pageDataPath, SCROLL_KEY, SCROLL_STORAGE } from '../paths.js';
|
|
2
|
+
let visit;
|
|
4
3
|
/**
|
|
5
|
-
* Same-origin links
|
|
6
|
-
* shown in place
|
|
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
7
|
*/
|
|
8
|
-
export function startNavigation({ initial, show, prerender, pending, preload }) {
|
|
9
|
-
const
|
|
10
|
-
if (!nav)
|
|
11
|
-
return;
|
|
8
|
+
export function startNavigation({ initial, show, prerender, pending, preload, isPage, hydrated, }) {
|
|
9
|
+
const scroll = scrollPositions();
|
|
12
10
|
let version = initial.version;
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
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);
|
|
26
23
|
};
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* A document load: a POST's after the page it was sent from (`push`); a GET's in place of the entry it committed:
|
|
31
|
-
* `instead` when that entry was pushed. A document replacing it would take the entries before it, of this
|
|
32
|
-
* document, for its own (Chromium): Back would change the URL only. So back to the entry it was pushed from, then
|
|
33
|
-
* the document pushed from there.
|
|
34
|
-
*/
|
|
35
|
-
const documentLoad = async (url, history) => {
|
|
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) => {
|
|
36
27
|
// http(s) only: a header's URL never runs as `javascript:` (an app's redirect to a URL it was given)
|
|
37
28
|
if (!isWebUrl(url))
|
|
38
29
|
return console.error(`fondu: not loading ${url}: not an http(s) URL`);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
/** the page a response is, or undefined when it's handled here (a new build, not a page) */
|
|
45
|
-
const read = async (response, url, history) => {
|
|
46
|
-
const post = history === 'push';
|
|
47
|
-
const to = response.headers.get('X-Fondu-Location');
|
|
48
|
-
if (response.status === 409 && to)
|
|
49
|
-
return void documentLoad(to, history);
|
|
50
|
-
// a redirect to a #fragment, followed here: fetch() would lose it
|
|
51
|
-
const redirect = response.headers.get('X-Fondu-Redirect');
|
|
52
|
-
if (redirect && isWebUrl(redirect))
|
|
53
|
-
return void nav.navigate(redirect, { history: post ? 'push' : 'replace' });
|
|
54
|
-
if (!response.headers.has('X-Fondu')) {
|
|
55
|
-
// a proxy's error page, a file: the URL as a document (a POST's redirect: its target); the answer to a POST
|
|
56
|
-
// itself can't be asked for again
|
|
57
|
-
if (!post || response.redirected)
|
|
58
|
-
return void documentLoad(url, history);
|
|
59
|
-
const html = await response.text();
|
|
60
|
-
document.open();
|
|
61
|
-
document.write(html);
|
|
62
|
-
document.close();
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const page = (await response.json());
|
|
66
|
-
if (version && page.version && page.version !== version)
|
|
67
|
-
return void documentLoad(page.url, history);
|
|
68
|
-
version = page.version;
|
|
69
|
-
return page;
|
|
30
|
+
if (replace)
|
|
31
|
+
location.replace(url);
|
|
32
|
+
else
|
|
33
|
+
location.assign(url);
|
|
70
34
|
};
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
+
})();
|
|
75
172
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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)
|
|
80
178
|
return;
|
|
81
|
-
|
|
82
|
-
if (
|
|
179
|
+
const link = event.target?.closest?.('a[href]');
|
|
180
|
+
if (!(link instanceof HTMLAnchorElement))
|
|
83
181
|
return;
|
|
84
|
-
if (
|
|
182
|
+
if ((link.target && link.target !== '_self') || link.hasAttribute('download'))
|
|
85
183
|
return;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
// the same page (a form back with its errors): the focus stays where it was, as the scroll position does
|
|
93
|
-
focusReset: fresh ? 'after-transition' : 'manual',
|
|
94
|
-
async handler() {
|
|
95
|
-
await show(page, fresh, () => event.signal.aborted);
|
|
96
|
-
if (scroll && !event.signal.aborted)
|
|
97
|
-
scrollAfter(event);
|
|
98
|
-
},
|
|
99
|
-
});
|
|
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();
|
|
100
190
|
return;
|
|
101
191
|
}
|
|
102
|
-
// a
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
})
|
|
124
|
-
.catch((error) => {
|
|
125
|
-
if (error.name !== 'AbortError')
|
|
126
|
-
throw error; // the form stays, to send again
|
|
127
|
-
})
|
|
128
|
-
.finally(done);
|
|
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;
|
|
195
|
+
event.preventDefault();
|
|
196
|
+
visit(url.href, { history: url.href === location.href ? 'replace' : 'push' });
|
|
197
|
+
});
|
|
198
|
+
// forms: GET and POST to this origin, in this window, which the page let through
|
|
199
|
+
document.addEventListener('submit', (event) => {
|
|
200
|
+
if (event.defaultPrevented)
|
|
201
|
+
return;
|
|
202
|
+
const form = event.target;
|
|
203
|
+
if (!(form instanceof HTMLFormElement))
|
|
204
|
+
return;
|
|
205
|
+
const submitter = event.submitter;
|
|
206
|
+
const attr = (name, own) => submitter?.getAttribute(own) ?? form.getAttribute(name) ?? '';
|
|
207
|
+
const method = attr('method', 'formmethod').toLowerCase() || 'get';
|
|
208
|
+
const target = attr('target', 'formtarget');
|
|
209
|
+
if ((method !== 'get' && method !== 'post') || (target && target !== '_self'))
|
|
210
|
+
return;
|
|
211
|
+
const url = new URL(attr('action', 'formaction') || location.href, location.href);
|
|
212
|
+
if (url.origin !== location.origin || !isWebUrl(url.href))
|
|
129
213
|
return;
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
const data = new FormData(form, submitter);
|
|
216
|
+
// as the browser would send them: a file by its name in a URL or plain text, line breaks as CRLF (a <textarea>'s)
|
|
217
|
+
const crlf = (text) => text.replace(/\r\n|\r|\n/g, '\r\n');
|
|
218
|
+
const fields = [...data].map(([k, v]) => [crlf(k), crlf(typeof v === 'string' ? v : v.name)]);
|
|
219
|
+
if (method === 'post') {
|
|
220
|
+
const enctype = attr('enctype', 'formenctype').toLowerCase();
|
|
221
|
+
const body = enctype === 'multipart/form-data'
|
|
222
|
+
? data
|
|
223
|
+
: enctype === 'text/plain'
|
|
224
|
+
? fields.map(([k, v]) => `${k}=${v}\r\n`).join('')
|
|
225
|
+
: new URLSearchParams(fields);
|
|
226
|
+
return visit(url.href, { history: 'push', body, form });
|
|
130
227
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const response = await fetch(from, { headers: headers(), signal: event.signal });
|
|
144
|
-
const page = await read(response, url.href, pushed);
|
|
145
|
-
if (!page || event.signal.aborted)
|
|
146
|
-
return;
|
|
147
|
-
// redirected: the page at its own URL, in place of this entry
|
|
148
|
-
if (page.url !== path)
|
|
149
|
-
return void (await display(page, true, true, 'replace').finished?.catch(noop));
|
|
150
|
-
const reload = event.navigationType === 'reload';
|
|
151
|
-
// Back and Forward commit after the navigate event: scrolled before, the page would show at the top first
|
|
152
|
-
await nav.transition?.committed;
|
|
153
|
-
// superseded meanwhile (Back pressed before it was on the screen): neither shown nor scrolled
|
|
154
|
-
await show(page, !reload, () => event.signal.aborted);
|
|
155
|
-
if (!reload && !event.signal.aborted)
|
|
156
|
-
scrollAfter(event);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
failed(url.href, pushed)(error);
|
|
160
|
-
}
|
|
161
|
-
finally {
|
|
162
|
-
done();
|
|
163
|
-
}
|
|
164
|
-
},
|
|
228
|
+
url.search = new URLSearchParams(fields).toString();
|
|
229
|
+
visit(url.href, { history: 'push', form });
|
|
230
|
+
});
|
|
231
|
+
// Back and Forward: the entry's page, where it was scrolled
|
|
232
|
+
window.addEventListener('popstate', (event) => {
|
|
233
|
+
const popped = scroll.pop(event.state);
|
|
234
|
+
if (popped.fragment)
|
|
235
|
+
return restore(popped.position, location.hash);
|
|
236
|
+
visit(location.href, {
|
|
237
|
+
history: 'none',
|
|
238
|
+
scroll: popped.position,
|
|
239
|
+
kept: kept.get(popped.key ?? ''),
|
|
165
240
|
});
|
|
166
241
|
});
|
|
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);
|
|
246
|
+
// the browser's own #fragment navigations: the entry gets its key
|
|
247
|
+
window.addEventListener('hashchange', scroll.commit);
|
|
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);
|
|
256
|
+
}
|
|
257
|
+
keep(initial);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Scroll positions, per history entry (a key in `history.state`), in memory and in sessionStorage (a reload, a page
|
|
261
|
+
* of another site and back): the browser's restoration is off. Each belongs to the entry on the screen, even once
|
|
262
|
+
* the browser has started a traversal away from it.
|
|
263
|
+
*/
|
|
264
|
+
function scrollPositions() {
|
|
265
|
+
const KEY = SCROLL_KEY;
|
|
266
|
+
const STORAGE = SCROLL_STORAGE;
|
|
267
|
+
const positions = new Map();
|
|
268
|
+
const keyOf = (state) => state &&
|
|
269
|
+
typeof state === 'object' &&
|
|
270
|
+
typeof state[KEY] === 'string'
|
|
271
|
+
? state[KEY]
|
|
272
|
+
: null;
|
|
273
|
+
const newKey = () => Math.random().toString(36).slice(2, 10);
|
|
274
|
+
let rendered = keyOf(history.state);
|
|
275
|
+
let renderedUrl = location.pathname + location.search;
|
|
276
|
+
const read = (key) => {
|
|
277
|
+
if (!key)
|
|
278
|
+
return null;
|
|
279
|
+
const kept = positions.get(key);
|
|
280
|
+
if (kept)
|
|
281
|
+
return kept;
|
|
282
|
+
try {
|
|
283
|
+
const saved = JSON.parse(sessionStorage.getItem(STORAGE + key) ?? 'null');
|
|
284
|
+
return typeof saved?.x === 'number' && typeof saved.y === 'number' ? saved : null;
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
const save = () => {
|
|
291
|
+
if (!rendered)
|
|
292
|
+
return;
|
|
293
|
+
const position = { x: scrollX, y: scrollY };
|
|
294
|
+
positions.set(rendered, position);
|
|
295
|
+
try {
|
|
296
|
+
sessionStorage.setItem(STORAGE + rendered, JSON.stringify(position));
|
|
297
|
+
}
|
|
298
|
+
catch { }
|
|
299
|
+
};
|
|
300
|
+
// the entry on the screen follows its scrolling (once a frame, in memory): its position is the one it was left at,
|
|
301
|
+
// whenever the browser starts a traversal away from it
|
|
302
|
+
let frame = 0;
|
|
303
|
+
addEventListener('scroll', () => {
|
|
304
|
+
const key = rendered; // the entry scrolled, before a popstate makes another one rendered
|
|
305
|
+
if (frame || !key)
|
|
306
|
+
return;
|
|
307
|
+
frame = requestAnimationFrame(() => {
|
|
308
|
+
frame = 0;
|
|
309
|
+
if (rendered === key)
|
|
310
|
+
positions.set(key, { x: scrollX, y: scrollY });
|
|
311
|
+
});
|
|
312
|
+
}, { passive: true });
|
|
313
|
+
/** this entry's key, given one if it has none (a first visit, a #fragment the browser navigated to) */
|
|
314
|
+
const commit = () => {
|
|
315
|
+
let key = keyOf(history.state);
|
|
316
|
+
if (!key)
|
|
317
|
+
history.replaceState({ ...history.state, [KEY]: (key = newKey()) }, '');
|
|
318
|
+
rendered = key;
|
|
319
|
+
renderedUrl = location.pathname + location.search;
|
|
320
|
+
};
|
|
321
|
+
return {
|
|
322
|
+
save,
|
|
323
|
+
commit,
|
|
324
|
+
/** the key of the entry on the screen */
|
|
325
|
+
key: () => rendered,
|
|
326
|
+
/**
|
|
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)
|
|
329
|
+
*/
|
|
330
|
+
start() {
|
|
331
|
+
history.scrollRestoration = 'manual';
|
|
332
|
+
commit();
|
|
333
|
+
return read(rendered);
|
|
334
|
+
},
|
|
335
|
+
/** the page on the screen, at `url`: a new entry or this one */
|
|
336
|
+
write(url, replace) {
|
|
337
|
+
const state = { ...(replace ? history.state : {}), [KEY]: newKey() };
|
|
338
|
+
if (replace)
|
|
339
|
+
history.replaceState(state, '', url);
|
|
340
|
+
else
|
|
341
|
+
history.pushState(state, '', url);
|
|
342
|
+
},
|
|
343
|
+
/** Back or Forward to `state`'s entry: where it was; `fragment`: the same page, another #fragment */
|
|
344
|
+
pop(state) {
|
|
345
|
+
// nothing saved here: the browser may have scrolled already (Firefox and Safari to a #fragment, before its
|
|
346
|
+
// popstate); the entry left has its position from its scroll events
|
|
347
|
+
const key = keyOf(state);
|
|
348
|
+
const fragment = location.pathname + location.search === renderedUrl;
|
|
349
|
+
const position = read(key);
|
|
350
|
+
if (fragment)
|
|
351
|
+
commit();
|
|
352
|
+
return { fragment, position, key };
|
|
353
|
+
},
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/** an entry's position, or its #fragment, or the top */
|
|
357
|
+
function restore(position, hash, first = false) {
|
|
358
|
+
if (position)
|
|
359
|
+
return window.scrollTo(position.x, position.y);
|
|
360
|
+
const target = hash && fragmentTarget(hash.slice(1));
|
|
361
|
+
if (target)
|
|
362
|
+
return target.scrollIntoView();
|
|
363
|
+
if (!first)
|
|
364
|
+
window.scrollTo(0, 0);
|
|
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
|
+
}
|
|
380
|
+
/** the element a #fragment names: decoded, or as it is (`#100%` doesn't decode); null for none */
|
|
381
|
+
function fragmentTarget(fragment) {
|
|
382
|
+
try {
|
|
383
|
+
return (document.getElementById(decodeURIComponent(fragment)) ?? document.getElementById(fragment));
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
return document.getElementById(fragment);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/** the new page's title, for screen readers: a navigation in place is otherwise silent */
|
|
390
|
+
let announcer;
|
|
391
|
+
function announce() {
|
|
392
|
+
if (!announcer) {
|
|
393
|
+
announcer = document.createElement('div');
|
|
394
|
+
announcer.setAttribute('aria-live', 'assertive');
|
|
395
|
+
announcer.setAttribute('aria-atomic', 'true');
|
|
396
|
+
Object.assign(announcer.style, {
|
|
397
|
+
position: 'absolute',
|
|
398
|
+
width: '1px',
|
|
399
|
+
height: '1px',
|
|
400
|
+
overflow: 'hidden',
|
|
401
|
+
clip: 'rect(0 0 0 0)',
|
|
402
|
+
whiteSpace: 'nowrap',
|
|
403
|
+
});
|
|
404
|
+
document.body.append(announcer);
|
|
405
|
+
}
|
|
406
|
+
announcer.textContent = document.title;
|
|
167
407
|
}
|
|
168
408
|
const isWebUrl = (url) => {
|
|
169
409
|
try {
|
|
@@ -173,38 +413,28 @@ const isWebUrl = (url) => {
|
|
|
173
413
|
return false;
|
|
174
414
|
}
|
|
175
415
|
};
|
|
176
|
-
/**
|
|
177
|
-
function
|
|
178
|
-
const
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
416
|
+
/** a Content-Disposition's file name: `filename*=UTF-8''…` (RFC 6266), else `filename="…"`; '' for none */
|
|
417
|
+
function fileName(disposition) {
|
|
418
|
+
const encoded = /filename\*\s*=\s*UTF-8''([^;\s]+)/i.exec(disposition)?.[1];
|
|
419
|
+
if (encoded)
|
|
420
|
+
try {
|
|
421
|
+
return decodeURIComponent(encoded);
|
|
422
|
+
}
|
|
423
|
+
catch { }
|
|
424
|
+
const m = /filename\s*=\s*(?:"([^"]*)"|([^;\s]+))/i.exec(disposition);
|
|
425
|
+
return m?.[1] ?? m?.[2] ?? '';
|
|
182
426
|
}
|
|
183
427
|
/**
|
|
184
|
-
* `href` loaded as a document. Every other navigation is the client's,
|
|
185
|
-
*
|
|
428
|
+
* `href` loaded as a document. Every other navigation is the client's, links and forms: this is for what a page
|
|
429
|
+
* can't change in place (`<html lang>`: another language).
|
|
186
430
|
*/
|
|
187
431
|
export function loadDocument(href) {
|
|
188
|
-
|
|
189
|
-
if (nav)
|
|
190
|
-
nav.navigate(href, { info: { fondu: 'document' } });
|
|
191
|
-
else
|
|
192
|
-
location.assign(href);
|
|
432
|
+
location.assign(href);
|
|
193
433
|
}
|
|
194
434
|
/** the page asked for again, its state and the scroll position kept */
|
|
195
435
|
export function again() {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
nav.reload({ info: { fondu: 'reload' } });
|
|
436
|
+
if (visit)
|
|
437
|
+
visit(location.href, { history: 'none', keep: true });
|
|
199
438
|
else
|
|
200
439
|
location.reload();
|
|
201
440
|
}
|
|
202
|
-
/**
|
|
203
|
-
* Where a navigation leaves the page: Back and Forward where it was, a #fragment at its target, else the top (which
|
|
204
|
-
* `event.scroll()` does too since whatwg/html#11987; Chromium 141 doesn't yet).
|
|
205
|
-
*/
|
|
206
|
-
function scrollAfter(event) {
|
|
207
|
-
event.scroll();
|
|
208
|
-
if (event.navigationType !== 'traverse' && !new URL(event.destination.url).hash)
|
|
209
|
-
window.scrollTo(0, 0);
|
|
210
|
-
}
|
|
@@ -9,8 +9,8 @@ export interface ProgressOptions {
|
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
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
|
|
13
|
-
* nothing for a Content Security Policy to refuse), above the page, out of the accessibility tree:
|
|
14
|
-
* is the app's way to say it in its own words. With reduced motion, a still bar.
|
|
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
15
|
*/
|
|
16
16
|
export declare function progressBar({ delay, color }: ProgressOptions, pending: () => boolean): () => void;
|