@krate/runtime 0.5.0-rc.5 → 0.5.0-rc.7

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 (98) hide show
  1. package/dist/actions.js +10 -1
  2. package/dist/cache-scope.d.ts +2 -0
  3. package/dist/cache-scope.js +6 -1
  4. package/dist/context.d.ts +5 -0
  5. package/dist/context.js +28 -17
  6. package/dist/flight-props.d.ts +7 -0
  7. package/dist/flight-props.js +105 -0
  8. package/dist/handler-routing.d.ts +2 -23
  9. package/dist/handler-routing.js +4 -188
  10. package/dist/head-tags.js +5 -2
  11. package/dist/highlight.d.ts +3 -2
  12. package/dist/highlight.js +12 -6
  13. package/dist/hints.d.ts +25 -2
  14. package/dist/hints.js +148 -74
  15. package/dist/index.d.ts +1 -0
  16. package/dist/island-swap.d.ts +45 -0
  17. package/dist/island-swap.js +608 -0
  18. package/dist/jsx-types.d.ts +7 -2
  19. package/dist/krate-core.js +1 -1
  20. package/dist/krate-f-actions.js +1 -1
  21. package/dist/krate-f-list.js +1 -1
  22. package/dist/krate-f-navigation.js +1 -1
  23. package/dist/krate-f-router-lite.js +1 -1
  24. package/dist/krate-f-router.js +1 -1
  25. package/dist/krate-features.json +452 -0
  26. package/dist/krate-hydrate-lite.js +5 -5
  27. package/dist/krate-hydrate.js +5 -5
  28. package/dist/krate-island-swap.js +1 -0
  29. package/dist/krate-runtime.js +5 -5
  30. package/dist/list.d.ts +14 -4
  31. package/dist/list.js +4 -5
  32. package/dist/navigation-react.d.ts +13 -0
  33. package/dist/navigation-react.js +86 -0
  34. package/dist/navigation.d.ts +19 -0
  35. package/dist/navigation.js +35 -0
  36. package/dist/react-compat.d.ts +39 -0
  37. package/dist/react-compat.js +359 -0
  38. package/dist/request-extras.d.ts +4 -0
  39. package/dist/request-extras.js +51 -0
  40. package/dist/route-source.d.ts +39 -0
  41. package/dist/route-source.js +532 -0
  42. package/dist/router.js +293 -23
  43. package/dist/server-handler.d.ts +20 -1
  44. package/dist/server-handler.js +208 -92
  45. package/dist/server.d.ts +24 -17
  46. package/dist/server.js +435 -101
  47. package/dist/site-config.d.ts +2 -0
  48. package/dist/testing.d.ts +4 -1
  49. package/dist/testing.js +3 -1
  50. package/package.json +15 -2
  51. package/dist/actions.test.d.ts +0 -1
  52. package/dist/actions.test.js +0 -75
  53. package/dist/after.test.d.ts +0 -1
  54. package/dist/after.test.js +0 -58
  55. package/dist/builtin-tags.test.d.ts +0 -1
  56. package/dist/builtin-tags.test.js +0 -12
  57. package/dist/cache-adapter.test.d.ts +0 -1
  58. package/dist/cache-adapter.test.js +0 -45
  59. package/dist/cache.test.d.ts +0 -1
  60. package/dist/cache.test.js +0 -37
  61. package/dist/events.test.d.ts +0 -1
  62. package/dist/events.test.js +0 -42
  63. package/dist/handler-html.test.d.ts +0 -1
  64. package/dist/handler-html.test.js +0 -55
  65. package/dist/handler-segments.test.d.ts +0 -1
  66. package/dist/handler-segments.test.js +0 -25
  67. package/dist/head-tags.test.d.ts +0 -1
  68. package/dist/head-tags.test.js +0 -24
  69. package/dist/inline-action-token.test.d.ts +0 -1
  70. package/dist/inline-action-token.test.js +0 -31
  71. package/dist/jsx-svg-types.test.d.ts +0 -1
  72. package/dist/jsx-svg-types.test.js +0 -58
  73. package/dist/link-base-path.test.d.ts +0 -1
  74. package/dist/link-base-path.test.js +0 -28
  75. package/dist/node-server.test.d.ts +0 -1
  76. package/dist/node-server.test.js +0 -138
  77. package/dist/platform.test.d.ts +0 -1
  78. package/dist/platform.test.js +0 -44
  79. package/dist/router-nav.test.d.ts +0 -1
  80. package/dist/router-nav.test.js +0 -130
  81. package/dist/server-handler.test.d.ts +0 -1
  82. package/dist/server-handler.test.js +0 -338
  83. package/dist/server-renderer.test.d.ts +0 -1
  84. package/dist/server-renderer.test.js +0 -162
  85. package/dist/server.test.d.ts +0 -1
  86. package/dist/server.test.js +0 -91
  87. package/dist/sha256.test.d.ts +0 -1
  88. package/dist/sha256.test.js +0 -13
  89. package/dist/signal.test.d.ts +0 -1
  90. package/dist/signal.test.js +0 -68
  91. package/dist/site-config.test.d.ts +0 -1
  92. package/dist/site-config.test.js +0 -30
  93. package/dist/slot.test.d.ts +0 -1
  94. package/dist/slot.test.js +0 -25
  95. package/dist/ssr-core-behavior.test.d.ts +0 -1
  96. package/dist/ssr-core-behavior.test.js +0 -108
  97. package/dist/testing.test.d.ts +0 -1
  98. package/dist/testing.test.js +0 -73
