@opsydyn/astro-foldkit 0.9.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [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
+
5
+ ### Features
6
+
7
+ - **astro-foldkit:** warn about islands that fail silently ([166dc12](https://github.com/opsydyn/fold-kit-experiments/commit/166dc12f752ccb1ec499a18196b40600d0d481dd))
8
+ - **astro-foldkit:** warn about islands that fail silently ([232e940](https://github.com/opsydyn/fold-kit-experiments/commit/232e94023d5181e049d6d964a93220d364d06fff))
9
+
10
+ ## [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
+
12
+ ### Features
13
+
14
+ - **astro-foldkit:** let an island pre-render its first paint ([46011d0](https://github.com/opsydyn/fold-kit-experiments/commit/46011d037531541d405a86b1d4079114fe547476))
15
+ - **astro-foldkit:** let an island pre-render its first paint ([ae06ae9](https://github.com/opsydyn/fold-kit-experiments/commit/ae06ae9df22a8cbf094cfe3a9a28ef48da5cf51d))
16
+
3
17
  ## [0.9.3](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.9.2...astro-foldkit-v0.9.3) (2026-10-29)
4
18
 
5
19
  ### Bug Fixes
package/README.md CHANGED
@@ -75,36 +75,129 @@ export default lazyApp(() => import('./main'));
75
75
 
76
76
  ```ts
77
77
  // src/apps/counter/main.ts
78
+ import { Schema } from 'effect';
78
79
  import type { Document, HtmlBuilder } from 'foldkit/html';
79
80
 
80
81
  type Message = 'Inc' | 'Dec';
81
82
 
82
- export const Model = null;
83
+ // `Model` is a runtime Schema, not a plain value: FoldKit reads `Model.ast`
84
+ // while the island hydrates. A non-Schema `Model` type-checks, because the
85
+ // contract types it as `unknown`, then fails at hydration.
86
+ export const Model = Schema.Number;
87
+ export type Model = typeof Model.Type;
88
+
83
89
  export const init = () => ({ model: 0 });
84
- export const update = (model: number, message: Message) => ({
90
+ export const update = (model: Model, message: Message) => ({
85
91
  model: message === 'Inc' ? model + 1 : model - 1,
86
92
  });
87
- export const view = (model: number, h: HtmlBuilder<Message>): Document => ({
93
+ export const view = (model: Model, h: HtmlBuilder<Message>): Document => ({
88
94
  title: `Counter: ${model}`,
89
95
  body: h.button([h.OnClick('Inc')], [String(model)]),
90
96
  });
91
97
  ```
92
98
 
99
+ A string-union `Message` keeps this example small. Applications with more than a
100
+ couple of messages should use `defineMessageUnion` from `foldkit/message`, as
101
+ the [astro-foldkit skill](../../skills/astro-foldkit) and the demo apps do.
102
+
93
103
  Use the app in an Astro page:
94
104
 
95
105
  ```astro
96
106
  ---
97
107
  import Counter from '../apps/counter/app'
98
108
  ---
99
- <Counter client:load />
109
+ <Counter client:load noMeta />
100
110
  ```
101
111
 
112
+ `noMeta` keeps the island from writing `document.title` on every render tick,
113
+ which is what you want whenever the Astro page owns its own title — in
114
+ particular on any page carrying more than one island. Drop it only when this
115
+ island is meant to own the page title. See [`noMeta`](#nometa).
116
+
102
117
  For `lazyApp` / `defineApp`, the server renderer emits a deterministic
103
118
  `<div data-foldkit-island="true"></div>` mount shell. It does not load or
104
119
  execute the FoldKit application during SSR; the application is loaded and
105
120
  embedded only by the client renderer. Astro still owns the outer island
106
121
  serialisation and client directive behavior.
107
122
 
123
+ ### Pre-rendered islands (opt-in)
124
+
125
+ An island can put its first paint in the HTML:
126
+
127
+ ```ts
128
+ import { lazyApp } from '@opsydyn/astro-foldkit/define-app';
129
+
130
+ export default lazyApp<Props>(() => import('./main'), { prerender: true });
131
+ ```
132
+
133
+ The server then loads the module and runs `init` and `view` over the props
134
+ Astro already serialises, filling the same mount shell instead of leaving it
135
+ empty. The markup is static. Nothing hydrates it, and the client replaces it on
136
+ boot exactly as it replaces an empty mount; because both sides run the same
137
+ pure `init` and `view` over the same props, the replacement is the same markup.
138
+
139
+ It carries no hydration stamp and no Flags payload, so any number of
140
+ pre-rendered islands may share one document — unlike a `definePage` owner, of
141
+ which a document may hold one. No build identity is involved, and no Astro
142
+ request context is read.
143
+
144
+ Two consequences are worth knowing:
145
+
146
+ - `init` and `view` run on the server. An island whose `init` touches a
147
+ browser-only resource must leave this off. Opting in and failing fails the
148
+ render rather than quietly falling back to the empty mount, because an author
149
+ who asked for markup should be told they did not get it.
150
+ - `client:visible` only works for an island that opts in. Astro's directive
151
+ observes an island's children, because `astro-island` is `display: contents`;
152
+ an empty mount measures 0x0 and never intersects, so the island silently
153
+ never hydrates. A pre-rendered island has size, and the directive works.
154
+
155
+ ### Choosing when JavaScript runs
156
+
157
+ Pre-rendering separates two decisions that used to be one: whether an island
158
+ appears in the HTML, and whether it ships JavaScript. Make them per island.
159
+
160
+ | Island needs | Write | JavaScript at load |
161
+ | --------------------------- | ------------------------------------------------- | ---------------------------- |
162
+ | To be read, never touched | `prerender: true`, **no directive** | none for this island |
163
+ | Interaction, below the fold | `prerender: true`, `client:visible` | none until it is scrolled to |
164
+ | Interaction, immediately | `prerender: true`, `client:load` or `client:idle` | the runtime, at load |
165
+ | A browser-only `init` | no `prerender`, `client:load` or `client:idle` | the runtime, at load |
166
+
167
+ An island with no directive is Astro's ordinary static component: Astro emits
168
+ no `<astro-island>` element and no script, so a pre-rendered FoldKit chart with
169
+ no directive is complete HTML and nothing else. Most charts in a report are
170
+ never interacted with, and this is how they should ship.
171
+
172
+ One thing decides how much any of this saves: **the FoldKit runtime is shared,
173
+ so one island that hydrates at load pays for all of it.** Making three of four
174
+ islands static saves their own chunks and serialised props, not the runtime the
175
+ fourth still needs. The large win comes when _nothing_ on the page hydrates at
176
+ load. The pattern that gets there is to pre-render everything, leave the
177
+ readable islands without a directive, and give the one interactive island
178
+ `client:visible` if it sits below the fold.
179
+
180
+ Measured on a report page with three charts and a data grid over 7,729 rows:
181
+
182
+ | Configuration | JavaScript at load | After scrolling to the grid |
183
+ | ------------------------------------------------------------- | -------------------- | --------------------------- |
184
+ | Four islands, `client:load` | 377 KB, 17 requests | — |
185
+ | Three charts static, grid `client:load` | 351 KB, 8 requests | — |
186
+ | Everything pre-rendered, charts static, grid `client:visible` | **0 KB, 0 requests** | 351 KB, 8 requests |
187
+
188
+ In the last configuration the whole report, the grid's first page included,
189
+ renders before any JavaScript runs. A chart that loses its hover readout by
190
+ going static can keep the information as a native SVG `<title>` on each mark,
191
+ which the browser shows as a tooltip and which costs no runtime.
192
+
193
+ The renderer warns at build and dev time about the two arrangements that fail
194
+ without an error, each once per component:
195
+
196
+ - an island with **no directive and no `prerender`** renders an empty mount and
197
+ ships nothing, so it never displays anything;
198
+ - an island with **`client:visible` and no `prerender`** never intersects, for
199
+ the reason above, so it never hydrates.
200
+
108
201
  FoldKit replaces the inner mount, not the outer `<astro-island>`. The outer
109
202
  element remains Astro's lifecycle owner, including for empty `client:only`
110
203
  hosts, where the client renderer creates an inner mount. Nested islands keep
@@ -326,6 +419,24 @@ Both `noMeta` and `noMeta={true}` are accepted. The wrapper pins only the
326
419
  Document `title`; FoldKit still applies `lang` and `dir` from the app's current
327
420
  Document.
328
421
 
422
+ `noMeta` is declared on the default `EmptyProps`. Supplying your own `Props` to
423
+ `lazyApp<Props>` replaces that type, so re-declare the prop to keep the opt-out
424
+ available:
425
+
426
+ ```ts
427
+ // src/apps/chart/app.ts
428
+ import { lazyApp } from '@opsydyn/astro-foldkit/define-app';
429
+
430
+ import type { ChartProps } from './main';
431
+
432
+ type Props = ChartProps & { readonly noMeta?: boolean | '' };
433
+
434
+ export default lazyApp<Props>(() => import('./main'));
435
+ ```
436
+
437
+ Without it, `<Chart client:load noMeta />` fails to type-check with
438
+ `Property 'noMeta' does not exist`.
439
+
329
440
  ## Architecture
330
441
 
331
442
  `definePage` owns one server-rendered FoldKit application for a document;
package/dist/client.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { n as NavigationEvent } from "./navigation-Cy2lnE4G.mjs";
2
- import { a as FoldkitApp, n as AppConfigContract } from "./types-DDk3MMb7.mjs";
2
+ import { n as AppConfigContract, o as FoldkitApp } from "./types-Dws9AyDS.mjs";
3
3
 
4
4
  //#region src/client.d.ts
5
5
  interface RuntimeConfigInput {}
@@ -1,8 +1,8 @@
1
- import { a as FoldkitApp, i as EmptyProps, n as AppConfigContract, t as AppConfig } from "./types-DDk3MMb7.mjs";
1
+ import { a as EmptyProps, n as AppConfigContract, o as FoldkitApp, r as DefineAppOptions, t as AppConfig } from "./types-Dws9AyDS.mjs";
2
2
 
3
3
  //#region src/define-app.d.ts
4
- declare function defineApp<Props extends object = EmptyProps, Config extends AppConfigContract<Props> = AppConfigContract<Props>>(load: () => Promise<Config>): FoldkitApp<Props, Config>;
4
+ declare function defineApp<Props extends object = EmptyProps, Config extends AppConfigContract<Props> = AppConfigContract<Props>>(load: () => Promise<Config>, options?: DefineAppOptions): FoldkitApp<Props, Config>;
5
5
  /** Preferred name for Astro's intentional lazy island entry boundary. */
6
6
  declare const lazyApp: typeof defineApp;
7
7
  //#endregion
8
- export { type AppConfig, type FoldkitApp, defineApp, lazyApp };
8
+ export { type AppConfig, type DefineAppOptions, type FoldkitApp, defineApp, lazyApp };
@@ -1,8 +1,9 @@
1
1
  //#region src/define-app.ts
2
- function defineApp(load) {
2
+ function defineApp(load, options) {
3
3
  return Object.assign((_props) => {}, {
4
4
  __foldkit: true,
5
- load
5
+ load,
6
+ prerender: options?.prerender === true
6
7
  });
7
8
  }
8
9
  /** Preferred name for Astro's intentional lazy island entry boundary. */
@@ -1,4 +1,4 @@
1
- import { c as PageConfigContract, d as PageFlagsSchema, f as PageParams, i as EmptyProps, l as PageContext, o as FoldkitPage, r as DefinePageOptions, s as PageConfig, u as PageFlagsContext } from "./types-DDk3MMb7.mjs";
1
+ import { a as EmptyProps, c as PageConfig, d as PageFlagsContext, f as PageFlagsSchema, i as DefinePageOptions, l as PageConfigContract, p as PageParams, s as FoldkitPage, u as PageContext } from "./types-Dws9AyDS.mjs";
2
2
 
3
3
  //#region src/define-page.d.ts
4
4
  declare function definePage<Props extends object = EmptyProps, Flags extends object = EmptyProps, Config extends PageConfigContract<Flags> = PageConfigContract<Flags>>(load: () => Promise<Config>, options: DefinePageOptions<Props, Flags>): FoldkitPage<Props, Flags, Config>;
@@ -1,4 +1,4 @@
1
- import { l as PageContext, o as FoldkitPage } from "./types-DDk3MMb7.mjs";
1
+ import { s as FoldkitPage, u as PageContext } from "./types-Dws9AyDS.mjs";
2
2
  import { RenderedApplication } from "foldkit/experimental/server";
3
3
 
4
4
  //#region src/server-document.d.ts
@@ -1,8 +1,21 @@
1
- import { c as PageConfigContract } from "./types-DDk3MMb7.mjs";
1
+ import { l as PageConfigContract, n as AppConfigContract } from "./types-Dws9AyDS.mjs";
2
2
  import { RenderedApplication } from "foldkit/experimental/server";
3
3
 
4
4
  //#region src/server-render.d.ts
5
5
  type ServerConfig<Flags extends object> = Pick<PageConfigContract<Flags>, 'Flags' | 'init' | 'view'>;
6
6
  declare function renderFoldkitServerApplication<Flags extends object>(config: ServerConfig<Flags>, flags: Flags, buildId?: string): Promise<RenderedApplication>;
7
+ type IslandConfig<Props extends object> = Pick<AppConfigContract<Props>, 'init' | 'view'>;
8
+ /**
9
+ * Renders an island's first paint as static markup.
10
+ *
11
+ * The island declares no Flags, and needs none: `renderToString` accepts a
12
+ * config whose `init` takes no argument, so the props Astro already serialises
13
+ * for the client are closed over here. `isHydratable: false` is what keeps
14
+ * this safe beside other islands — the render carries no root stamp, so it
15
+ * cannot collide with another island's, and `Runtime.hydrate`'s one-root rule
16
+ * never applies because nothing hydrates this markup. The client's existing
17
+ * `embed` replaces it on boot.
18
+ */
19
+ declare function renderFoldkitIslandMarkup<Props extends object>(config: IslandConfig<Props>, props: Props): Promise<RenderedApplication>;
7
20
  //#endregion
8
- export { renderFoldkitServerApplication };
21
+ export { renderFoldkitIslandMarkup, renderFoldkitServerApplication };
@@ -11,5 +11,22 @@ async function renderFoldkitServerApplication(config, flags, buildId) {
11
11
  buildId
12
12
  }));
13
13
  }
14
+ /**
15
+ * Renders an island's first paint as static markup.
16
+ *
17
+ * The island declares no Flags, and needs none: `renderToString` accepts a
18
+ * config whose `init` takes no argument, so the props Astro already serialises
19
+ * for the client are closed over here. `isHydratable: false` is what keeps
20
+ * this safe beside other islands — the render carries no root stamp, so it
21
+ * cannot collide with another island's, and `Runtime.hydrate`'s one-root rule
22
+ * never applies because nothing hydrates this markup. The client's existing
23
+ * `embed` replaces it on boot.
24
+ */
25
+ async function renderFoldkitIslandMarkup(config, props) {
26
+ return await Effect.runPromise(renderToString({
27
+ init: () => config.init(props),
28
+ view: config.view
29
+ }, { isHydratable: false }));
30
+ }
14
31
  //#endregion
15
- export { renderFoldkitServerApplication };
32
+ export { renderFoldkitIslandMarkup, renderFoldkitServerApplication };
package/dist/server.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { f as PageParams } from "./types-DDk3MMb7.mjs";
1
+ import { p as PageParams } from "./types-Dws9AyDS.mjs";
2
2
  import { NamedSSRLoadedRendererValue } from "astro";
3
3
 
4
4
  //#region src/server.d.ts
@@ -19,10 +19,19 @@ type AstroLike<Props extends AstroProps> = Readonly<{
19
19
  params?: PageParams;
20
20
  props?: Props;
21
21
  }>;
22
+ /**
23
+ * What Astro passes a renderer about the component it is rendering: its name,
24
+ * and the client directive it carries, if any. A `client:only` component never
25
+ * reaches the renderer, so it never appears here.
26
+ */
27
+ type AstroRenderMetadata = Readonly<{
28
+ displayName?: string;
29
+ hydrate?: string;
30
+ }>;
22
31
  declare function check<Component>(component: Component): Promise<boolean>;
23
- declare function renderToStaticMarkup(this: RendererThis | void, component: AstroComponentInput, props: AstroProps, slots: AstroSlots): Promise<{
32
+ declare function renderToStaticMarkup(this: RendererThis | void, component: AstroComponentInput, props: AstroProps, slots: AstroSlots, metadata?: AstroRenderMetadata): Promise<{
24
33
  html: string;
25
34
  }>;
26
35
  declare const renderer: NamedSSRLoadedRendererValue;
27
36
  //#endregion
28
- export { AstroComponentInput, AstroProps, AstroSlots, check, renderer as default, renderToStaticMarkup };
37
+ export { AstroComponentInput, AstroProps, AstroRenderMetadata, AstroSlots, check, renderer as default, renderToStaticMarkup };
package/dist/server.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { readFoldkitBuildId } from "./build-id.mjs";
2
- import { renderFoldkitServerApplication } from "./server-render.mjs";
2
+ import { renderFoldkitIslandMarkup, renderFoldkitServerApplication } from "./server-render.mjs";
3
3
  //#region src/server.ts
4
4
  const claimedPageResults = /* @__PURE__ */ new WeakSet();
5
5
  const isObjectOrFunction = (value) => value != null && (typeof value === "object" || typeof value === "function");
@@ -30,12 +30,53 @@ const renderPage = async (result, component, props, slots) => {
30
30
  const flags = component.flags(context);
31
31
  return { html: (await renderFoldkitServerApplication(await component.load(), flags, readFoldkitBuildId())).html };
32
32
  };
33
+ /** The mount the client's `findIslandMount` resolves, empty or filled. */
34
+ const islandMount = (markup) => `<div data-foldkit-island="true">${markup}</div>`;
35
+ const isPrerenderedApp = (value) => isFoldkitApp(value) && value.prerender === true;
36
+ /**
37
+ * Renders an opted-in island inside the mount the client already resolves.
38
+ *
39
+ * The markup goes inside the mount rather than beside it, because
40
+ * `findIslandMount` resolves that element as the runtime's container and the
41
+ * runtime replaces its contents on boot. Server and client run the same pure
42
+ * `init` and `view` over the same props, so the replacement is the same markup
43
+ * and the swap is invisible.
44
+ */
45
+ const renderIsland = async (component, props) => {
46
+ return { html: islandMount((await renderFoldkitIslandMarkup(await component.load(), props)).html) };
47
+ };
48
+ const warnedIslands = /* @__PURE__ */ new Set();
49
+ const warnOnce = (key, message) => {
50
+ if (warnedIslands.has(key)) return;
51
+ warnedIslands.add(key);
52
+ console.warn(message);
53
+ };
54
+ /**
55
+ * Names the two ways an island that is not pre-rendered fails without an
56
+ * error, both of which leave it as the empty mount shell forever.
57
+ *
58
+ * With no client directive, nothing ever replaces the shell, so the island
59
+ * renders nothing and ships nothing. With `client:visible`, Astro observes the
60
+ * island's children because `astro-island` is `display: contents`; the empty
61
+ * mount measures 0x0, a zero-area target never intersects, and the island never
62
+ * hydrates. Other directives fire whatever the island's size.
63
+ *
64
+ * Both warn rather than fail: the shell is still valid output, and a warning
65
+ * should never be what breaks a build.
66
+ */
67
+ const diagnoseShellIsland = (metadata) => {
68
+ const name = metadata?.displayName ?? "A FoldKit island";
69
+ 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.`);
70
+ 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
+ };
33
72
  async function check(component) {
34
73
  return isFoldkitApp(component) || isFoldkitPage(component);
35
74
  }
36
- async function renderToStaticMarkup(component, props, slots) {
75
+ async function renderToStaticMarkup(component, props, slots, metadata) {
37
76
  if (isFoldkitPage(component)) return renderPage(this?.result, component, props, slots);
38
- return { html: "<div data-foldkit-island=\"true\"></div>" };
77
+ if (isPrerenderedApp(component)) return renderIsland(component, props);
78
+ diagnoseShellIsland(metadata);
79
+ return { html: islandMount("") };
39
80
  }
40
81
  const renderer = {
41
82
  name: "astro-foldkit",
@@ -29,10 +29,28 @@ type AppConfig<Props extends object = EmptyProps, Model = unknown, Message exten
29
29
  readonly update: Runtime.ApplicationConfig<Model, Message>['update'];
30
30
  readonly view: Runtime.ApplicationConfig<Model, Message>['view'];
31
31
  };
32
+ type DefineAppOptions = {
33
+ /**
34
+ * Render this island's markup on the server for first paint.
35
+ *
36
+ * The markup is static: nothing hydrates it, and the client replaces it when
37
+ * the island boots, exactly as it replaces the empty mount today. It carries
38
+ * no hydration stamp and no Flags payload, so any number of pre-rendered
39
+ * islands may share a document — unlike a `definePage` owner, of which a
40
+ * document may hold one.
41
+ *
42
+ * `init` and `view` therefore run on the server. An island whose `init`
43
+ * touches a browser-only resource must leave this off; opting in and failing
44
+ * fails the render rather than quietly serving the empty mount, because an
45
+ * author who asked for markup should be told they did not get it.
46
+ */
47
+ readonly prerender?: boolean;
48
+ };
32
49
  type FoldkitApp<Props extends object = EmptyProps, Config extends AppConfigContract<Props> = AppConfigContract<Props>> = {
33
50
  (props?: Props): void;
34
51
  readonly __foldkit: true;
35
52
  readonly load: () => Promise<Config>;
53
+ readonly prerender?: boolean;
36
54
  };
37
55
  type PageParams = Readonly<Record<string, string | undefined>>;
38
56
  type PageFlagsContext<Props extends object = EmptyProps> = {
@@ -59,4 +77,4 @@ type FoldkitPage<Props extends object = EmptyProps, Flags extends object = Empty
59
77
  readonly flags: (context: PageFlagsContext<Props>) => Flags;
60
78
  };
61
79
  //#endregion
62
- export { FoldkitApp as a, PageConfigContract as c, PageFlagsSchema as d, PageParams as f, EmptyProps as i, PageContext as l, AppConfigContract as n, FoldkitPage as o, DefinePageOptions as r, PageConfig as s, AppConfig as t, PageFlagsContext as u };
80
+ export { EmptyProps as a, PageConfig as c, PageFlagsContext as d, PageFlagsSchema as f, DefinePageOptions as i, PageConfigContract as l, AppConfigContract as n, FoldkitApp as o, PageParams as p, DefineAppOptions as r, FoldkitPage as s, AppConfig as t, PageContext as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opsydyn/astro-foldkit",
3
- "version": "0.9.3",
3
+ "version": "0.11.0",
4
4
  "description": "Astro integration and renderer for FoldKit.",
5
5
  "keywords": [
6
6
  "astro",
@@ -76,7 +76,7 @@
76
76
  },
77
77
  "devDependencies": {
78
78
  "@types/node": "^22.0.0",
79
- "astro": "7.4.0",
79
+ "astro": "7.4.11",
80
80
  "bun-types": "^1.3.14",
81
81
  "effect": "4.1.0",
82
82
  "fast-check": "^4.8.0",