@opsydyn/astro-foldkit 0.11.0 → 0.12.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.
- package/CHANGELOG.md +12 -2
- package/README.md +42 -2
- package/dist/directive-interaction.d.mts +90 -0
- package/dist/directive-interaction.mjs +197 -0
- package/dist/index.d.mts +12 -1
- package/dist/index.mjs +30 -2
- package/dist/server.mjs +6 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.12.0](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.11.0...astro-foldkit-v0.12.0) (2026-11-12)
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
- **astro-foldkit:** add a client:interaction directive that replays early input ([9e2afa8](https://github.com/opsydyn/fold-kit-experiments/commit/9e2afa848cefa0accb1cc58767b1389c652239df))
|
|
8
|
+
- **astro-foldkit:** add a client:interaction directive that replays early input ([0ddee8a](https://github.com/opsydyn/fold-kit-experiments/commit/0ddee8a79f8b84ea4fdfb097524faa873830da2f))
|
|
9
|
+
|
|
10
|
+
### Bug Fixes
|
|
11
|
+
|
|
12
|
+
- **astro-foldkit:** pre-bundle island dependencies before the first request ([13c8a73](https://github.com/opsydyn/fold-kit-experiments/commit/13c8a73c46b4458d4513a10e26fc4780e05e2838))
|
|
13
|
+
- **astro-foldkit:** pre-bundle island dependencies before the first request ([a5b52c1](https://github.com/opsydyn/fold-kit-experiments/commit/a5b52c18e2207af6d71e2e4c58032ec720b2172e))
|
|
14
|
+
|
|
3
15
|
## [0.11.0](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.10.0...astro-foldkit-v0.11.0) (2026-11-12)
|
|
4
16
|
|
|
5
17
|
### Features
|
|
6
18
|
|
|
7
|
-
- **astro-foldkit:** warn about islands that fail silently ([166dc12](https://github.com/opsydyn/fold-kit-experiments/commit/166dc12f752ccb1ec499a18196b40600d0d481dd))
|
|
8
19
|
- **astro-foldkit:** warn about islands that fail silently ([232e940](https://github.com/opsydyn/fold-kit-experiments/commit/232e94023d5181e049d6d964a93220d364d06fff))
|
|
9
20
|
|
|
10
21
|
## [0.10.0](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.9.3...astro-foldkit-v0.10.0) (2026-11-11)
|
|
11
22
|
|
|
12
23
|
### Features
|
|
13
24
|
|
|
14
|
-
- **astro-foldkit:** let an island pre-render its first paint ([46011d0](https://github.com/opsydyn/fold-kit-experiments/commit/46011d037531541d405a86b1d4079114fe547476))
|
|
15
25
|
- **astro-foldkit:** let an island pre-render its first paint ([ae06ae9](https://github.com/opsydyn/fold-kit-experiments/commit/ae06ae9df22a8cbf094cfe3a9a28ef48da5cf51d))
|
|
16
26
|
|
|
17
27
|
## [0.9.3](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.9.2...astro-foldkit-v0.9.3) (2026-10-29)
|
package/README.md
CHANGED
|
@@ -161,6 +161,7 @@ appears in the HTML, and whether it ships JavaScript. Make them per island.
|
|
|
161
161
|
| --------------------------- | ------------------------------------------------- | ---------------------------- |
|
|
162
162
|
| To be read, never touched | `prerender: true`, **no directive** | none for this island |
|
|
163
163
|
| Interaction, below the fold | `prerender: true`, `client:visible` | none until it is scrolled to |
|
|
164
|
+
| Interaction, by a few | `prerender: true`, `client:interaction` | none until it is touched |
|
|
164
165
|
| Interaction, immediately | `prerender: true`, `client:load` or `client:idle` | the runtime, at load |
|
|
165
166
|
| A browser-only `init` | no `prerender`, `client:load` or `client:idle` | the runtime, at load |
|
|
166
167
|
|
|
@@ -190,13 +191,52 @@ renders before any JavaScript runs. A chart that loses its hover readout by
|
|
|
190
191
|
going static can keep the information as a native SVG `<title>` on each mark,
|
|
191
192
|
which the browser shows as a tooltip and which costs no runtime.
|
|
192
193
|
|
|
193
|
-
|
|
194
|
+
#### `client:interaction`
|
|
195
|
+
|
|
196
|
+
The integration adds a `client:interaction` directive. It hydrates a
|
|
197
|
+
pre-rendered island on the reader's first pointer press, focus or key press
|
|
198
|
+
inside it, and replays what they did before it booted:
|
|
199
|
+
|
|
200
|
+
```astro
|
|
201
|
+
<Counter client:interaction />
|
|
202
|
+
<Counter client:interaction={{ hover: true }} />
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
FoldKit renders an island fresh rather than adopting the pre-rendered markup,
|
|
206
|
+
so without help the click that started the download would be lost, and text
|
|
207
|
+
typed into the static markup would disappear in the swap. The directive records
|
|
208
|
+
clicks, field edits and focus inside the island, in order, and replays them
|
|
209
|
+
onto the matching elements of the first client render: a field edit arrives as
|
|
210
|
+
`input` and `change` events, a click as a click, and focus and the caret are
|
|
211
|
+
restored. Elements are matched by position, which holds because server and
|
|
212
|
+
client run the same pure `init` and `view`. A step whose element has changed,
|
|
213
|
+
or is now disabled, is skipped. A run of edits to one field replays as its
|
|
214
|
+
final value. With `{ hover: true }` a mouse pointer over the island also starts
|
|
215
|
+
the download, which usually finishes before the click.
|
|
216
|
+
|
|
217
|
+
The pre-rendered markup never reacts, so replay is faithful only up to the
|
|
218
|
+
first point where the application would have changed the DOM. Typing into a
|
|
219
|
+
field, pressing a button that clears it, then typing again replays the button
|
|
220
|
+
press correctly but leaves the field holding everything typed.
|
|
221
|
+
|
|
222
|
+
Choose it over `client:visible` only when most readers who see the island never
|
|
223
|
+
use it, and its controls work in the pre-rendered markup. Every interaction
|
|
224
|
+
waits for the download first. On the report page above, under Slow 4G and a
|
|
225
|
+
4× CPU slowdown on a phone viewport, the grid's search was usable 1.65 s after
|
|
226
|
+
the tap with `client:interaction`, against immediately with `client:visible`,
|
|
227
|
+
which had already spent 172 KB while the reader scrolled. That grid also keeps
|
|
228
|
+
its controls disabled until its rows arrive, so the reader's first tap does
|
|
229
|
+
nothing but start the download: `client:visible` is the better choice there.
|
|
230
|
+
|
|
231
|
+
The renderer warns at build and dev time about the arrangements that misbehave
|
|
194
232
|
without an error, each once per component:
|
|
195
233
|
|
|
196
234
|
- an island with **no directive and no `prerender`** renders an empty mount and
|
|
197
235
|
ships nothing, so it never displays anything;
|
|
198
236
|
- an island with **`client:visible` and no `prerender`** never intersects, for
|
|
199
|
-
the reason above, so it never hydrates
|
|
237
|
+
the reason above, so it never hydrates;
|
|
238
|
+
- an island with **`client:interaction` and no `prerender`** has no markup to
|
|
239
|
+
interact with, so it hydrates at once, as `client:load` would.
|
|
200
240
|
|
|
201
241
|
FoldKit replaces the inner mount, not the outer `<astro-island>`. The outer
|
|
202
242
|
element remains Astro's lifecycle owner, including for empty `client:only`
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
//#region src/directive-interaction.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* `client:interaction`: hydrate a pre-rendered FoldKit island on the reader's
|
|
4
|
+
* first sign of intent rather than at load or on sight, and replay what they
|
|
5
|
+
* did before it booted.
|
|
6
|
+
*
|
|
7
|
+
* FoldKit's `embed` renders fresh: it replaces the pre-rendered markup instead
|
|
8
|
+
* of adopting it. A click that triggers the download would otherwise be lost,
|
|
9
|
+
* and anything typed or focused before the swap would vanish with the old DOM.
|
|
10
|
+
* So the directive records clicks, edited form values and focus inside the
|
|
11
|
+
* island while the application loads, and replays them onto the matching
|
|
12
|
+
* elements of the first client render. The match is by position: server and
|
|
13
|
+
* client run the same pure `init` and `view` over the same props, so the two
|
|
14
|
+
* trees have the same shape.
|
|
15
|
+
*
|
|
16
|
+
* Astro bundles this module on its own into an inline script, so it imports
|
|
17
|
+
* nothing.
|
|
18
|
+
*/
|
|
19
|
+
/** Options for `client:interaction`. */
|
|
20
|
+
type InteractionOptions = Readonly<{
|
|
21
|
+
/**
|
|
22
|
+
* Also hydrate when a mouse pointer moves over the island. Hover usually
|
|
23
|
+
* comes well before a click, so the application is often ready by then, at
|
|
24
|
+
* the cost of loading for readers who only pass over it.
|
|
25
|
+
*/
|
|
26
|
+
hover?: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
type TreeNode = {
|
|
29
|
+
readonly tagName: string;
|
|
30
|
+
readonly parentElement: TreeNode | null;
|
|
31
|
+
readonly children: ArrayLike<TreeNode>;
|
|
32
|
+
};
|
|
33
|
+
/** Where an element sits under a root: the child index at each level. */
|
|
34
|
+
type Position = Readonly<{
|
|
35
|
+
path: ReadonlyArray<number>;
|
|
36
|
+
tagName: string;
|
|
37
|
+
}>;
|
|
38
|
+
declare const positionOf: (root: TreeNode, target: TreeNode) => Position | undefined;
|
|
39
|
+
/** The element at `position` under `root`, if it is still the same kind of element. */
|
|
40
|
+
declare const resolvePosition: (root: TreeNode, position: Position) => TreeNode | undefined;
|
|
41
|
+
/** One thing the reader did before the application booted. */
|
|
42
|
+
type Step = Readonly<{
|
|
43
|
+
kind: 'click';
|
|
44
|
+
position: Position;
|
|
45
|
+
}> | Readonly<{
|
|
46
|
+
kind: 'value';
|
|
47
|
+
position: Position;
|
|
48
|
+
value: string;
|
|
49
|
+
}>;
|
|
50
|
+
/** What the reader did inside the island before the application booted, in order. */
|
|
51
|
+
type Recording = Readonly<{
|
|
52
|
+
steps: ReadonlyArray<Step>;
|
|
53
|
+
focus: Readonly<{
|
|
54
|
+
position: Position;
|
|
55
|
+
selection?: readonly [number, number] | undefined;
|
|
56
|
+
}> | undefined;
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* Adds a step, folding a run of edits to one field into its latest value: the
|
|
60
|
+
* application needs where the reader got to, not every keystroke.
|
|
61
|
+
*/
|
|
62
|
+
declare const appendStep: (steps: ReadonlyArray<Step>, step: Step) => ReadonlyArray<Step>;
|
|
63
|
+
/**
|
|
64
|
+
* Replays a recording onto the freshly rendered application under `root`, in
|
|
65
|
+
* the order the reader acted, then restores focus. A value reaches the
|
|
66
|
+
* application as `input` and `change` events, as typing would.
|
|
67
|
+
*
|
|
68
|
+
* Steps whose element no longer matches, or is disabled, are skipped: a
|
|
69
|
+
* control the application has not enabled must not receive input the reader
|
|
70
|
+
* could not have given it. The pre-rendered markup never reacted to the
|
|
71
|
+
* reader, so a field the application would have changed between two edits
|
|
72
|
+
* holds the reader's text as typed into the static field.
|
|
73
|
+
*/
|
|
74
|
+
declare const replay: (root: TreeNode, recording: Recording) => void;
|
|
75
|
+
/**
|
|
76
|
+
* The application root that replaced `oldRoot`. FoldKit renders its root in
|
|
77
|
+
* place of the mount element; should it ever render inside the mount instead,
|
|
78
|
+
* the mount is still in its slot and the root is its first child.
|
|
79
|
+
*/
|
|
80
|
+
declare const currentRoot: <Node extends TreeNode>(slot: Readonly<{
|
|
81
|
+
parent: Node;
|
|
82
|
+
index: number;
|
|
83
|
+
mount: Node;
|
|
84
|
+
}>) => Node | undefined;
|
|
85
|
+
type DirectiveLoad = () => Promise<() => Promise<void>>;
|
|
86
|
+
declare const interactionDirective: (load: DirectiveLoad, options: {
|
|
87
|
+
readonly value: unknown;
|
|
88
|
+
}, el: HTMLElement) => void;
|
|
89
|
+
//#endregion
|
|
90
|
+
export { InteractionOptions, Position, Recording, Step, TreeNode, appendStep, currentRoot, interactionDirective as default, positionOf, replay, resolvePosition };
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
//#region src/directive-interaction.ts
|
|
2
|
+
const positionOf = (root, target) => {
|
|
3
|
+
const path = [];
|
|
4
|
+
let node = target;
|
|
5
|
+
while (node !== root) {
|
|
6
|
+
const parent = node.parentElement;
|
|
7
|
+
if (parent === null) return void 0;
|
|
8
|
+
path.unshift(Array.prototype.indexOf.call(parent.children, node));
|
|
9
|
+
node = parent;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
path,
|
|
13
|
+
tagName: target.tagName
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/** The element at `position` under `root`, if it is still the same kind of element. */
|
|
17
|
+
const resolvePosition = (root, position) => {
|
|
18
|
+
let node = root;
|
|
19
|
+
for (const index of position.path) {
|
|
20
|
+
node = node.children[index];
|
|
21
|
+
if (node === void 0) return void 0;
|
|
22
|
+
}
|
|
23
|
+
return node.tagName === position.tagName ? node : void 0;
|
|
24
|
+
};
|
|
25
|
+
const samePosition = (a, b) => a.tagName === b.tagName && a.path.length === b.path.length && a.path.every((index, level) => index === b.path[level]);
|
|
26
|
+
/**
|
|
27
|
+
* Adds a step, folding a run of edits to one field into its latest value: the
|
|
28
|
+
* application needs where the reader got to, not every keystroke.
|
|
29
|
+
*/
|
|
30
|
+
const appendStep = (steps, step) => {
|
|
31
|
+
const last = steps.at(-1);
|
|
32
|
+
return step.kind === "value" && last?.kind === "value" && samePosition(last.position, step.position) ? [...steps.slice(0, -1), step] : [...steps, step];
|
|
33
|
+
};
|
|
34
|
+
const dispatch = (target, type) => {
|
|
35
|
+
target.dispatchEvent(new Event(type, { bubbles: true }));
|
|
36
|
+
};
|
|
37
|
+
const replayStep = (root, step) => {
|
|
38
|
+
const target = resolvePosition(root, step.position);
|
|
39
|
+
if (target === void 0 || target.disabled === true) return;
|
|
40
|
+
if (step.kind === "click") return target.click();
|
|
41
|
+
if (target.value === step.value) return;
|
|
42
|
+
target.value = step.value;
|
|
43
|
+
dispatch(target, "input");
|
|
44
|
+
dispatch(target, "change");
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Replays a recording onto the freshly rendered application under `root`, in
|
|
48
|
+
* the order the reader acted, then restores focus. A value reaches the
|
|
49
|
+
* application as `input` and `change` events, as typing would.
|
|
50
|
+
*
|
|
51
|
+
* Steps whose element no longer matches, or is disabled, are skipped: a
|
|
52
|
+
* control the application has not enabled must not receive input the reader
|
|
53
|
+
* could not have given it. The pre-rendered markup never reacted to the
|
|
54
|
+
* reader, so a field the application would have changed between two edits
|
|
55
|
+
* holds the reader's text as typed into the static field.
|
|
56
|
+
*/
|
|
57
|
+
const replay = (root, recording) => {
|
|
58
|
+
for (const step of recording.steps) replayStep(root, step);
|
|
59
|
+
if (recording.focus === void 0) return;
|
|
60
|
+
const target = resolvePosition(root, recording.focus.position);
|
|
61
|
+
if (target === void 0 || target.disabled === true) return;
|
|
62
|
+
target.focus({ preventScroll: true });
|
|
63
|
+
const selection = recording.focus.selection;
|
|
64
|
+
if (selection !== void 0) target.setSelectionRange?.(selection[0], selection[1]);
|
|
65
|
+
};
|
|
66
|
+
const triggers = [
|
|
67
|
+
"pointerdown",
|
|
68
|
+
"focusin",
|
|
69
|
+
"keydown"
|
|
70
|
+
];
|
|
71
|
+
const recorded = [
|
|
72
|
+
"click",
|
|
73
|
+
"focusin",
|
|
74
|
+
"focusout",
|
|
75
|
+
"input",
|
|
76
|
+
"change",
|
|
77
|
+
"submit"
|
|
78
|
+
];
|
|
79
|
+
/** Fields whose state a click changes, so replaying the click carries it. */
|
|
80
|
+
const checkable = new Set(["checkbox", "radio"]);
|
|
81
|
+
/** The control a click on `target` will be forwarded to, when it is inside a label for another element. */
|
|
82
|
+
const labelledControl = (target) => {
|
|
83
|
+
const control = target.closest("label")?.control ?? null;
|
|
84
|
+
return control === null || control === target || control.contains(target) ? null : control;
|
|
85
|
+
};
|
|
86
|
+
/** The caret or selection of a text field, if it has one. */
|
|
87
|
+
const selectionOf = (element) => {
|
|
88
|
+
if (!(element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) return void 0;
|
|
89
|
+
const { selectionStart, selectionEnd } = element;
|
|
90
|
+
return selectionStart === null || selectionEnd === null ? void 0 : [selectionStart, selectionEnd];
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* The application root that replaced `oldRoot`. FoldKit renders its root in
|
|
94
|
+
* place of the mount element; should it ever render inside the mount instead,
|
|
95
|
+
* the mount is still in its slot and the root is its first child.
|
|
96
|
+
*/
|
|
97
|
+
const currentRoot = (slot) => {
|
|
98
|
+
const occupant = slot.parent.children[slot.index];
|
|
99
|
+
return occupant === slot.mount ? slot.mount.children[0] : occupant;
|
|
100
|
+
};
|
|
101
|
+
/** Resolves once `oldRoot` has left the document, which is when the client render replaced it. */
|
|
102
|
+
const replacement = (el, oldRoot) => new Promise((resolve) => {
|
|
103
|
+
if (!oldRoot.isConnected) return resolve();
|
|
104
|
+
const observer = new MutationObserver(() => {
|
|
105
|
+
if (oldRoot.isConnected) return;
|
|
106
|
+
observer.disconnect();
|
|
107
|
+
resolve();
|
|
108
|
+
});
|
|
109
|
+
observer.observe(el, {
|
|
110
|
+
childList: true,
|
|
111
|
+
subtree: true
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
const interactionDirective = (load, options, el) => {
|
|
115
|
+
const { value } = options;
|
|
116
|
+
const hover = value instanceof Object && "hover" in value && value.hover === true;
|
|
117
|
+
const mount = el.querySelector("[data-foldkit-island]");
|
|
118
|
+
const oldRoot = mount?.firstElementChild ?? null;
|
|
119
|
+
const parent = mount?.parentElement ?? null;
|
|
120
|
+
if (mount === null || oldRoot === null || parent === null) {
|
|
121
|
+
load().then((hydrate) => hydrate());
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const slot = {
|
|
125
|
+
parent,
|
|
126
|
+
index: Array.prototype.indexOf.call(parent.children, mount),
|
|
127
|
+
mount
|
|
128
|
+
};
|
|
129
|
+
let steps = [];
|
|
130
|
+
let focused;
|
|
131
|
+
let started = false;
|
|
132
|
+
const record = (event) => {
|
|
133
|
+
const target = event.target;
|
|
134
|
+
if (!(target instanceof Element)) return;
|
|
135
|
+
const position = positionOf(oldRoot, target);
|
|
136
|
+
if (position === void 0) return;
|
|
137
|
+
switch (event.type) {
|
|
138
|
+
case "click":
|
|
139
|
+
if (target.closest("a[href]") !== null) return;
|
|
140
|
+
if (labelledControl(target) !== null) return;
|
|
141
|
+
steps = appendStep(steps, {
|
|
142
|
+
kind: "click",
|
|
143
|
+
position
|
|
144
|
+
});
|
|
145
|
+
return;
|
|
146
|
+
case "focusin":
|
|
147
|
+
focused = {
|
|
148
|
+
element: target,
|
|
149
|
+
position
|
|
150
|
+
};
|
|
151
|
+
return;
|
|
152
|
+
case "focusout":
|
|
153
|
+
setTimeout(() => {
|
|
154
|
+
if (focused?.element === target && target.isConnected) focused = void 0;
|
|
155
|
+
});
|
|
156
|
+
return;
|
|
157
|
+
case "submit":
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
return;
|
|
160
|
+
default: if (target instanceof HTMLInputElement && !checkable.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement) steps = appendStep(steps, {
|
|
161
|
+
kind: "value",
|
|
162
|
+
position,
|
|
163
|
+
value: target.value
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
const stopRecording = () => {
|
|
168
|
+
for (const type of recorded) el.removeEventListener(type, record, true);
|
|
169
|
+
};
|
|
170
|
+
const replayRecording = () => {
|
|
171
|
+
const recording = {
|
|
172
|
+
steps,
|
|
173
|
+
focus: focused === void 0 || focused.element.isConnected ? void 0 : {
|
|
174
|
+
position: focused.position,
|
|
175
|
+
selection: selectionOf(focused.element)
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const root = currentRoot(slot);
|
|
179
|
+
if (root !== void 0) replay(root, recording);
|
|
180
|
+
};
|
|
181
|
+
const hydrateAndReplay = () => {
|
|
182
|
+
const swapped = replacement(el, oldRoot);
|
|
183
|
+
return load().then((hydrate) => hydrate()).then(() => swapped).finally(stopRecording).then(replayRecording);
|
|
184
|
+
};
|
|
185
|
+
const start = (event) => {
|
|
186
|
+
if (event.type === "pointerover" && event.pointerType !== "mouse") return;
|
|
187
|
+
if (started) return;
|
|
188
|
+
started = true;
|
|
189
|
+
for (const type of [...triggers, "pointerover"]) el.removeEventListener(type, start, true);
|
|
190
|
+
hydrateAndReplay();
|
|
191
|
+
};
|
|
192
|
+
for (const type of recorded) el.addEventListener(type, record, true);
|
|
193
|
+
for (const type of triggers) el.addEventListener(type, start, true);
|
|
194
|
+
if (hover) el.addEventListener("pointerover", start, true);
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
export { appendStep, currentRoot, interactionDirective as default, positionOf, replay, resolvePosition };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import { n as NavigationEvent, r as NavigationPhase, t as NavigationConfig } from "./navigation-Cy2lnE4G.mjs";
|
|
2
|
+
import { InteractionOptions } from "./directive-interaction.mjs";
|
|
2
3
|
import { AstroIntegration } from "astro";
|
|
3
4
|
|
|
4
5
|
//#region src/index.d.ts
|
|
6
|
+
declare module 'astro' {
|
|
7
|
+
interface AstroClientDirectives {
|
|
8
|
+
/**
|
|
9
|
+
* Hydrate a pre-rendered FoldKit island on the reader's first pointer press,
|
|
10
|
+
* focus or key press inside it, replaying clicks, typed values and focus
|
|
11
|
+
* that happened before it booted. Requires `lazyApp(load, { prerender: true })`.
|
|
12
|
+
*/
|
|
13
|
+
'client:interaction'?: InteractionOptions | boolean;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
5
16
|
type FoldkitIntegrationOptions = Readonly<{
|
|
6
17
|
server?: Readonly<{
|
|
7
18
|
buildId: string;
|
|
@@ -9,4 +20,4 @@ type FoldkitIntegrationOptions = Readonly<{
|
|
|
9
20
|
}>;
|
|
10
21
|
declare function foldkit(options?: FoldkitIntegrationOptions): AstroIntegration;
|
|
11
22
|
//#endregion
|
|
12
|
-
export { FoldkitIntegrationOptions, type NavigationConfig, type NavigationEvent, type NavigationPhase, foldkit as default };
|
|
23
|
+
export { FoldkitIntegrationOptions, type InteractionOptions, type NavigationConfig, type NavigationEvent, type NavigationPhase, foldkit as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url";
|
|
2
2
|
import { foldkit as foldkit$1 } from "@foldkit/vite-plugin";
|
|
3
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
4
|
+
import { join, relative, sep } from "node:path";
|
|
5
|
+
//#region src/island-scan.ts
|
|
6
|
+
const scannable = /\.[cm]?[jt]s$/;
|
|
7
|
+
const declaration = /\.d\.[cm]?ts$/;
|
|
8
|
+
const definesFoldkitEntry = /@opsydyn\/astro-foldkit\/define-(?:app|page)['"]/;
|
|
9
|
+
const globSyntax = /[()[\]{}*?!+@]/g;
|
|
10
|
+
const listFiles = (directory) => existsSync(directory) ? readdirSync(directory, {
|
|
11
|
+
recursive: true,
|
|
12
|
+
encoding: "utf8"
|
|
13
|
+
}) : [];
|
|
14
|
+
const isCandidate = (path) => scannable.test(path) && !declaration.test(path) && !path.split(sep).includes("node_modules");
|
|
15
|
+
const toEntryPattern = (root, file) => relative(root, file).split(sep).join("/").replace(globSyntax, "\\$&");
|
|
16
|
+
/** Root-relative glob patterns, escaped, for the source modules that define FoldKit islands or pages. */
|
|
17
|
+
const findIslandEntries = (root, srcDir) => listFiles(srcDir).filter(isCandidate).map((path) => join(srcDir, path)).filter((file) => definesFoldkitEntry.test(readFileSync(file, "utf8"))).map((file) => toEntryPattern(root, file));
|
|
18
|
+
const islandScanPlugin = (root, srcDir) => ({
|
|
19
|
+
name: "astro-foldkit:island-scan",
|
|
20
|
+
configEnvironment: (name) => {
|
|
21
|
+
if (name !== "client") return void 0;
|
|
22
|
+
const entries = findIslandEntries(root, srcDir);
|
|
23
|
+
return entries.length === 0 ? void 0 : { optimizeDeps: { entries } };
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
//#endregion
|
|
3
27
|
//#region src/index.ts
|
|
4
28
|
function foldkit(options = {}) {
|
|
5
29
|
const configuredBuildId = options.server?.buildId;
|
|
6
30
|
if (configuredBuildId !== void 0 && configuredBuildId.trim() === "") throw new Error("foldkit({ server: { buildId } }) requires a non-empty server.buildId.");
|
|
7
31
|
return {
|
|
8
32
|
name: "astro-foldkit",
|
|
9
|
-
hooks: { "astro:config:setup": ({ addRenderer, updateConfig }) => {
|
|
33
|
+
hooks: { "astro:config:setup": ({ config, addClientDirective, addRenderer, updateConfig }) => {
|
|
10
34
|
addRenderer({
|
|
11
35
|
name: "astro-foldkit",
|
|
12
36
|
clientEntrypoint: fileURLToPath(new URL("./client.mjs", import.meta.url)),
|
|
13
37
|
serverEntrypoint: fileURLToPath(new URL("./server.mjs", import.meta.url))
|
|
14
38
|
});
|
|
15
|
-
|
|
39
|
+
addClientDirective({
|
|
40
|
+
name: "interaction",
|
|
41
|
+
entrypoint: fileURLToPath(new URL("./directive-interaction.mjs", import.meta.url))
|
|
42
|
+
});
|
|
43
|
+
updateConfig({ vite: { plugins: [foldkit$1(options.server), islandScanPlugin(fileURLToPath(config.root), fileURLToPath(config.srcDir))] } });
|
|
16
44
|
} }
|
|
17
45
|
};
|
|
18
46
|
}
|
package/dist/server.mjs
CHANGED
|
@@ -52,8 +52,8 @@ const warnOnce = (key, message) => {
|
|
|
52
52
|
console.warn(message);
|
|
53
53
|
};
|
|
54
54
|
/**
|
|
55
|
-
* Names the
|
|
56
|
-
*
|
|
55
|
+
* Names the ways an island that is not pre-rendered silently misbehaves. Two
|
|
56
|
+
* leave it as the empty mount shell forever.
|
|
57
57
|
*
|
|
58
58
|
* With no client directive, nothing ever replaces the shell, so the island
|
|
59
59
|
* renders nothing and ships nothing. With `client:visible`, Astro observes the
|
|
@@ -61,12 +61,16 @@ const warnOnce = (key, message) => {
|
|
|
61
61
|
* mount measures 0x0, a zero-area target never intersects, and the island never
|
|
62
62
|
* hydrates. Other directives fire whatever the island's size.
|
|
63
63
|
*
|
|
64
|
+
* `client:interaction` on a shell does not fail, but it cannot do its job: it
|
|
65
|
+
* waits for input on markup the shell does not have, so it hydrates at once.
|
|
66
|
+
*
|
|
64
67
|
* Both warn rather than fail: the shell is still valid output, and a warning
|
|
65
68
|
* should never be what breaks a build.
|
|
66
69
|
*/
|
|
67
70
|
const diagnoseShellIsland = (metadata) => {
|
|
68
71
|
const name = metadata?.displayName ?? "A FoldKit island";
|
|
69
72
|
if (metadata?.hydrate === void 0) warnOnce(`${name}:no-directive`, `[astro-foldkit] ${name} has no client directive and is not pre-rendered, so it renders an empty mount and ships no JavaScript: it will never display anything. Pass { prerender: true } to lazyApp to render it as static HTML, or add a client directive.`);
|
|
73
|
+
else if (metadata.hydrate === "interaction") warnOnce(`${name}:interaction`, `[astro-foldkit] ${name} uses client:interaction but is not pre-rendered. It waits for the reader to interact with the island's markup, and an empty FoldKit mount has none, so it hydrates at once, as client:load would. Pass { prerender: true } to lazyApp, or use client:load or client:idle.`);
|
|
70
74
|
else if (metadata.hydrate === "visible") warnOnce(`${name}:visible`, `[astro-foldkit] ${name} uses client:visible but is not pre-rendered. Astro observes an island's children, and an empty FoldKit mount measures 0x0, so it never intersects and the island never hydrates. Pass { prerender: true } to lazyApp, or use client:load or client:idle.`);
|
|
71
75
|
};
|
|
72
76
|
async function check(component) {
|