package/dist/hints.js CHANGED
@@ -1,97 +1,171 @@
1
1
  // React DOM's resource hints (preload, preinit, preconnect, prefetchDNS,
2
2
  // preloadModule, preinitModule). React emits <link>/<script> tags into the
3
- // document head for them; on the client they add the tag once.
4
- function headHas(selector) {
5
- return !!document.head.querySelector(selector);
3
+ // document head for them. In the browser they add the tag once, and nothing
4
+ // when the document already has it (the server HTML put it there). While the
5
+ // server renders, they record the tag on the render scope and the renderer
6
+ // puts the recorded tags into the <head> of the response (withCacheScope).
7
+ // When the build renders an island outside any request, they record the tag
8
+ // in the set the build installs as globalThis.__krate_island_hints, and the
9
+ // build puts it into the head of the page.
10
+ // This module must stay free of imports: island bundles inline it.
11
+ function optAttrs(opts, names) {
12
+ const out = [];
13
+ if (!opts)
14
+ return out;
15
+ for (const name of names) {
16
+ const v = opts[name];
17
+ if (v === undefined || v === null || v === false)
18
+ continue;
19
+ // crossOrigin: "" is a valid value (anonymous); the others need text.
20
+ if (v === '' && name !== 'crossOrigin')
21
+ continue;
22
+ const attr = name === 'crossOrigin' ? 'crossorigin' :
23
+ name === 'fetchPriority' ? 'fetchpriority' :
24
+ name === 'referrerPolicy' ? 'referrerpolicy' :
25
+ name === 'imageSrcSet' ? 'imagesrcset' :
26
+ name === 'imageSizes' ? 'imagesizes' : name;
27
+ if (name === 'crossOrigin') {
28
+ // As React: "use-credentials" or anonymous (""), so react-dom in a
29
+ // real-React island finds the tag by the same selector.
30
+ out.push([attr, v === 'use-credentials' ? v : '']);
31
+ continue;
32
+ }
33
+ out.push([attr, v === true ? '' : String(v)]);
34
+ }
35
+ return out;
6
36
  }
7
- function addLink(rel, href, opts) {
8
- if (typeof document === 'undefined' || !href)
9
- return;
10
- const sel = 'link[rel="' + rel + '"][href="' + (window.CSS && CSS.escape ? CSS.escape(href) : href) + '"]';
11
- if (headHas(sel))
37
+ function escapeAttr(v) {
38
+ return v.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
39
+ }
40
+ // The scope of the render that is running on the server, if any. The cache
41
+ // runtime keeps it on globalThis, so every copy of this module sees it.
42
+ function serverScope() {
43
+ const rt = globalThis[Symbol.for('krate.cache.runtime')];
44
+ const store = rt && rt.renderStore;
45
+ return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
46
+ }
47
+ // serverHints is where a hint called outside a browser goes: the running
48
+ // render's scope, else the build's island collector, else nowhere.
49
+ function serverHints() {
50
+ const scope = serverScope();
51
+ if (scope)
52
+ return (scope.hints ?? (scope.hints = new Set()));
53
+ const c = globalThis.__krate_island_hints;
54
+ return c instanceof Set ? c : undefined;
55
+ }
56
+ function absURL(href) {
57
+ try {
58
+ return new URL(href, document.baseURI).href;
59
+ }
60
+ catch {
61
+ return href;
62
+ }
63
+ }
64
+ // present reports whether the document already has the tag: the same rel (or
65
+ // script type) and the same URL, however it is spelled. A preload also needs
66
+ // the same `as`, as in React.
67
+ function present(tag, attrs) {
68
+ const get = (name) => {
69
+ for (const [k, v] of attrs)
70
+ if (k === name)
71
+ return v;
72
+ return undefined;
73
+ };
74
+ if (tag === 'script') {
75
+ const src = absURL(get('src') || '');
76
+ const module = get('type') === 'module';
77
+ for (const el of Array.from(document.querySelectorAll('script[src]'))) {
78
+ if (el.src === src && (el.type === 'module') === module)
79
+ return true;
80
+ }
81
+ return false;
82
+ }
83
+ const rel = get('rel') || '';
84
+ const href = absURL(get('href') || '');
85
+ const as = get('as');
86
+ for (const el of Array.from(document.querySelectorAll('link[href]'))) {
87
+ if (el.href !== href)
88
+ continue;
89
+ if ((el.getAttribute('rel') || '').toLowerCase() !== rel)
90
+ continue;
91
+ if (rel === 'preload' && (el.getAttribute('as') || '') !== (as || ''))
92
+ continue;
93
+ return true;
94
+ }
95
+ return false;
96
+ }
97
+ function tagText(tag, attrs) {
98
+ const text = attrs.map(([k, v]) => (v === '' && k !== 'crossorigin' ? ' ' + k : ' ' + k + '="' + escapeAttr(v) + '"')).join('');
99
+ return tag === 'link' ? '<link' + text + '>' : '<script' + text + '></script>';
100
+ }
101
+ // emit adds one head tag: to the document in a browser, to the render scope
102
+ // (or the build's collector) on the server. A tag that is already there is
103
+ // not added twice.
104
+ function emit(tag, attrs) {
105
+ if (typeof document === 'undefined') {
106
+ serverHints()?.add(tagText(tag, attrs));
12
107
  return;
13
- const link = document.createElement('link');
14
- link.rel = rel;
15
- link.href = href;
16
- if (opts) {
17
- if (opts.as)
18
- link.as = opts.as;
19
- if (opts.crossOrigin !== undefined)
20
- link.crossOrigin = opts.crossOrigin;
21
- if (opts.integrity)
22
- link.integrity = opts.integrity;
23
- if (opts.type)
24
- link.type = opts.type;
25
- if (opts.media)
26
- link.media = opts.media;
27
- if (opts.nonce)
28
- link.nonce = opts.nonce;
29
- if (opts.fetchPriority)
30
- link.setAttribute('fetchpriority', opts.fetchPriority);
31
- if (opts.referrerPolicy)
32
- link.referrerPolicy = opts.referrerPolicy;
33
- if (opts.imageSrcSet)
34
- link.setAttribute('imagesrcset', opts.imageSrcSet);
35
- if (opts.imageSizes)
36
- link.setAttribute('imagesizes', opts.imageSizes);
37
108
  }
38
- document.head.appendChild(link);
109
+ if (present(tag, attrs))
110
+ return;
111
+ const el = document.createElement(tag);
112
+ for (const [k, v] of attrs)
113
+ el.setAttribute(k, v);
114
+ document.head.appendChild(el);
115
+ }
116
+ function link(rel, href, opts, names) {
117
+ if (!href || typeof href !== 'string')
118
+ return;
119
+ emit('link', [['rel', rel], ['href', href], ...optAttrs(opts, names)]);
120
+ }
121
+ /**
122
+ * Records head tags the build collected for a component that the server does
123
+ * not render (a real-React client component). Server only; the browser
124
+ * already has them in the document.
125
+ */
126
+ export function krateHintTags(tags) {
127
+ if (typeof document !== 'undefined')
128
+ return;
129
+ const hints = serverHints();
130
+ if (hints)
131
+ for (const t of tags)
132
+ hints.add(t);
39
133
  }
40
134
  export function krateHintPreload(href, opts) {
41
- addLink('preload', href, opts);
135
+ link('preload', href, opts, ['as', 'crossOrigin', 'integrity', 'type', 'media', 'nonce', 'fetchPriority', 'referrerPolicy', 'imageSrcSet', 'imageSizes']);
42
136
  }
43
137
  export function krateHintPreloadModule(href, opts) {
44
- addLink('modulepreload', href, opts);
138
+ link('modulepreload', href, opts, ['as', 'crossOrigin', 'integrity', 'nonce']);
45
139
  }
46
140
  export function krateHintPreconnect(href, opts) {
47
- addLink('preconnect', href, opts);
141
+ link('preconnect', href, opts, ['crossOrigin']);
48
142
  }
49
143
  export function krateHintPrefetchDNS(href) {
50
- addLink('dns-prefetch', href);
144
+ link('dns-prefetch', href, undefined, []);
51
145
  }
52
146
  export function krateHintPreinit(href, opts) {
53
- if (typeof document === 'undefined' || !href)
147
+ if (!href || typeof href !== 'string')
54
148
  return;
55
149
  if (opts && opts.as === 'script') {
56
- if (headHas('script[src="' + href + '"]'))
57
- return;
58
- const s = document.createElement('script');
59
- s.src = href;
60
- s.async = true;
61
- if (opts.crossOrigin !== undefined)
62
- s.crossOrigin = opts.crossOrigin;
63
- if (opts.integrity)
64
- s.integrity = opts.integrity;
65
- if (opts.nonce)
66
- s.nonce = opts.nonce;
67
- document.head.appendChild(s);
150
+ emit('script', [['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce', 'fetchPriority'])]);
68
151
  return;
69
152
  }
70
- // as: "style" (the default use): load the stylesheet.
71
- if (headHas('link[rel="stylesheet"][href="' + href + '"]'))
72
- return;
73
- const link = document.createElement('link');
74
- link.rel = 'stylesheet';
75
- link.href = href;
76
- if (opts && opts.crossOrigin !== undefined)
77
- link.crossOrigin = opts.crossOrigin;
78
- if (opts && opts.integrity)
79
- link.integrity = opts.integrity;
80
- if (opts && opts.media)
81
- link.media = opts.media;
82
- document.head.appendChild(link);
153
+ // as: "style" (the default use) loads the stylesheet.
154
+ link('stylesheet', href, opts, ['crossOrigin', 'integrity', 'media', 'nonce', 'fetchPriority']);
83
155
  }
84
156
  export function krateHintPreinitModule(href, opts) {
85
- if (typeof document === 'undefined' || !href)
157
+ if (!href || typeof href !== 'string')
86
158
  return;
87
- if (headHas('script[type="module"][src="' + href + '"]'))
88
- return;
89
- const s = document.createElement('script');
90
- s.type = 'module';
91
- s.src = href;
92
- if (opts && opts.crossOrigin !== undefined)
93
- s.crossOrigin = opts.crossOrigin;
94
- if (opts && opts.integrity)
95
- s.integrity = opts.integrity;
96
- document.head.appendChild(s);
159
+ emit('script', [['type', 'module'], ['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce'])]);
160
+ }
161
+ // The react-dom exports a server module can use. flushSync(fn) is fn() here.
162
+ export const preload = krateHintPreload;
163
+ export const preinit = krateHintPreinit;
164
+ export const preconnect = krateHintPreconnect;
165
+ export const prefetchDNS = krateHintPrefetchDNS;
166
+ export const preloadModule = krateHintPreloadModule;
167
+ export const preinitModule = krateHintPreinitModule;
168
+ export function flushSync(fn) {
169
+ return fn();
97
170
  }
171
+ export default { preload, preinit, preconnect, prefetchDNS, preloadModule, preinitModule, flushSync };
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export { clearPersisted, readPersisted, writePersisted } from './storage.js';
10
10
  export type { PersistOptions, PersistSpec, PersistStore, StorageLike } from './storage.js';
11
11
  export { h, mount, hydrate, insert, clearNodes, disposeNode, krateApplyStyle } from './dom.js';
12
12
  export { krateList, krateBindList } from './list.js';
13
+ export type { KrateListValue } from './list.js';
13
14
  export { krateHighlightInit, krateHighlight } from './highlight-bind.js';
14
15
  export { krateCondSwap, krateCondDetached, krateCondReset, krateSanitizeAttr, BLOCKED_URL } from './slots.js';
15
16
  export { Fragment, jsx, jsxs } from './jsx-runtime.js';
@@ -0,0 +1,45 @@
1
+ /** A server-children slot a Preact island adopted from the document. */
2
+ export interface IslandSlot {
3
+ id: string;
4
+ /** The slot's server HTML (outerHTML of its nodes) when it was adopted. */
5
+ html: string;
6
+ /** The live top-level nodes of the slot (the island keeps the array current). */
7
+ set: Node[];
8
+ }
9
+ /** What an island chunk's mount returns. */
10
+ export interface IslandHandle {
11
+ update(props: unknown, pool?: Record<string, Node[][]>): unknown;
12
+ unmount(): void;
13
+ slots?: IslandSlot[];
14
+ }
15
+ /** An island record of window.__krateIslands. */
16
+ export interface IslandRecord {
17
+ status?: string;
18
+ handle?: IslandHandle | null;
19
+ gone?: boolean;
20
+ }
21
+ /** Serializes nodes as the island chunks snapshot a slot's server HTML. */
22
+ export declare function nodesHTML(nodes: ArrayLike<Node>): string;
23
+ /**
24
+ * HTML with every island replaced by a marker naming its component, so two
25
+ * renders compare equal when only the islands (their props, or what they
26
+ * rendered since) differ.
27
+ */
28
+ export declare function maskIslands(html: string): string;
29
+ /**
30
+ * Unmounts every island in nodes, innermost first, so their effects and
31
+ * portals are cleaned up before the nodes leave the document. An island still
32
+ * loading is marked gone (the loader unmounts it once its chunk mounted).
33
+ */
34
+ export declare function unmountIslandsIn(nodes: ArrayLike<Node>): void;
35
+ export interface SwapOptions {
36
+ /** Only islands whose server props are unchanged are kept (a navigation to another page). */
37
+ sameProps?: boolean;
38
+ }
39
+ /**
40
+ * Replaces the live children of parent between start and end (exclusive;
41
+ * all children when both are null) with newNodes (imported into the
42
+ * document, detached), keeping matched islands mounted. Returns the pending
43
+ * work of island updates (a re-render that loads more chunks first).
44
+ */
45
+ export declare function swapPreservingIslands(parent: Node, start: Node | null, end: Node | null, newNodes: Node[], opts?: SwapOptions): Promise<unknown>[];