clarusjs 0.7.0 → 0.8.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/dist/check/generate.d.ts +6 -0
- package/dist/check/generate.js +12 -22
- package/dist/cli/check.js +58 -15
- package/dist/cli/explain.js +0 -1
- package/dist/cli/index.js +0 -1
- package/dist/compiler/emit-client.js +1 -11
- package/dist/compiler/emit-server.js +1 -2
- package/dist/compiler/errors.js +0 -1
- package/dist/compiler/html-expr.js +2 -7
- package/dist/compiler/index.js +10 -8
- package/dist/compiler/ir.d.ts +1 -0
- package/dist/compiler/ir.js +9 -14
- package/dist/compiler/parse.d.ts +1 -0
- package/dist/compiler/parse.js +18 -19
- package/dist/compiler/script.js +2 -31
- package/dist/compiler/shared.js +0 -5
- package/dist/runtime/dom.js +4 -1
- package/dist/runtime/fragments.d.ts +1 -1
- package/dist/runtime/fragments.js +10 -30
- package/dist/runtime/html.d.ts +2 -0
- package/dist/runtime/html.js +4 -0
- package/dist/runtime/loader.js +41 -2
- package/dist/runtime/navigate.d.ts +38 -0
- package/dist/runtime/navigate.js +88 -0
- package/dist/runtime/navigation.d.ts +88 -0
- package/dist/runtime/navigation.js +165 -0
- package/dist/runtime/router.d.ts +8 -1
- package/dist/runtime/router.js +32 -42
- package/dist/runtime/server.d.ts +1 -1
- package/dist/runtime/server.js +1 -1
- package/dist/server/handler.d.ts +18 -0
- package/dist/server/handler.js +80 -53
- package/dist/server/head.d.ts +36 -0
- package/dist/server/head.js +129 -0
- package/dist/server/manifest.js +0 -3
- package/dist/server/modes.js +0 -5
- package/dist/server/views.js +0 -7
- package/dist/vite/aliases.d.ts +10 -0
- package/dist/vite/aliases.js +42 -0
- package/dist/vite/build.d.ts +9 -0
- package/dist/vite/build.js +43 -17
- package/dist/vite/links.d.ts +2 -0
- package/dist/vite/links.js +11 -10
- package/dist/vite/manifest.js +2 -10
- package/dist/vite/netlify.js +1 -7
- package/dist/vite/plugin.d.ts +35 -0
- package/dist/vite/plugin.js +162 -88
- package/dist/vite/redirects.d.ts +10 -0
- package/dist/vite/redirects.js +41 -0
- package/dist/vite/sitemap.d.ts +11 -0
- package/dist/vite/sitemap.js +31 -0
- package/package.json +18 -2
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type Signal } from './signals.ts';
|
|
2
|
+
import type { Transition } from './navigate.ts';
|
|
3
|
+
export type { Transition };
|
|
4
|
+
/**
|
|
5
|
+
* The current page's URL, as a store: `let here = shared(currentUrl)` in an island's script. `null` on the server
|
|
6
|
+
* (what the server renders), so pass the page's path as a prop when the server HTML needs it.
|
|
7
|
+
* @see https://clarusjs.com/guide/fragment-navigation
|
|
8
|
+
*/
|
|
9
|
+
export declare const currentUrl: Signal<URL | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Go to `to`, as a link click would: the page's navigator swaps or renders it. Resolves once the new page is shown,
|
|
12
|
+
* and also when the navigation is cancelled or a newer one replaces it (so check `currentUrl` when it matters);
|
|
13
|
+
* rejects when it fails.
|
|
14
|
+
* @see https://clarusjs.com/guide/fragment-navigation
|
|
15
|
+
* @see https://clarusjs.com/guide/client-routes
|
|
16
|
+
*/
|
|
17
|
+
export declare function navigate(to: string | URL, options?: {
|
|
18
|
+
replace?: boolean;
|
|
19
|
+
state?: unknown;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Stops the navigation in progress, like the browser's stop button: its requests are cancelled and, where the URL was
|
|
23
|
+
* held while loading (push and replace in browsers with precommit handlers), page and URL stay as they were. Hooks
|
|
24
|
+
* see it through `t.signal`. Like the stop button, it also stops the page's own loading (images still arriving).
|
|
25
|
+
* @see https://clarusjs.com/guide/navigation
|
|
26
|
+
*/
|
|
27
|
+
export declare function abort(): void;
|
|
28
|
+
/** Back one entry, like the browser's back button. */
|
|
29
|
+
export declare function back(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Before any navigation (links, forms, back/forward, `navigate()`): `nav.cancel()` stops it, for example to keep
|
|
32
|
+
* unsaved work. Back/forward can only be cancelled where the browser allows it.
|
|
33
|
+
* @see https://clarusjs.com/guide/navigation
|
|
34
|
+
*/
|
|
35
|
+
export declare function beforeNavigate(fn: (to: URL, nav: {
|
|
36
|
+
type: string;
|
|
37
|
+
cancel(): void;
|
|
38
|
+
}) => void): void;
|
|
39
|
+
/**
|
|
40
|
+
* After a navigation within the page finished: the new page is in and its islands attached.
|
|
41
|
+
* @see https://clarusjs.com/guide/navigation
|
|
42
|
+
*/
|
|
43
|
+
export declare function afterNavigate(fn: (to: URL) => void): void;
|
|
44
|
+
/**
|
|
45
|
+
* A navigation failed (a fragment or a client page could not be loaded). The page then loads normally.
|
|
46
|
+
* @see https://clarusjs.com/guide/navigation
|
|
47
|
+
*/
|
|
48
|
+
export declare function onNavigateError(fn: (to: URL, error: unknown) => void): void;
|
|
49
|
+
/**
|
|
50
|
+
* The navigation in progress, as a store (`let nav = shared(navigating)`): `null` when idle, else where it goes and
|
|
51
|
+
* its phase, `'loading'` (the next page is fetched) or `'swapping'` (it replaces this one). For CSS alone,
|
|
52
|
+
* `<html data-cl-navigating>` carries the phase.
|
|
53
|
+
* @see https://clarusjs.com/guide/navigation
|
|
54
|
+
*/
|
|
55
|
+
export declare const navigating: Signal<Transition | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Page changes run as view transitions where the browser has them (`document.startViewTransition`), so CSS
|
|
58
|
+
* `::view-transition-*` rules animate them; elsewhere they stay instant. Call it once, from any island's script.
|
|
59
|
+
* `beforeSwap` hooks run before the browser's snapshot, `afterSwap` hooks once the new page is in.
|
|
60
|
+
* @see https://clarusjs.com/guide/navigation
|
|
61
|
+
*/
|
|
62
|
+
export declare function viewTransitions(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Runs once the next page is loaded, before it replaces this one (animate out, save state). Return a promise to
|
|
65
|
+
* make the navigation wait; `t.signal` aborts when a newer navigation takes over. A hook that throws makes the page
|
|
66
|
+
* load normally.
|
|
67
|
+
* @see https://clarusjs.com/guide/navigation
|
|
68
|
+
*/
|
|
69
|
+
export declare function beforeSwap(fn: (t: Transition) => unknown): void;
|
|
70
|
+
/**
|
|
71
|
+
* Runs once the new page is in and its islands attached (animate in, move focus). Return a promise to make the
|
|
72
|
+
* navigation wait for it.
|
|
73
|
+
* @see https://clarusjs.com/guide/navigation
|
|
74
|
+
*/
|
|
75
|
+
export declare function afterSwap(fn: (t: Transition) => unknown): void;
|
|
76
|
+
/** Route → params. Filled in by the generated .clarusjs/routes.d.ts (declaration merging; see `clarusjs check`). */
|
|
77
|
+
export interface Routes {
|
|
78
|
+
}
|
|
79
|
+
/** Without the generated routes any path is accepted; with them, only real routes. */
|
|
80
|
+
export type RouteName = [keyof Routes] extends [never] ? string : Extract<keyof Routes, string>;
|
|
81
|
+
type ParamsOf<R> = R extends keyof Routes ? Routes[R] : Record<string, string>;
|
|
82
|
+
/**
|
|
83
|
+
* A page's URL from its route, as its file names it, with the site's base: `href('/releases/[version]',
|
|
84
|
+
* { version: '0.8.0' })` is `'/releases/0.8.0'`. With .clarusjs/routes.d.ts (written by `clarusjs check`), a route
|
|
85
|
+
* that doesn't exist or a missing param is a type error. Params are encoded.
|
|
86
|
+
* @see https://clarusjs.com/guide/navigation
|
|
87
|
+
*/
|
|
88
|
+
export declare function href<R extends RouteName>(route: R, ...params: [keyof Routes] extends [never] ? [Record<string, string>?] : keyof ParamsOf<R> extends never ? [] : [ParamsOf<R>]): string;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Moving between pages from islands: `import { navigate, back, currentUrl } from 'clarusjs/navigation'`.
|
|
2
|
+
// Navigations go through the Navigation API, so the page's navigator (fragments or the client router) takes them
|
|
3
|
+
// over exactly like a link click; without it they are normal page loads. Island scripts also run on the server,
|
|
4
|
+
// where this does nothing.
|
|
5
|
+
import { onDispose, state } from './signals.js';
|
|
6
|
+
const navigation = globalThis.navigation;
|
|
7
|
+
const browser = typeof document !== 'undefined';
|
|
8
|
+
/**
|
|
9
|
+
* The current page's URL, as a store: `let here = shared(currentUrl)` in an island's script. `null` on the server
|
|
10
|
+
* (what the server renders), so pass the page's path as a prop when the server HTML needs it.
|
|
11
|
+
* @see https://clarusjs.com/guide/fragment-navigation
|
|
12
|
+
*/
|
|
13
|
+
export const currentUrl = state(browser ? new URL(location.href) : null);
|
|
14
|
+
if (browser) {
|
|
15
|
+
const update = () => currentUrl.set(new URL(location.href));
|
|
16
|
+
// currententrychange covers links, back/forward and the page's own pushState/replaceState; without the
|
|
17
|
+
// Navigation API, only back/forward and #hash changes happen within a page.
|
|
18
|
+
if (navigation)
|
|
19
|
+
navigation.addEventListener('currententrychange', update);
|
|
20
|
+
else
|
|
21
|
+
for (const type of ['popstate', 'hashchange'])
|
|
22
|
+
addEventListener(type, update);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Go to `to`, as a link click would: the page's navigator swaps or renders it. Resolves once the new page is shown,
|
|
26
|
+
* and also when the navigation is cancelled or a newer one replaces it (so check `currentUrl` when it matters);
|
|
27
|
+
* rejects when it fails.
|
|
28
|
+
* @see https://clarusjs.com/guide/fragment-navigation
|
|
29
|
+
* @see https://clarusjs.com/guide/client-routes
|
|
30
|
+
*/
|
|
31
|
+
export async function navigate(to, options = {}) {
|
|
32
|
+
if (!browser)
|
|
33
|
+
return;
|
|
34
|
+
if (!navigation)
|
|
35
|
+
return options.replace ? location.replace(String(to)) : location.assign(String(to));
|
|
36
|
+
try {
|
|
37
|
+
await navigation.navigate(String(to), { history: options.replace ? 'replace' : 'auto', state: options.state }).finished;
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
if (err.name != 'AbortError')
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Stops the navigation in progress, like the browser's stop button: its requests are cancelled and, where the URL was
|
|
46
|
+
* held while loading (push and replace in browsers with precommit handlers), page and URL stay as they were. Hooks
|
|
47
|
+
* see it through `t.signal`. Like the stop button, it also stops the page's own loading (images still arriving).
|
|
48
|
+
* @see https://clarusjs.com/guide/navigation
|
|
49
|
+
*/
|
|
50
|
+
export function abort() {
|
|
51
|
+
if (browser)
|
|
52
|
+
stop();
|
|
53
|
+
}
|
|
54
|
+
/** Back one entry, like the browser's back button. */
|
|
55
|
+
export function back() {
|
|
56
|
+
if (browser)
|
|
57
|
+
history.back();
|
|
58
|
+
}
|
|
59
|
+
// A hook listens on the Navigation API for as long as the island that registered it (no-op without the API).
|
|
60
|
+
function hook(type, listener) {
|
|
61
|
+
if (!navigation)
|
|
62
|
+
return;
|
|
63
|
+
navigation.addEventListener(type, listener);
|
|
64
|
+
onDispose(() => navigation.removeEventListener(type, listener));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Before any navigation (links, forms, back/forward, `navigate()`): `nav.cancel()` stops it, for example to keep
|
|
68
|
+
* unsaved work. Back/forward can only be cancelled where the browser allows it.
|
|
69
|
+
* @see https://clarusjs.com/guide/navigation
|
|
70
|
+
*/
|
|
71
|
+
export function beforeNavigate(fn) {
|
|
72
|
+
hook('navigate', (e) => fn(new URL(e.destination.url), { type: e.navigationType, cancel: () => e.preventDefault() }));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* After a navigation within the page finished: the new page is in and its islands attached.
|
|
76
|
+
* @see https://clarusjs.com/guide/navigation
|
|
77
|
+
*/
|
|
78
|
+
export function afterNavigate(fn) {
|
|
79
|
+
hook('navigatesuccess', () => fn(new URL(location.href)));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A navigation failed (a fragment or a client page could not be loaded). The page then loads normally.
|
|
83
|
+
* @see https://clarusjs.com/guide/navigation
|
|
84
|
+
*/
|
|
85
|
+
export function onNavigateError(fn) {
|
|
86
|
+
// Where it was going: a navigation that failed before its URL changed (held while loading) never became current.
|
|
87
|
+
let to = '';
|
|
88
|
+
hook('navigate', (e) => { to = e.destination.url; });
|
|
89
|
+
hook('navigateerror', (e) => fn(new URL(to || location.href), e.error));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The navigation in progress, as a store (`let nav = shared(navigating)`): `null` when idle, else where it goes and
|
|
93
|
+
* its phase, `'loading'` (the next page is fetched) or `'swapping'` (it replaces this one). For CSS alone,
|
|
94
|
+
* `<html data-cl-navigating>` carries the phase.
|
|
95
|
+
* @see https://clarusjs.com/guide/navigation
|
|
96
|
+
*/
|
|
97
|
+
export const navigating = state(null);
|
|
98
|
+
const before = [];
|
|
99
|
+
const after = [];
|
|
100
|
+
// The page's navigator (the router or fragments, a separate script) reports each step here and waits for it.
|
|
101
|
+
if (browser) {
|
|
102
|
+
;
|
|
103
|
+
globalThis.__clNav = async (step, t) => {
|
|
104
|
+
navigating.set(step ? { ...t } : null);
|
|
105
|
+
if (step != 'swapping')
|
|
106
|
+
done?.(); // the new page is in (or the navigation ended): the transition animates
|
|
107
|
+
const hooks = step == 'swapping' ? before : step == 'swapped' ? after : [];
|
|
108
|
+
// Each hook is awaited in turn; one whose island was released meanwhile (by an earlier hook) is skipped.
|
|
109
|
+
for (const hook of [...hooks])
|
|
110
|
+
if (hooks.includes(hook))
|
|
111
|
+
await hook(t);
|
|
112
|
+
// A view transition: the browser snapshots this page, then the swap (which waits for this) runs inside it.
|
|
113
|
+
const start = document.startViewTransition;
|
|
114
|
+
if (step == 'swapping' && transitions && start) {
|
|
115
|
+
await new Promise((snapshot) => start.call(document, () => { snapshot(); return new Promise((r) => (done = r)); }));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
let transitions = false;
|
|
120
|
+
let done;
|
|
121
|
+
/**
|
|
122
|
+
* Page changes run as view transitions where the browser has them (`document.startViewTransition`), so CSS
|
|
123
|
+
* `::view-transition-*` rules animate them; elsewhere they stay instant. Call it once, from any island's script.
|
|
124
|
+
* `beforeSwap` hooks run before the browser's snapshot, `afterSwap` hooks once the new page is in.
|
|
125
|
+
* @see https://clarusjs.com/guide/navigation
|
|
126
|
+
*/
|
|
127
|
+
export function viewTransitions() {
|
|
128
|
+
transitions = true;
|
|
129
|
+
}
|
|
130
|
+
function inBetween(hooks, fn) {
|
|
131
|
+
if (!browser)
|
|
132
|
+
return;
|
|
133
|
+
hooks.push(fn);
|
|
134
|
+
onDispose(() => hooks.splice(hooks.indexOf(fn), 1));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Runs once the next page is loaded, before it replaces this one (animate out, save state). Return a promise to
|
|
138
|
+
* make the navigation wait; `t.signal` aborts when a newer navigation takes over. A hook that throws makes the page
|
|
139
|
+
* load normally.
|
|
140
|
+
* @see https://clarusjs.com/guide/navigation
|
|
141
|
+
*/
|
|
142
|
+
export function beforeSwap(fn) {
|
|
143
|
+
inBetween(before, fn);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Runs once the new page is in and its islands attached (animate in, move focus). Return a promise to make the
|
|
147
|
+
* navigation wait for it.
|
|
148
|
+
* @see https://clarusjs.com/guide/navigation
|
|
149
|
+
*/
|
|
150
|
+
export function afterSwap(fn) {
|
|
151
|
+
inBetween(after, fn);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A page's URL from its route, as its file names it, with the site's base: `href('/releases/[version]',
|
|
155
|
+
* { version: '0.8.0' })` is `'/releases/0.8.0'`. With .clarusjs/routes.d.ts (written by `clarusjs check`), a route
|
|
156
|
+
* that doesn't exist or a missing param is a type error. Params are encoded.
|
|
157
|
+
* @see https://clarusjs.com/guide/navigation
|
|
158
|
+
*/
|
|
159
|
+
export function href(route,
|
|
160
|
+
// Without the generated routes, params are optional; with them, required exactly when the route has some.
|
|
161
|
+
...params) {
|
|
162
|
+
const values = (params[0] ?? {});
|
|
163
|
+
const base = (import.meta.env?.BASE_URL ?? '/').replace(/\/$/, '');
|
|
164
|
+
return base + route.replace(/\[([^\]]+)\]/g, (_, key) => encodeURIComponent(values[key] ?? ''));
|
|
165
|
+
}
|
package/dist/runtime/router.d.ts
CHANGED
|
@@ -11,5 +11,12 @@ export declare function matchRoute(table: Entry[], pathname: string): {
|
|
|
11
11
|
params: Record<string, string>;
|
|
12
12
|
} | null;
|
|
13
13
|
/** Render the client route at `url` into the document. False when `url` is not a client route. */
|
|
14
|
-
export declare function show(url: URL, table: Entry[], load?: (src: string) => Promise<Page>, get?: (url: string) => Promise<Response
|
|
14
|
+
export declare function show(url: URL, table: Entry[], isCurrent?: () => Promise<boolean> | boolean, load?: (src: string) => Promise<Page>, get?: (url: string) => Promise<Response>): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* Puts a whole page in: its body, its own <html> attributes (lang, dir, …), its title (through `title`, else as
|
|
17
|
+
* written) and its inline head scripts (such as setting the theme before paint, run once as on a normal load); then
|
|
18
|
+
* attaches its islands (the loader also releases the old page's). The head stays. Resolves once the islands that
|
|
19
|
+
* attach right away are attached.
|
|
20
|
+
*/
|
|
21
|
+
export declare function swapIn(html: string, title?: (title: string) => string): Promise<void>;
|
|
15
22
|
export {};
|
package/dist/runtime/router.js
CHANGED
|
@@ -11,6 +11,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
11
11
|
// the island loader attach. Later navigations between client routes are intercepted with the Navigation API;
|
|
12
12
|
// browsers without it simply load pages normally.
|
|
13
13
|
import { parse } from './serialize.js';
|
|
14
|
+
import { latest, takeOver } from './navigate.js';
|
|
14
15
|
/** The route a path belongs to, with its decoded params. Same grammar as the server: whole [param] segments. */
|
|
15
16
|
export function matchRoute(table, pathname) {
|
|
16
17
|
const got = pathname.replace(/(.)\/+$/, '$1').split('/');
|
|
@@ -34,9 +35,10 @@ export function matchRoute(table, pathname) {
|
|
|
34
35
|
return null;
|
|
35
36
|
}
|
|
36
37
|
/** Render the client route at `url` into the document. False when `url` is not a client route. */
|
|
37
|
-
export async function show(url, table,
|
|
38
|
-
//
|
|
39
|
-
|
|
38
|
+
export async function show(url, table,
|
|
39
|
+
// Awaited right before the swap (where the in-between hooks run): false once a newer navigation has started,
|
|
40
|
+
// then this one must not touch the document.
|
|
41
|
+
isCurrent = () => true, load = (src) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ src)), get = fetch) {
|
|
40
42
|
const found = matchRoute(table, url.pathname);
|
|
41
43
|
if (!found)
|
|
42
44
|
return false;
|
|
@@ -49,60 +51,48 @@ isCurrent = () => true) {
|
|
|
49
51
|
return parse(await res.text());
|
|
50
52
|
}),
|
|
51
53
|
]);
|
|
52
|
-
if (!isCurrent())
|
|
54
|
+
if (!(await isCurrent()))
|
|
53
55
|
return false;
|
|
54
|
-
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
// The head stays (its stylesheets are every client page's, linked by the shell); the title gets the site's
|
|
57
|
+
// titleTemplate, from the shell (always set: '%s' without one).
|
|
58
|
+
await swapIn(page.render(props), (title) => document.getElementById('cl-routes').dataset.title.replace('%s', () => title));
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Puts a whole page in: its body, its own <html> attributes (lang, dir, …), its title (through `title`, else as
|
|
63
|
+
* written) and its inline head scripts (such as setting the theme before paint, run once as on a normal load); then
|
|
64
|
+
* attaches its islands (the loader also releases the old page's). The head stays. Resolves once the islands that
|
|
65
|
+
* attach right away are attached.
|
|
66
|
+
*/
|
|
67
|
+
export async function swapIn(html, title = (t) => t) {
|
|
68
|
+
const doc = new DOMParser().parseFromString(html, 'text/html');
|
|
58
69
|
const root = document.documentElement;
|
|
59
70
|
for (const { name } of [...root.attributes])
|
|
60
71
|
root.removeAttribute(name);
|
|
61
72
|
for (const { name, value } of [...doc.documentElement.attributes])
|
|
62
73
|
root.setAttribute(name, value);
|
|
74
|
+
document.title = title(doc.title); // before islands attach: the loader announces it
|
|
63
75
|
document.body.replaceWith(doc.body);
|
|
64
|
-
// The page's inline head scripts (such as setting the theme before paint) run once, as on a normal load.
|
|
65
76
|
for (const old of doc.head.querySelectorAll('script:not([src])')) {
|
|
66
77
|
const script = document.createElement('script');
|
|
67
78
|
script.textContent = old.textContent;
|
|
68
79
|
document.head.append(script);
|
|
69
80
|
script.remove();
|
|
70
81
|
}
|
|
71
|
-
|
|
72
|
-
;
|
|
73
|
-
globalThis.__clAttach?.(document.body);
|
|
74
|
-
return true;
|
|
82
|
+
await globalThis.__clAttach?.(document.body, 1); // 1: navigated (bytes)
|
|
75
83
|
}
|
|
76
|
-
const routes =
|
|
84
|
+
const routes = globalThis.document?.getElementById('cl-routes');
|
|
77
85
|
if (routes) {
|
|
78
|
-
const table = JSON.parse(routes.textContent
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (latest !== 1)
|
|
86
|
+
const table = JSON.parse(routes.textContent);
|
|
87
|
+
// The first render is current until a navigation is taken over.
|
|
88
|
+
show(new URL(location.href), table, () => !latest).catch((err) => {
|
|
89
|
+
if (latest)
|
|
83
90
|
return;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
let fallback = '';
|
|
89
|
-
const navigation = globalThis.navigation;
|
|
90
|
-
navigation?.addEventListener('navigate', (event) => {
|
|
91
|
-
const e = event;
|
|
92
|
-
if (!e.canIntercept || e.hashChange || e.downloadRequest != null || e.formData)
|
|
93
|
-
return;
|
|
94
|
-
const url = new URL(e.destination.url);
|
|
95
|
-
if (url.href === fallback || !matchRoute(table, url.pathname))
|
|
96
|
-
return;
|
|
97
|
-
const id = ++latest;
|
|
98
|
-
const isCurrent = () => latest === id && !e.signal.aborted;
|
|
99
|
-
e.intercept({
|
|
100
|
-
handler: () => show(url, table, undefined, undefined, isCurrent).then(() => { }, () => {
|
|
101
|
-
if (!isCurrent())
|
|
102
|
-
return;
|
|
103
|
-
fallback = url.href;
|
|
104
|
-
location.reload(); // the URL is already committed; assign() would stay in the page with a #hash
|
|
105
|
-
}),
|
|
106
|
-
});
|
|
91
|
+
// The site's 404 or 500 page, at this same URL (its title as written); plain text when the site has none.
|
|
92
|
+
fetch(routes.dataset.base + (/^404/.test(err.message) ? 404 : 500))
|
|
93
|
+
.then((res) => (res.ok ? res.text() : Promise.reject()))
|
|
94
|
+
.then((html) => swapIn(html), () => { document.body.textContent = 'Could not load page.'; });
|
|
107
95
|
});
|
|
96
|
+
// Later navigations to client routes.
|
|
97
|
+
takeOver((url) => matchRoute(table, url.pathname) && ((url, isCurrent, signal) => show(url, table, isCurrent, undefined, (src) => fetch(src, { signal }))));
|
|
108
98
|
}
|
package/dist/runtime/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { attrHtml } from './html.ts';
|
|
2
|
-
export { escape, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
2
|
+
export { escape, json, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
3
3
|
/**
|
|
4
4
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
5
5
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
package/dist/runtime/server.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Server-side helpers used by compiled render() functions. Runs at build time or per request, never in the browser.
|
|
2
2
|
import { attrHtml, escape } from './html.js';
|
|
3
3
|
import { stringify } from './serialize.js';
|
|
4
|
-
export { escape, blockHtml, forHtml, regionHtml } from './html.js';
|
|
4
|
+
export { escape, json, blockHtml, forHtml, regionHtml } from './html.js';
|
|
5
5
|
/**
|
|
6
6
|
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
7
7
|
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
package/dist/server/handler.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ModeTable } from './modes.ts';
|
|
2
|
+
import { type HeadOptions, type SiteOptions } from './head.ts';
|
|
2
3
|
type PageModule = {
|
|
3
4
|
render(props?: object): string;
|
|
4
5
|
};
|
|
@@ -15,6 +16,12 @@ type DataModule = {
|
|
|
15
16
|
}): object | Promise<object>;
|
|
16
17
|
};
|
|
17
18
|
/** '/src/pages/index.tscl' → '/', '/src/pages/blog/index.tscl' → '/blog', '/src/pages/about.md' → '/about' (any page source) */
|
|
19
|
+
/**
|
|
20
|
+
* A redirect for a server page's `load()` to return: `return redirect('/login')`. 302 by default; 301 or 308 for
|
|
21
|
+
* a page that moved for good, 303 after a form, 307 to keep the method.
|
|
22
|
+
* @see https://clarusjs.com/guide/routing
|
|
23
|
+
*/
|
|
24
|
+
export declare function redirect(to: string, status?: 301 | 302 | 303 | 307 | 308): Response;
|
|
18
25
|
export declare function routeFromFile(file: string): string;
|
|
19
26
|
export declare function createHandler(pages: Record<string, () => Promise<PageModule>>, options?: {
|
|
20
27
|
loaderUrl?: string;
|
|
@@ -33,6 +40,12 @@ export declare function createHandler(pages: Record<string, () => Promise<PageMo
|
|
|
33
40
|
stylesUrl?: string;
|
|
34
41
|
/** Stylesheets each page imports (directly or through its components), by route as its file names it. */
|
|
35
42
|
css?: Record<string, string[]>;
|
|
43
|
+
/** Title template, site-wide head tags and a transform (see head.ts). */
|
|
44
|
+
head?: HeadOptions;
|
|
45
|
+
/** The site's public address: canonical URLs and social cards (see head.ts). */
|
|
46
|
+
site?: SiteOptions;
|
|
47
|
+
/** Development: a failing page throws its error (the dev server shows it with its stack) instead of answering 500. */
|
|
48
|
+
dev?: boolean;
|
|
36
49
|
}): {
|
|
37
50
|
fetch(request: Request): Promise<Response>;
|
|
38
51
|
routes: string[];
|
|
@@ -46,4 +59,9 @@ export declare function createHandler(pages: Record<string, () => Promise<PageMo
|
|
|
46
59
|
* its <body> when nothing is marked. Comments are ignored when looking; nested tags of the same name are balanced.
|
|
47
60
|
*/
|
|
48
61
|
export declare function toFragment(html: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* The 404 page is served at any depth (/a/b/c), so its relative links (href, src) are resolved from the site root:
|
|
64
|
+
* 'guide' means <base>guide wherever the visitor is.
|
|
65
|
+
*/
|
|
66
|
+
export declare function rootLinks(html: string, base: string): string;
|
|
49
67
|
export {};
|