netjes 0.0.4 → 0.0.5
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 +8 -8
- package/cli/commands.mjs +32 -6
- package/dist/actions.d.ts +3 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +5 -4
- package/dist/actions.js.map +1 -1
- package/dist/document.d.ts +3 -1
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +4 -3
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lang.d.ts +10 -0
- package/dist/lang.d.ts.map +1 -0
- package/dist/lang.js +26 -0
- package/dist/lang.js.map +1 -0
- package/dist/link.d.ts +15 -3
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +15 -9
- package/dist/link.js.map +1 -1
- package/dist/mounted.d.ts +3 -7
- package/dist/mounted.d.ts.map +1 -1
- package/dist/mounted.js.map +1 -1
- package/dist/navigation-guard.d.ts +59 -0
- package/dist/navigation-guard.d.ts.map +1 -0
- package/dist/navigation-guard.js +116 -0
- package/dist/navigation-guard.js.map +1 -0
- package/dist/payload.d.ts +20 -2
- package/dist/payload.d.ts.map +1 -1
- package/dist/payload.js +25 -1
- package/dist/payload.js.map +1 -1
- package/dist/root.d.ts.map +1 -1
- package/dist/root.js +2 -1
- package/dist/root.js.map +1 -1
- package/dist/router.d.ts +101 -20
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +590 -68
- package/dist/router.js.map +1 -1
- package/dist/schema.d.ts +2 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +5 -0
- package/dist/schema.js.map +1 -1
- package/dist/testing.d.ts +17 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +63 -22
- package/dist/testing.js.map +1 -1
- package/package.json +5 -1
- package/sidecar/server.mjs +80 -14
- package/skills/netjes/SKILL.md +2 -2
- package/skills/netjes/references/data-and-actions.md +5 -4
- package/skills/netjes/references/deployment.md +1 -1
- package/skills/netjes/references/routing.md +9 -5
- package/skills/netjes/references/testing.md +6 -3
- package/src/actions.tsx +5 -4
- package/src/document.tsx +11 -4
- package/src/index.ts +9 -0
- package/src/lang.ts +28 -0
- package/src/link.tsx +37 -9
- package/src/mounted.tsx +3 -3
- package/src/navigation-guard.ts +136 -0
- package/src/payload.ts +37 -3
- package/src/root.tsx +3 -1
- package/src/router.tsx +764 -104
- package/src/schema.ts +11 -0
- package/src/testing-vitest.ts +1 -1
- package/src/testing.tsx +67 -15
- package/template/Dockerfile +1 -1
- package/template/README.md +1 -1
- package/template/_AGENTS.md +4 -2
- package/template/docker-compose.override.yml +15 -3
- package/template/generated/netjes.ts +1 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/** How a navigation a guard holds would go: adding or replacing an entry, or through history, either way. */
|
|
2
|
+
export type NavigationAttemptType = 'push' | 'replace' | 'back' | 'forward';
|
|
3
|
+
|
|
4
|
+
/** A navigation `useNavigationGuard` holds: where it goes, how, and the two answers to it. */
|
|
5
|
+
export type NavigationAttempt = {
|
|
6
|
+
/** Where it goes: the path, query and fragment, without the base path, as `usePathname()` and the routes read them. */
|
|
7
|
+
to: string;
|
|
8
|
+
type: NavigationAttemptType;
|
|
9
|
+
/** Lets it go on: to the next guard that holds, if any, then to the page. */
|
|
10
|
+
proceed(): void;
|
|
11
|
+
/** Keeps the page on screen: the navigation does not happen. */
|
|
12
|
+
stay(): void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** What `useNavigationGuard` takes. */
|
|
16
|
+
export type NavigationGuardOptions = {
|
|
17
|
+
/** Whether to hold navigations now: a form with changes that are not saved, files on their way up. */
|
|
18
|
+
when: boolean;
|
|
19
|
+
/** Called with each navigation held, which waits until it calls `proceed()` or `stay()`. */
|
|
20
|
+
onAttempt(attempt: NavigationAttempt): void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Guard = { read(): NavigationGuardOptions };
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The guards of one router: the ones that hold now, in the order they started to, and the attempt being asked about.
|
|
27
|
+
* Each navigation a guard holds asks the guards in turn, the one that started to hold last first, and runs once every
|
|
28
|
+
* one of them has let it proceed. A newer attempt takes the place of one still being asked about, so a guard that never
|
|
29
|
+
* answers keeps no navigation from being asked again, and so does a navigation that goes at once because no guard holds
|
|
30
|
+
* any more: the earlier attempt's answers then do nothing.
|
|
31
|
+
*
|
|
32
|
+
* While any guard holds, `beforeunload` has the browser ask its own question before the document unloads: a reload,
|
|
33
|
+
* closing the tab, typing an address, a link to another site. The listener is there only then, as such a listener has
|
|
34
|
+
* kept pages out of some browsers' back-forward cache.
|
|
35
|
+
*/
|
|
36
|
+
export class NavigationGuards {
|
|
37
|
+
private readonly guards: Guard[] = [];
|
|
38
|
+
/** The attempt being asked about: dropping it ends it, as a stay does. */
|
|
39
|
+
private asking: { drop(): void } | null = null;
|
|
40
|
+
/** The next unload is a full page load the guards let through: the browser does not ask about it again. */
|
|
41
|
+
private unloadLetThrough = false;
|
|
42
|
+
|
|
43
|
+
private readonly onBeforeUnload = (event: BeforeUnloadEvent) => {
|
|
44
|
+
if (this.unloadLetThrough) {
|
|
45
|
+
this.unloadLetThrough = false;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
// What browsers from before preventDefault() asked for.
|
|
50
|
+
event.returnValue = true;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** Whether any guard holds now. */
|
|
54
|
+
get holding(): boolean {
|
|
55
|
+
return this.guards.length > 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Holds navigations until the function it returns is called; `read` gives the latest options. */
|
|
59
|
+
register(read: () => NavigationGuardOptions): () => void {
|
|
60
|
+
const guard: Guard = { read };
|
|
61
|
+
this.guards.push(guard);
|
|
62
|
+
if (this.guards.length === 1) {
|
|
63
|
+
this.unloadLetThrough = false;
|
|
64
|
+
window.addEventListener('beforeunload', this.onBeforeUnload);
|
|
65
|
+
}
|
|
66
|
+
return () => {
|
|
67
|
+
const at = this.guards.indexOf(guard);
|
|
68
|
+
if (at < 0) return;
|
|
69
|
+
this.guards.splice(at, 1);
|
|
70
|
+
if (this.guards.length === 0) window.removeEventListener('beforeunload', this.onBeforeUnload);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A navigation the guards let through is about to load a document (the router hands it to the browser): the
|
|
76
|
+
* `beforeunload` that follows does not ask again. Only that one.
|
|
77
|
+
*/
|
|
78
|
+
letUnloadThrough(): void {
|
|
79
|
+
if (this.guards.length > 0) this.unloadLetThrough = true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Drops the attempt still waiting for its answer, whose answers then do nothing: Back or Forward to another page took
|
|
84
|
+
* its place, whether a guard holds that step (and asks about it once its entry is put back) or it went on.
|
|
85
|
+
*/
|
|
86
|
+
dropAttempt(): void {
|
|
87
|
+
this.asking?.drop();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Takes the place of the attempt still waiting for its answer, if any. Then runs `go` at once while no guard holds;
|
|
92
|
+
* else asks the guards that hold, newest first, and runs it once each has let it proceed. Resolves with `go`'s
|
|
93
|
+
* promise, or once a guard keeps the page or a newer attempt takes its place.
|
|
94
|
+
*/
|
|
95
|
+
attempt(to: string, type: NavigationAttemptType, go: () => Promise<void>): Promise<void> {
|
|
96
|
+
this.asking?.drop();
|
|
97
|
+
if (this.guards.length === 0) return go();
|
|
98
|
+
return new Promise<void>((resolve) => {
|
|
99
|
+
const queue = [...this.guards].reverse();
|
|
100
|
+
const asking = {
|
|
101
|
+
drop: () => {
|
|
102
|
+
if (this.asking === asking) this.asking = null;
|
|
103
|
+
resolve();
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
this.asking = asking;
|
|
107
|
+
const next = () => {
|
|
108
|
+
if (this.asking !== asking) return;
|
|
109
|
+
// A guard that stopped holding while an earlier one was asked has nothing to say any more.
|
|
110
|
+
let guard = queue.shift();
|
|
111
|
+
while (guard && !this.guards.includes(guard)) guard = queue.shift();
|
|
112
|
+
if (!guard) {
|
|
113
|
+
this.asking = null;
|
|
114
|
+
resolve(go());
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let answered = false;
|
|
118
|
+
guard.read().onAttempt({
|
|
119
|
+
to,
|
|
120
|
+
type,
|
|
121
|
+
proceed: () => {
|
|
122
|
+
if (answered) return;
|
|
123
|
+
answered = true;
|
|
124
|
+
next();
|
|
125
|
+
},
|
|
126
|
+
stay: () => {
|
|
127
|
+
if (answered) return;
|
|
128
|
+
answered = true;
|
|
129
|
+
asking.drop();
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
next();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
package/src/payload.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isLanguageTag } from './lang.ts';
|
|
1
2
|
import type { Metadata } from './metadata.tsx';
|
|
2
3
|
|
|
3
4
|
// The JSON contract with the .NET side (src/Netjes/Rendering/PagePayload.cs). Keep the two in sync.
|
|
@@ -12,6 +13,11 @@ export const ANTIFORGERY_HEADER = 'X-Netjes-Antiforgery';
|
|
|
12
13
|
* pages the router keeps for back and forward show the app as it was before.
|
|
13
14
|
*/
|
|
14
15
|
export const COOKIES_HEADER = 'X-Netjes-Cookies';
|
|
16
|
+
/**
|
|
17
|
+
* `1` on the router's request for the URL a redirect sent it to (a redirect payload, or a server action's redirect):
|
|
18
|
+
* where no page answers that URL, the server answers `{ kind: 'document' }` without running the endpoint.
|
|
19
|
+
*/
|
|
20
|
+
export const REDIRECTED_HEADER = 'X-Netjes-Redirected';
|
|
15
21
|
export const DATA_ELEMENT_ID = '__NETJES_DATA__';
|
|
16
22
|
/** The id of the empty element that ends the body of every server-rendered document, and so the shell. */
|
|
17
23
|
export const SHELL_END_ID = '__NETJES_SHELL_END__';
|
|
@@ -46,6 +52,8 @@ export type SegmentRef = {
|
|
|
46
52
|
depth?: number | undefined;
|
|
47
53
|
/** Each slot page's own route values (its parameter names can differ from the page's). */
|
|
48
54
|
slotParams?: Record<string, Params> | undefined;
|
|
55
|
+
/** The layout loader's `PageMetadata.Lang`. A reused segment's is the one the client holds, as its props. */
|
|
56
|
+
lang?: string | undefined;
|
|
49
57
|
};
|
|
50
58
|
|
|
51
59
|
export type SpecialPageRef = { page: string; segments: SegmentRef[] };
|
|
@@ -68,9 +76,14 @@ export type AntiforgeryToken = {
|
|
|
68
76
|
/** A TanStack Query cache entry seeded by the C# loader (`PageResult.WithQuery`). `updatedAt` is Unix milliseconds. */
|
|
69
77
|
export type SeededQuery = { key: readonly unknown[]; data: unknown; updatedAt: number };
|
|
70
78
|
|
|
71
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Left by a form post that failed (a page action's, or a server action's posted without JavaScript); the form's page
|
|
81
|
+
* renders with it. `status` is 422 after a failed validation and 429 after the rate limiter refused the post
|
|
82
|
+
* (`Page.TooManyRequests`); 422 when absent.
|
|
83
|
+
*/
|
|
72
84
|
export type ActionStateRef = {
|
|
73
85
|
action: string;
|
|
86
|
+
status?: number;
|
|
74
87
|
errors: Record<string, string[]>;
|
|
75
88
|
values: Record<string, string | string[]>;
|
|
76
89
|
};
|
|
@@ -115,6 +128,8 @@ export type PagePayload = {
|
|
|
115
128
|
intercepts?: InterceptRef[];
|
|
116
129
|
/** From the C# loader (`.WithMetadata(...)`), merged over the files' `export const metadata`. */
|
|
117
130
|
metadata?: Metadata;
|
|
131
|
+
/** The page loader's `PageMetadata.Lang`: the document's lang, before its layouts' and the root's (`documentLang`). */
|
|
132
|
+
lang?: string;
|
|
118
133
|
/** The page was rendered in draft mode. */
|
|
119
134
|
draftMode?: boolean;
|
|
120
135
|
/** `UsePathBase`'s base path, when the app is not mounted at the root; `url` includes it. */
|
|
@@ -160,7 +175,10 @@ export type InitialPayload = PagePayload & { document: DocumentInfo };
|
|
|
160
175
|
|
|
161
176
|
export type RedirectPayload = { kind: 'redirect'; location: string; version: string };
|
|
162
177
|
|
|
163
|
-
|
|
178
|
+
/** The answer to a request a redirect led to (`REDIRECTED_HEADER`) when no page answers its URL: load it as a document. */
|
|
179
|
+
export type LoadDocumentPayload = { kind: 'document' };
|
|
180
|
+
|
|
181
|
+
export type NavigationPayload = PagePayload | RedirectPayload | LoadDocumentPayload;
|
|
164
182
|
|
|
165
183
|
export class PayloadError extends Error {
|
|
166
184
|
override name = 'PayloadError';
|
|
@@ -221,6 +239,13 @@ function optionalString(value: unknown, what: string): string | undefined {
|
|
|
221
239
|
return value === undefined || value === null ? undefined : string(value, what);
|
|
222
240
|
}
|
|
223
241
|
|
|
242
|
+
/** A language tag's shape (`PageMetadata.Lang`), since the router writes it on `<html>`. */
|
|
243
|
+
function languageTag(value: unknown, what: string): string {
|
|
244
|
+
const tag = string(value, what);
|
|
245
|
+
if (!isLanguageTag(tag)) fail(`${what} must be a language tag, such as nl or en-GB`);
|
|
246
|
+
return tag;
|
|
247
|
+
}
|
|
248
|
+
|
|
224
249
|
function segments(value: unknown, what: string): SegmentRef[] {
|
|
225
250
|
if (!Array.isArray(value)) fail(`${what} must be an array`);
|
|
226
251
|
return value.map((item, i) => {
|
|
@@ -234,6 +259,7 @@ function segments(value: unknown, what: string): SegmentRef[] {
|
|
|
234
259
|
const key = optionalString(json.key, `${what}[${i}].key`);
|
|
235
260
|
if (key !== undefined) segment.key = key;
|
|
236
261
|
if (json.reused === true) segment.reused = true;
|
|
262
|
+
if (json.lang !== undefined && json.lang !== null) segment.lang = languageTag(json.lang, `${what}[${i}].lang`);
|
|
237
263
|
if (json.depth !== undefined && json.depth !== null) {
|
|
238
264
|
if (typeof json.depth !== 'number' || !Number.isInteger(json.depth) || json.depth < 0) {
|
|
239
265
|
throw new PayloadError(`${what}[${i}].depth must be a whole number`);
|
|
@@ -268,7 +294,12 @@ function actionState(value: unknown): ActionStateRef {
|
|
|
268
294
|
for (const [key, item] of Object.entries(object(json.values ?? {}, 'actionState.values'))) {
|
|
269
295
|
values[key] = Array.isArray(item) ? strings(item, `actionState.values.${key}`) : string(item, `actionState.values.${key}`);
|
|
270
296
|
}
|
|
271
|
-
return {
|
|
297
|
+
return {
|
|
298
|
+
action: string(json.action, 'actionState.action'),
|
|
299
|
+
...(json.status !== undefined && json.status !== null ? { status: number(json.status, 'actionState.status') } : {}),
|
|
300
|
+
errors: errorMap(json.errors ?? {}, 'actionState.errors'),
|
|
301
|
+
values,
|
|
302
|
+
};
|
|
272
303
|
}
|
|
273
304
|
|
|
274
305
|
export function parseActionResponse(value: unknown): ActionResponse {
|
|
@@ -332,6 +363,7 @@ function pagePayload(json: Json): PagePayload {
|
|
|
332
363
|
});
|
|
333
364
|
}
|
|
334
365
|
if (json.metadata !== undefined && json.metadata !== null) payload.metadata = object(json.metadata, 'metadata') as Metadata;
|
|
366
|
+
if (json.lang !== undefined && json.lang !== null) payload.lang = languageTag(json.lang, 'lang');
|
|
335
367
|
if (json.pageKind !== undefined && json.pageKind !== null) {
|
|
336
368
|
if (!PAGE_KINDS.includes(json.pageKind as PageKind)) fail(`unknown pageKind ${JSON.stringify(json.pageKind)}`);
|
|
337
369
|
payload.pageKind = json.pageKind as PageKind;
|
|
@@ -379,6 +411,8 @@ export function parseNavigationPayload(value: unknown): NavigationPayload {
|
|
|
379
411
|
return pagePayload(json);
|
|
380
412
|
case 'redirect':
|
|
381
413
|
return { kind: 'redirect', location: string(json.location, 'location'), version: string(json.version, 'version') };
|
|
414
|
+
case 'document':
|
|
415
|
+
return { kind: 'document' };
|
|
382
416
|
default:
|
|
383
417
|
return fail(`unknown kind ${JSON.stringify(json.kind)}`);
|
|
384
418
|
}
|
package/src/root.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
|
|
|
2
2
|
import { QueryShell } from 'virtual:netjes/query';
|
|
3
3
|
import { Document } from './document.tsx';
|
|
4
4
|
import { ImageConfigProvider } from './image.tsx';
|
|
5
|
+
import { documentLang } from './lang.ts';
|
|
5
6
|
import { NonceProvider } from './nonce.tsx';
|
|
6
7
|
import { PublicConfigProvider } from './public-config.tsx';
|
|
7
8
|
import type { ModuleLoader, RouteModules } from './modules.ts';
|
|
@@ -24,13 +25,14 @@ function RootProviders({ info, children }: { info: DocumentInfo; children: React
|
|
|
24
25
|
/** The same tree on the server and in the browser, so hydration matches. */
|
|
25
26
|
export function Root({ payload, modules, loader, dataJson }: RootProps) {
|
|
26
27
|
return (
|
|
27
|
-
<Document info={payload.document} dataJson={dataJson}>
|
|
28
|
+
<Document info={payload.document} lang={documentLang(payload, payload.document.lang)} dataJson={dataJson}>
|
|
28
29
|
<RootProviders info={payload.document}>
|
|
29
30
|
<RouterProvider
|
|
30
31
|
initial={payload}
|
|
31
32
|
initialModules={modules}
|
|
32
33
|
loader={loader}
|
|
33
34
|
errorPage={payload.document.errorPage}
|
|
35
|
+
documentLang={payload.document.lang}
|
|
34
36
|
shell={QueryShell}
|
|
35
37
|
/>
|
|
36
38
|
</RootProviders>
|