@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

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.
Files changed (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
package/dist/list.test.js DELETED
@@ -1,344 +0,0 @@
1
- import { describe, it, expect, vi, afterEach } from 'vitest';
2
- import { createSignal, onMount, onCleanup } from './signal';
3
- import { h } from './dom';
4
- import { krateList, krateBindList, longestIncreasingSubsequence } from './list';
5
- import { createEffectDeps } from './effects';
6
- const tick = () => new Promise((r) => setTimeout(r, 0));
7
- function mountList(node) {
8
- const root = document.createElement('div');
9
- document.body.appendChild(root);
10
- root.appendChild(node);
11
- return root;
12
- }
13
- function rowTexts(root, selector = 'li') {
14
- return Array.from(root.querySelectorAll(selector)).map((el) => el.textContent || '');
15
- }
16
- afterEach(() => {
17
- document.body.innerHTML = '';
18
- });
19
- describe('longestIncreasingSubsequence', () => {
20
- it('ignores negative entries and returns indices in order', () => {
21
- expect(longestIncreasingSubsequence([2, 0, 1, -1, 3])).toEqual([1, 2, 4]);
22
- expect(longestIncreasingSubsequence([3, 2, 1, 0])).toHaveLength(1);
23
- expect(longestIncreasingSubsequence([])).toEqual([]);
24
- });
25
- });
26
- describe('keyed lists', () => {
27
- it('reorders by key keeping DOM nodes, input values and focus', async () => {
28
- const [items, setItems] = createSignal([
29
- { id: 1, label: 'a' },
30
- { id: 2, label: 'b' },
31
- { id: 3, label: 'c' },
32
- ]);
33
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label, h('input', { class: () => 'in-' + t().id })), 1).mount());
34
- const inputs = Array.from(root.querySelectorAll('input'));
35
- inputs[0].value = 'typed-a';
36
- inputs[2].focus();
37
- const lis = Array.from(root.querySelectorAll('li'));
38
- setItems([...items()].reverse());
39
- await tick();
40
- expect(rowTexts(root)).toEqual(['c', 'b', 'a']);
41
- const after = Array.from(root.querySelectorAll('li'));
42
- expect(after).toEqual([lis[2], lis[1], lis[0]]);
43
- expect(root.querySelector('.in-1').value).toBe('typed-a');
44
- expect(document.activeElement).toBe(inputs[2]);
45
- });
46
- it('keeps component state per key and updates item values in place', async () => {
47
- const [items, setItems] = createSignal([
48
- { id: 1, label: 'one' },
49
- { id: 2, label: 'two' },
50
- ]);
51
- function Row(props) {
52
- const [clicks, setClicks] = createSignal(0);
53
- return h('li', null, h('span', { class: 'label' }, () => props.item.label), h('button', { onClick: () => setClicks(clicks() + 1) }, () => clicks()), h('em', null, () => props.index));
54
- }
55
- const root = mountList(krateList(() => items(), (t) => t.id, (t, i) => h(Row, {
56
- get item() {
57
- return t();
58
- },
59
- get index() {
60
- return i();
61
- },
62
- }), 1).mount());
63
- root.querySelectorAll('button')[1].click();
64
- root.querySelectorAll('button')[1].click();
65
- await tick();
66
- expect(root.querySelectorAll('button')[1].textContent).toBe('2');
67
- // New object for key 2 and a move to the front: state stays, label and index update.
68
- setItems([{ id: 2, label: 'TWO' }, { id: 1, label: 'one' }]);
69
- await tick();
70
- expect(rowTexts(root, '.label')).toEqual(['TWO', 'one']);
71
- expect(root.querySelectorAll('button')[0].textContent).toBe('2');
72
- expect(rowTexts(root, 'em')).toEqual(['0', '1']);
73
- });
74
- it('runs cleanups for removed rows before new rows mount', async () => {
75
- const log = [];
76
- const [items, setItems] = createSignal([1, 2, 3]);
77
- function Row(props) {
78
- const id = props.id;
79
- onMount(() => {
80
- log.push('mount ' + id);
81
- onCleanup(() => log.push('unmount ' + id));
82
- });
83
- return h('li', null, String(id));
84
- }
85
- const root = mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
86
- await tick();
87
- expect(log).toEqual(['mount 1', 'mount 2', 'mount 3']);
88
- log.length = 0;
89
- setItems([1, 4, 3]);
90
- await tick();
91
- expect(rowTexts(root)).toEqual(['1', '4', '3']);
92
- expect(log).toEqual(['unmount 2', 'mount 4']);
93
- });
94
- it('inserts and removes in the middle with minimal DOM work', async () => {
95
- const [items, setItems] = createSignal([1, 2, 3, 4]);
96
- const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, () => String(n())), 1).mount());
97
- const before = Array.from(root.querySelectorAll('li'));
98
- setItems([1, 2, 9, 3, 4]);
99
- await tick();
100
- expect(rowTexts(root)).toEqual(['1', '2', '9', '3', '4']);
101
- const after = Array.from(root.querySelectorAll('li'));
102
- expect(after[0]).toBe(before[0]);
103
- expect(after[3]).toBe(before[2]);
104
- setItems([1, 9, 4]);
105
- await tick();
106
- expect(rowTexts(root)).toEqual(['1', '9', '4']);
107
- expect(root.querySelectorAll('li')[2]).toBe(before[3]);
108
- });
109
- it('moves only rows outside the longest increasing run', async () => {
110
- const [items, setItems] = createSignal([1, 2, 3, 4, 5]);
111
- const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, String(n())), 1).mount());
112
- const ul = root;
113
- const spy = vi.spyOn(ul, 'insertBefore');
114
- setItems([5, 1, 2, 3, 4]);
115
- await tick();
116
- expect(rowTexts(root)).toEqual(['5', '1', '2', '3', '4']);
117
- // One row moved: its start marker, element and end marker.
118
- expect(spy.mock.calls.length).toBe(3);
119
- spy.mockRestore();
120
- });
121
- it('warns on duplicate keys and keeps the first occurrence on its row', async () => {
122
- const err = vi.spyOn(console, 'error').mockImplementation(() => { });
123
- const [items, setItems] = createSignal([
124
- { id: 1, label: 'a' },
125
- { id: 1, label: 'b' },
126
- ]);
127
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
128
- expect(rowTexts(root)).toEqual(['a', 'b']);
129
- expect(err).toHaveBeenCalled();
130
- expect(String(err.mock.calls[0][0])).toContain('same key');
131
- const first = root.querySelector('li');
132
- setItems([{ id: 1, label: 'a2' }, { id: 1, label: 'b2' }, { id: 2, label: 'c' }]);
133
- await tick();
134
- expect(rowTexts(root)).toEqual(['a2', 'b2', 'c']);
135
- expect(root.querySelector('li')).toBe(first);
136
- err.mockRestore();
137
- });
138
- it('compares keys as strings like React', async () => {
139
- const [items, setItems] = createSignal([{ k: 1 }, { k: 2 }]);
140
- const root = mountList(krateList(() => items(), (t) => t.k, () => h('li', null, h('input', null)), 1).mount());
141
- root.querySelectorAll('input')[0].value = 'kept';
142
- setItems([{ k: '2' }, { k: '1' }]);
143
- await tick();
144
- expect(root.querySelectorAll('input')[1].value).toBe('kept');
145
- });
146
- it('remounts a keyed single element when its key changes', async () => {
147
- const log = [];
148
- const [user, setUser] = createSignal('ada');
149
- function Profile(props) {
150
- const [draft, setDraft] = createSignal('');
151
- const name = props.name;
152
- onMount(() => {
153
- log.push('mount ' + name);
154
- onCleanup(() => log.push('unmount ' + name));
155
- });
156
- return h('input', { value: () => draft(), onInput: (e) => setDraft(e.target.value), 'data-user': name });
157
- }
158
- const root = mountList(krateList(() => [user()], (k) => k, () => h(Profile, { get name() { return user(); } }), 1).mount());
159
- await tick();
160
- const input = root.querySelector('input');
161
- input.value = 'draft';
162
- input.dispatchEvent(new Event('input'));
163
- setUser('ada');
164
- await tick();
165
- expect(root.querySelector('input')).toBe(input);
166
- setUser('grace');
167
- await tick();
168
- const next = root.querySelector('input');
169
- expect(next).not.toBe(input);
170
- expect(next.value).toBe('');
171
- expect(next.getAttribute('data-user')).toBe('grace');
172
- expect(log).toEqual(['mount ada', 'unmount ada', 'mount grace']);
173
- });
174
- it('renders keyed fragments, text rows and nested lists', async () => {
175
- const [groups, setGroups] = createSignal([
176
- { id: 'g1', items: ['x', 'y'] },
177
- { id: 'g2', items: ['z'] },
178
- ]);
179
- const root = mountList(krateList(() => groups(), (g) => g.id, (g) => [
180
- h('dt', null, () => g().id),
181
- h('dd', null, krateList(() => g().items, null, (s) => () => s(), 1)),
182
- ], 1).mount());
183
- expect(rowTexts(root, 'dt')).toEqual(['g1', 'g2']);
184
- expect(rowTexts(root, 'dd')).toEqual(['xy', 'z']);
185
- const dts = Array.from(root.querySelectorAll('dt'));
186
- setGroups([{ id: 'g2', items: ['z', 'w'] }, { id: 'g1', items: ['y'] }]);
187
- await tick();
188
- expect(rowTexts(root, 'dt')).toEqual(['g2', 'g1']);
189
- expect(rowTexts(root, 'dd')).toEqual(['zw', 'y']);
190
- expect(Array.from(root.querySelectorAll('dt'))).toEqual([dts[1], dts[0]]);
191
- });
192
- it('switches between empty and non-empty and works inside tables', async () => {
193
- const [rows, setRows] = createSignal([]);
194
- const table = document.createElement('table');
195
- const tbody = document.createElement('tbody');
196
- table.appendChild(tbody);
197
- document.body.appendChild(table);
198
- tbody.appendChild(krateList(() => rows(), (n) => n, (n) => h('tr', null, h('td', null, () => String(n()))), 1).mount());
199
- expect(tbody.querySelectorAll('tr')).toHaveLength(0);
200
- setRows([1, 2]);
201
- await tick();
202
- expect(rowTexts(tbody, 'td')).toEqual(['1', '2']);
203
- setRows([]);
204
- await tick();
205
- expect(tbody.querySelectorAll('tr')).toHaveLength(0);
206
- expect(Array.from(tbody.childNodes).every((n) => n.nodeType === 8)).toBe(true);
207
- });
208
- it('uses index keys for unkeyed lists (state stays with the position)', async () => {
209
- const [items, setItems] = createSignal(['a', 'b']);
210
- const root = mountList(krateList(() => items(), null, (s) => h('li', null, () => s(), h('input', null)), 1).mount());
211
- root.querySelectorAll('input')[0].value = 'first';
212
- setItems(['b', 'a']);
213
- await tick();
214
- expect(rowTexts(root)).toEqual(['b', 'a']);
215
- expect(root.querySelectorAll('input')[0].value).toBe('first');
216
- });
217
- it('value mode re-renders a row whose item changed', async () => {
218
- const [items, setItems] = createSignal([{ id: 1, label: 'a' }]);
219
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, t.label), 0).mount());
220
- const li = root.querySelector('li');
221
- setItems([{ id: 1, label: 'b' }]);
222
- await tick();
223
- expect(rowTexts(root)).toEqual(['b']);
224
- expect(root.querySelector('li')).not.toBe(li);
225
- });
226
- it('detaches refs of removed rows', async () => {
227
- const refs = [];
228
- const [items, setItems] = createSignal([1]);
229
- mountList(krateList(() => items(), (n) => n, () => h('li', { ref: (el) => refs.push(el) }), 1).mount());
230
- setItems([]);
231
- await tick();
232
- expect(refs).toHaveLength(2);
233
- expect(refs[1]).toBeNull();
234
- });
235
- });
236
- describe('React effects in rows', () => {
237
- it('runs passive effects per key: none on reorder, cleanup on removal, setup for new keys', async () => {
238
- const log = [];
239
- const settle = async () => {
240
- for (let i = 0; i < 3; i++)
241
- await tick();
242
- };
243
- const [items, setItems] = createSignal([1, 2]);
244
- function Row(props) {
245
- const id = props.id;
246
- createEffectDeps(() => {
247
- log.push('effect ' + id);
248
- return () => log.push('cleanup ' + id);
249
- }, () => []);
250
- return h('li', null, String(id));
251
- }
252
- mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
253
- await settle();
254
- expect(log).toEqual(['effect 1', 'effect 2']);
255
- log.length = 0;
256
- setItems([2, 1]);
257
- await settle();
258
- expect(log).toEqual([]);
259
- setItems([2, 3]);
260
- await settle();
261
- expect(log).toEqual(['cleanup 1', 'effect 3']);
262
- });
263
- });
264
- describe('hydration adoption', () => {
265
- it('adopts server-rendered rows by key without replacing nodes', async () => {
266
- const root = document.createElement('ul');
267
- root.innerHTML =
268
- '<!--k:L--><!--k:L:$1--><li data-id="1">one<button>0</button></li><!--/k:L:$1-->' +
269
- '<!--k:L:$2--><li data-id="2">two<button>0</button></li><!--/k:L:$2--><!--/k:L-->';
270
- document.body.appendChild(root);
271
- const ssr = Array.from(root.querySelectorAll('li'));
272
- const [items, setItems] = createSignal([
273
- { id: 1, label: 'one' },
274
- { id: 2, label: 'two' },
275
- ]);
276
- function Row(props) {
277
- const [n, setN] = createSignal(0);
278
- return h('li', { 'data-id': () => props.item.id }, () => props.item.label, h('button', { onClick: () => setN(n() + 1) }, () => n()));
279
- }
280
- krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h(Row, { get item() { return t(); } }), 1);
281
- expect(Array.from(root.querySelectorAll('li'))).toEqual(ssr);
282
- root.querySelectorAll('button')[1].click();
283
- await tick();
284
- expect(root.querySelectorAll('button')[1].textContent).toBe('1');
285
- setItems([{ id: 2, label: 'two' }, { id: 1, label: 'uno' }]);
286
- await tick();
287
- expect(Array.from(root.querySelectorAll('li'))).toEqual([ssr[1], ssr[0]]);
288
- expect(rowTexts(root)).toEqual(['two1', 'uno0']);
289
- });
290
- it('adopts by position when the build could not compute keys and patches mismatches', async () => {
291
- const root = document.createElement('ul');
292
- root.innerHTML = '<!--k:M--><!--k:M:0--><li>stale</li><!--/k:M:0--><!--k:M:1--><li>gone</li><!--/k:M:1--><!--/k:M-->';
293
- document.body.appendChild(root);
294
- const first = root.querySelector('li');
295
- const [items] = createSignal([{ id: 'a', label: 'fresh' }]);
296
- krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1);
297
- expect(root.querySelectorAll('li')).toHaveLength(1);
298
- expect(root.querySelector('li')).toBe(first);
299
- expect(rowTexts(root)).toEqual(['fresh']);
300
- });
301
- it('adopts nested server-rendered lists inside rows', () => {
302
- const root = document.createElement('div');
303
- root.innerHTML =
304
- '<!--k:O--><!--k:O:$a--><section><h2>a</h2><ul><!--k:P--><!--k:P:$x--><li>x</li><!--/k:P:$x--><!--/k:P--></ul></section><!--/k:O:$a--><!--/k:O-->';
305
- document.body.appendChild(root);
306
- const li = root.querySelector('li');
307
- const [groups] = createSignal([{ id: 'a', items: ['x'] }]);
308
- krateBindList(root.firstChild, () => groups(), (g) => g.id, (g) => h('section', null, h('h2', null, () => g().id), h('ul', null, krateList(() => g().items, (s) => s, (s) => h('li', null, () => s()), 1))), 1);
309
- expect(root.querySelector('li')).toBe(li);
310
- expect(root.querySelectorAll('li')).toHaveLength(1);
311
- });
312
- });
313
- describe('list performance', () => {
314
- it('reorders, inserts and removes 1000 rows in roughly linear time', async () => {
315
- const make = (n) => Array.from({ length: n }, (_, i) => ({ id: i, label: 'row ' + i }));
316
- const [items, setItems] = createSignal(make(1000));
317
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
318
- expect(root.querySelectorAll('li')).toHaveLength(1000);
319
- const time = async (next) => {
320
- const t0 = performance.now();
321
- setItems(next);
322
- await tick();
323
- return performance.now() - t0;
324
- };
325
- const reversed = await time([...items()].reverse());
326
- expect(root.querySelector('li').textContent).toBe('row 999');
327
- const shuffled = [...items()];
328
- for (let i = shuffled.length - 1; i > 0; i--) {
329
- const j = (i * 7919) % (i + 1);
330
- [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
331
- }
332
- const shuffle = await time(shuffled);
333
- const inserted = [...items()];
334
- inserted.splice(500, 0, { id: 5000, label: 'inserted' });
335
- const insert = await time(inserted);
336
- const removed = items().filter((_, i) => i % 2 === 0);
337
- const remove = await time(removed);
338
- expect(root.querySelectorAll('li')).toHaveLength(removed.length);
339
- // Generous ceilings: happy-dom is slow, but a quadratic algorithm over
340
- // 1000 rows takes far longer than this.
341
- for (const ms of [reversed, shuffle, insert, remove])
342
- expect(ms).toBeLessThan(2000);
343
- });
344
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,167 +0,0 @@
1
- // @vitest-environment node
2
- import { describe, expect, it, vi } from 'vitest';
3
- import { metadataHeadTags, renderMetadataTags, resolveMetadata } from './metadata';
4
- import { forbidden, notFound } from './request-context';
5
- const tags = async (segments, props = {}) => metadataHeadTags(segments, props);
6
- describe('resolveMetadata merge order', () => {
7
- it('merges root layout to page: a deeper segment replaces a field wholesale', async () => {
8
- const { metadata } = await resolveMetadata([
9
- { mod: { metadata: { description: 'root', openGraph: { siteName: 'Site', title: 'root og' }, robots: 'noindex' } } },
10
- { mod: { metadata: { description: 'section' } } },
11
- { mod: { metadata: { openGraph: { title: 'page og' } } } },
12
- ], {});
13
- expect(metadata.description).toBe('section');
14
- // openGraph is replaced, not merged: siteName does not leak into the page.
15
- expect(metadata.openGraph).toEqual({ title: 'page og' });
16
- expect(metadata.robots).toBe('noindex');
17
- });
18
- it('applies the nearest title template to a title string, not to absolute or default', async () => {
19
- const r = await resolveMetadata([
20
- { mod: { metadata: { title: { default: 'Site', template: '%s | Site' } } } },
21
- { mod: { metadata: { title: 'About' } } },
22
- ], {});
23
- expect(r.metadata.title).toBe('About | Site');
24
- const abs = await resolveMetadata([{ mod: { metadata: { title: { template: '%s | Site', default: 'Site' } } } }, { mod: { metadata: { title: { absolute: 'Exactly' } } } }], {});
25
- expect(abs.metadata.title).toBe('Exactly');
26
- const own = await resolveMetadata([{ mod: { metadata: { title: { template: '%s | Site', default: 'Site' } } } }], {});
27
- expect(own.metadata.title).toBe('Site');
28
- const nested = await resolveMetadata([
29
- { mod: { metadata: { title: { template: '%s | A', default: 'A' } } } },
30
- { mod: { metadata: { title: { template: '%s | B', default: 'B' } } } },
31
- { mod: { metadata: { title: 'Page' } } },
32
- ], {});
33
- expect(nested.metadata.title).toBe('Page | B');
34
- });
35
- it('calls generateMetadata with awaitable params and searchParams and the parent metadata', async () => {
36
- const seen = [];
37
- const { metadata } = await resolveMetadata([
38
- { mod: { metadata: { description: 'from layout', title: { default: 'D', template: '%s!' } } } },
39
- {
40
- mod: {
41
- async generateMetadata(props, parent) {
42
- const params = await props.params;
43
- const sp = await props.searchParams;
44
- const above = await parent;
45
- seen.push([props.params.slug, params.slug, sp.q, above.description]);
46
- return { title: 'Post ' + params.slug };
47
- },
48
- },
49
- },
50
- ], { params: { slug: 'x' }, searchParams: { q: '1' } });
51
- expect(seen).toEqual([['x', 'x', '1', 'from layout']]);
52
- expect(metadata.title).toBe('Post x!');
53
- });
54
- it('skips a generateMetadata that throws and keeps the rest', async () => {
55
- const r = await resolveMetadata([
56
- { mod: { metadata: { description: 'layout' } }, file: 'layout' },
57
- {
58
- mod: {
59
- generateMetadata() {
60
- throw new Error('lookup failed');
61
- },
62
- metadata: { title: 'ignored when the function exists' },
63
- },
64
- file: 'page',
65
- },
66
- ], {});
67
- expect(r.metadata).toEqual({ description: 'layout' });
68
- expect(r.errors).toHaveLength(1);
69
- expect(r.errors[0].file).toBe('page');
70
- });
71
- it('lets notFound(), forbidden() and redirects through', async () => {
72
- await expect(resolveMetadata([{ mod: { generateMetadata: () => notFound() } }], {})).rejects.toMatchObject({ digest: 'NEXT_NOT_FOUND' });
73
- await expect(resolveMetadata([{ mod: { generateMetadata: () => forbidden() } }], {})).rejects.toMatchObject({ __krateHTTPStatus: 403 });
74
- await expect(resolveMetadata([{ mod: { generateMetadata: () => { throw Object.assign(new Error('r'), { __krateRedirect: '/x' }); } } }], {})).rejects.toMatchObject({ __krateRedirect: '/x' });
75
- });
76
- it('logs a failing generateMetadata and still returns the tags', async () => {
77
- const log = vi.fn();
78
- const out = await metadataHeadTags([{ mod: { metadata: { description: 'kept' } } }, { mod: { generateMetadata: () => { throw new Error('nope'); } }, file: 'page.tsx' }], {}, log);
79
- expect(out).toContain('<meta name="description" content="kept">');
80
- expect(log).toHaveBeenCalledOnce();
81
- expect(log.mock.calls[0][0]).toContain('page.tsx');
82
- });
83
- it('fills the fields a file convention supplies unless the segment sets them', async () => {
84
- const files = { openGraph: { images: ['/og.png'] }, twitter: { images: ['/tw.png'] } };
85
- const a = await resolveMetadata([{ mod: { metadata: { title: 'T' } }, files }], {});
86
- expect(a.metadata.openGraph).toEqual({ images: ['/og.png'] });
87
- expect(a.metadata.twitter).toMatchObject({ card: 'summary_large_image', images: ['/tw.png'] });
88
- const b = await resolveMetadata([{ mod: { metadata: { openGraph: { images: ['/mine.png'] } } }, files }], {});
89
- expect(b.metadata.openGraph).toEqual({ images: ['/mine.png'] });
90
- });
91
- });
92
- describe('renderMetadataTags', () => {
93
- it('renders the common fields', () => {
94
- const html = renderMetadataTags({
95
- title: 'Hello',
96
- description: 'A <b>"post"</b>',
97
- keywords: ['a', 'b'],
98
- authors: [{ name: 'Ada', url: 'https://ada.example' }],
99
- robots: { index: false, follow: true, googleBot: { index: true, maxImagePreview: 'large' } },
100
- alternates: { canonical: '/hello', languages: { 'en-US': '/en', de: '/de' } },
101
- themeColor: [{ media: '(prefers-color-scheme: dark)', color: '#000' }],
102
- metadataBase: 'https://example.com',
103
- icons: { icon: '/favicon.ico', apple: [{ url: '/apple.png', sizes: '180x180' }] },
104
- manifest: '/site.webmanifest',
105
- other: { custom: 'x' },
106
- });
107
- expect(html).toContain('<title>Hello</title>');
108
- expect(html).toContain('<meta name="description" content="A &lt;b&gt;&quot;post&quot;&lt;/b&gt;">');
109
- expect(html).toContain('<meta name="keywords" content="a,b">');
110
- expect(html).toContain('<meta name="author" content="Ada">');
111
- expect(html).toContain('<link rel="author" href="https://ada.example">');
112
- expect(html).toContain('<meta name="robots" content="noindex, follow">');
113
- expect(html).toContain('<meta name="googlebot" content="index, follow, max-image-preview:large">');
114
- expect(html).toContain('<link rel="canonical" href="https://example.com/hello">');
115
- expect(html).toContain('<link rel="alternate" hreflang="en-US" href="https://example.com/en">');
116
- expect(html).toContain('<meta name="theme-color" content="#000" media="(prefers-color-scheme: dark)">');
117
- expect(html).toContain('<link rel="icon" href="https://example.com/favicon.ico">');
118
- expect(html).toContain('<link rel="apple-touch-icon" href="https://example.com/apple.png" sizes="180x180">');
119
- expect(html).toContain('<link rel="manifest" href="https://example.com/site.webmanifest">');
120
- expect(html).toContain('<meta name="custom" content="x">');
121
- });
122
- it('renders Open Graph and Twitter with images resolved against metadataBase', () => {
123
- const html = renderMetadataTags({
124
- metadataBase: 'https://example.com/docs',
125
- openGraph: {
126
- type: 'article',
127
- title: 'T',
128
- url: '/a',
129
- images: ['/i.png', { url: 'https://cdn.example/x.png', width: 800, height: 600, alt: 'x' }],
130
- publishedTime: '2026-01-01',
131
- authors: ['Ada'],
132
- tags: ['a', 'b'],
133
- },
134
- twitter: { title: 'TT', images: ['/t.png'], creator: '@ada' },
135
- });
136
- expect(html).toContain('<meta property="og:url" content="https://example.com/docs/a">');
137
- expect(html).toContain('<meta property="og:image" content="https://example.com/docs/i.png">');
138
- expect(html).toContain('<meta property="og:image" content="https://cdn.example/x.png">');
139
- expect(html).toContain('<meta property="og:image:width" content="800">');
140
- expect(html).toContain('<meta property="og:image:alt" content="x">');
141
- expect(html).toContain('<meta property="article:published_time" content="2026-01-01">');
142
- expect(html).toContain('<meta property="article:tag" content="b">');
143
- expect(html).toContain('<meta name="twitter:card" content="summary_large_image">');
144
- expect(html).toContain('<meta name="twitter:image" content="https://example.com/docs/t.png">');
145
- expect(html).toContain('<meta name="twitter:creator" content="@ada">');
146
- });
147
- it('gives openGraph and twitter titles their own templates', async () => {
148
- const out = await tags([
149
- { mod: { metadata: { title: { default: 'D', template: '%s | Page' }, openGraph: { title: { default: 'OG', template: '%s | OG' } } } } },
150
- { mod: { metadata: { title: 'P', openGraph: { title: 'P' } } } },
151
- ]);
152
- expect(out).toContain('<title>P | Page</title>');
153
- expect(out).toContain('<meta property="og:title" content="P | OG">');
154
- });
155
- it('renders a viewport export with themeColor and colorScheme', async () => {
156
- const out = await tags([
157
- { mod: { viewport: { width: 'device-width', initialScale: 1, maximumScale: 5, userScalable: false, themeColor: '#fff', colorScheme: 'light dark' } } },
158
- ]);
159
- expect(out).toContain('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5, user-scalable=no">');
160
- expect(out).toContain('<meta name="theme-color" content="#fff">');
161
- expect(out).toContain('<meta name="color-scheme" content="light dark">');
162
- });
163
- it('renders nothing for an empty chain', async () => {
164
- expect(await tags([])).toBe('');
165
- expect(await tags([{ mod: {} }])).toBe('');
166
- });
167
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,130 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
- import { createEffect, createRoot, krateFlushSync, krateHook } from './index';
3
- import { krateResetNavigation, useParams, usePathname, useRouter, useSearchParams, useSelectedLayoutSegment, useSelectedLayoutSegments, } from './navigation';
4
- import * as serverNav from './navigation-server';
5
- const REQUEST_STORE = Symbol.for('krate.request.store');
6
- function setURL(url, route = '') {
7
- history.replaceState(null, '', url);
8
- globalThis.__KRATE_ROUTE__ = route;
9
- }
10
- function navigated() {
11
- window.dispatchEvent(new CustomEvent('krate:navigate-end'));
12
- }
13
- beforeEach(() => {
14
- krateResetNavigation();
15
- setURL('/');
16
- });
17
- afterEach(() => {
18
- delete globalThis.__KRATE_ROUTE__;
19
- delete globalThis[REQUEST_STORE];
20
- });
21
- describe('client router hooks', () => {
22
- it('usePathname follows the URL without the trailing slash', () => {
23
- setURL('/blog/hello/');
24
- const pathname = krateHook(usePathname());
25
- expect(pathname()).toBe('/blog/hello');
26
- setURL('/');
27
- navigated();
28
- expect(pathname()).toBe('/');
29
- setURL('/about/?x=1');
30
- navigated();
31
- expect(pathname()).toBe('/about');
32
- });
33
- it('is reactive: an effect runs again after a navigation, back and forward included', () => {
34
- setURL('/a/');
35
- const seen = [];
36
- createRoot(() => {
37
- const pathname = krateHook(usePathname());
38
- createEffect(() => {
39
- seen.push(pathname());
40
- });
41
- });
42
- setURL('/b/');
43
- navigated();
44
- krateFlushSync();
45
- setURL('/a/');
46
- navigated();
47
- krateFlushSync();
48
- expect(seen).toEqual(['/a', '/b', '/a']);
49
- });
50
- it('useSearchParams reads the live query', () => {
51
- setURL('/s/?q=one&tag=a&tag=b');
52
- const search = krateHook(useSearchParams());
53
- expect(search().get('q')).toBe('one');
54
- expect(search().getAll('tag')).toEqual(['a', 'b']);
55
- setURL('/s/?q=two');
56
- navigated();
57
- expect(search().get('q')).toBe('two');
58
- expect(search().has('tag')).toBe(false);
59
- });
60
- it('useParams matches the route pattern of the page', () => {
61
- setURL('/blog/hello/', '/blog/[slug]');
62
- const params = krateHook(useParams());
63
- expect(params()).toEqual({ slug: 'hello' });
64
- setURL('/blog/caf%C3%A9/', '/blog/[slug]');
65
- navigated();
66
- expect(params()).toEqual({ slug: 'café' });
67
- setURL('/docs/a/b/c/', '/docs/[...rest]');
68
- navigated();
69
- expect(params()).toEqual({ rest: ['a', 'b', 'c'] });
70
- setURL('/shop/', '/shop/[[...path]]');
71
- navigated();
72
- expect(params()).toEqual({});
73
- setURL('/about/', '');
74
- navigated();
75
- expect(params()).toEqual({});
76
- });
77
- it('a hook result reads as its value when it was not lowered', () => {
78
- setURL('/blog/hello/', '/blog/[slug]');
79
- expect(`${usePathname()}`).toBe('/blog/hello');
80
- expect(JSON.stringify({ p: useParams() })).toBe('{"p":{"slug":"hello"}}');
81
- expect(String(useSearchParams())).toBe('');
82
- });
83
- it('useSelectedLayoutSegment(s) count from the layout depth', () => {
84
- setURL('/dash/settings/profile/');
85
- const all = krateHook(useSelectedLayoutSegments());
86
- const fromDash = krateHook(useSelectedLayoutSegments(undefined, 1));
87
- const first = krateHook(useSelectedLayoutSegment(undefined, 1));
88
- const own = krateHook(useSelectedLayoutSegment(undefined, 3));
89
- expect(all()).toEqual(['dash', 'settings', 'profile']);
90
- expect(fromDash()).toEqual(['settings', 'profile']);
91
- expect(first()).toBe('settings');
92
- expect(own()).toBeNull();
93
- });
94
- it('useRouter has push, replace, back, forward, refresh and prefetch', () => {
95
- const router = useRouter();
96
- for (const m of ['push', 'replace', 'back', 'forward', 'refresh', 'prefetch']) {
97
- expect(typeof router[m]).toBe('function');
98
- }
99
- });
100
- });
101
- describe('router hooks while rendering a request', () => {
102
- function request(url, params, searchParams = {}) {
103
- globalThis[REQUEST_STORE] = {
104
- getStore: () => ({ url, method: 'GET', headers: new Headers(), cookies: new Map(), params, searchParams }),
105
- };
106
- }
107
- it('return plain values for the request, from the client entry or the server entry', () => {
108
- request('/post/[id]', { id: 'abc' }, { tab: 'info' });
109
- for (const nav of [{ usePathname, useParams, useSearchParams }, serverNav]) {
110
- expect(nav.usePathname()).toBe('/post/abc');
111
- expect(nav.useParams()).toEqual({ id: 'abc' });
112
- expect(nav.useSearchParams().get('tab')).toBe('info');
113
- }
114
- });
115
- it('fill catch-all params as arrays and as path parts', () => {
116
- request('/docs/[...rest]', { rest: 'a/b' });
117
- expect(usePathname()).toBe('/docs/a/b');
118
- expect(useParams()).toEqual({ rest: ['a', 'b'] });
119
- request('/shop/[[...path]]', {});
120
- expect(usePathname()).toBe('/shop');
121
- expect(useParams()).toEqual({});
122
- });
123
- it('segments count from the layout depth and useRouter methods throw', () => {
124
- request('/dash/settings', {});
125
- expect(useSelectedLayoutSegments(undefined, 1)).toEqual(['settings']);
126
- expect(useSelectedLayoutSegment(undefined, 0)).toBe('dash');
127
- expect(useSelectedLayoutSegment(undefined, 2)).toBeNull();
128
- expect(() => useRouter().push('/x')).toThrow(/browser/);
129
- });
130
- });
@@ -1 +0,0 @@
1
- export {};