@hamur/adapter-solid 0.0.0-stage → 0.0.2
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/LICENSE +22 -0
- package/README.md +35 -2
- package/dist/chunks/browser-index-8rhv2s58.js +14 -0
- package/dist/chunks/browser-index-b05qbzsh.js +165 -0
- package/dist/chunks/browser-index-n80jt19y.js +114 -0
- package/dist/chunks/browser-index-ncnkshd9.js +39 -0
- package/dist/chunks/browser-index-r4jwfgh7.js +407 -0
- package/dist/src/adapter.js +556 -0
- package/dist/src/error-page.js +466 -0
- package/dist/src/index.js +1535 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +174 -0
- package/dist/src/runtime/island.js +58 -0
- package/dist/src/runtime/server.js +138 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +4 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-component.d.ts +10 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/context.d.ts +89 -0
- package/dist/types/src/error-page.d.ts +1 -0
- package/dist/types/src/index.d.ts +392 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/island.d.ts +20 -0
- package/dist/types/src/jsx-locator.d.ts +6 -0
- package/dist/types/src/keepalive.d.ts +30 -0
- package/dist/types/src/lazy-hydration.d.ts +13 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +13 -0
- package/dist/types/src/plugin.d.ts +44 -0
- package/dist/types/src/runtime/client.d.ts +13 -0
- package/dist/types/src/runtime/hmr-solid.d.ts +5 -0
- package/dist/types/src/runtime/hmr.d.ts +27 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +10 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/portal.d.ts +13 -0
- package/dist/types/src/runtime/server.d.ts +14 -0
- package/dist/types/src/runtime/trusted.d.ts +2 -0
- package/dist/types/src/server-island.d.ts +10 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +74 -4
- package/src/activation.ts +51 -0
- package/src/adapter.ts +42 -0
- package/src/async-data.ts +105 -0
- package/src/client-component.ts +40 -0
- package/src/client-fallback.ts +2 -0
- package/src/context.ts +495 -0
- package/src/env.d.ts +3 -0
- package/src/error-page.ts +1 -0
- package/src/index.ts +1900 -0
- package/src/island-html.ts +58 -0
- package/src/island.ts +55 -0
- package/src/jsx-locator.ts +199 -0
- package/src/keepalive.ts +147 -0
- package/src/lazy-hydration.ts +146 -0
- package/src/markdown-page.ts +18 -0
- package/src/markdown.ts +70 -0
- package/src/plugin.ts +310 -0
- package/src/runtime/client.ts +175 -0
- package/src/runtime/hmr-solid.ts +19 -0
- package/src/runtime/hmr.ts +231 -0
- package/src/runtime/hydration-dev.ts +41 -0
- package/src/runtime/hydration.ts +9 -0
- package/src/runtime/island.ts +66 -0
- package/src/runtime/portal.ts +64 -0
- package/src/runtime/server.ts +160 -0
- package/src/runtime/trusted.ts +16 -0
- package/src/server-island.ts +47 -0
- package/src/solid.d.ts +4 -0
- package/src/svg.ts +53 -0
- package/src/webmcp.d.ts +20 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { getOwner, onCleanup, untrack } from "solid-js"
|
|
2
|
+
import type { Component, Owner } from "solid-js"
|
|
3
|
+
import { remountRoot } from "./client.ts"
|
|
4
|
+
|
|
5
|
+
/** One rendered instance of a wrapped component: the reads of the signals and stores its own body created, in order. */
|
|
6
|
+
interface Instance {
|
|
7
|
+
key: string
|
|
8
|
+
module: string
|
|
9
|
+
owner: Owner | null
|
|
10
|
+
/** The wrapped instance whose body was running when this one was created: its parent, when Solid renders it in place. */
|
|
11
|
+
parent: Instance | undefined
|
|
12
|
+
pass: number
|
|
13
|
+
reads: Array<() => unknown>
|
|
14
|
+
restore: unknown[] | undefined
|
|
15
|
+
broken: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface Slot {
|
|
19
|
+
impl: Component<any>
|
|
20
|
+
wrapper: Component<any>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface HotModule {
|
|
24
|
+
readonly id: string
|
|
25
|
+
signature: string
|
|
26
|
+
version: string
|
|
27
|
+
/** The namespaces of the modules it imports: a new one means that module ran again. */
|
|
28
|
+
dependencies: readonly unknown[]
|
|
29
|
+
next?: { signature: string; version: string; dependencies: readonly unknown[] }
|
|
30
|
+
slots: Map<string, Slot>
|
|
31
|
+
component<P extends object>(name: string, impl: Component<P>): Component<P>
|
|
32
|
+
commit(): void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const stack: Instance[] = []
|
|
36
|
+
/** Live instances per key, in creation order. */
|
|
37
|
+
const live = new Map<string, Instance[]>()
|
|
38
|
+
/** Keys that lost an instance while others stayed: their positions no longer say which is which. */
|
|
39
|
+
const shuffled = new Set<string>()
|
|
40
|
+
const restoring: Instance[] = []
|
|
41
|
+
let snapshot: Map<string, unknown[]> | undefined
|
|
42
|
+
const queued = new Set<HotModule>()
|
|
43
|
+
let pass = 0
|
|
44
|
+
let passOpen = false
|
|
45
|
+
const created = new Map<string, number>()
|
|
46
|
+
|
|
47
|
+
/** Instances created in one synchronous job share a pass: one render, in render order. */
|
|
48
|
+
function currentPass(): number {
|
|
49
|
+
if (!passOpen) {
|
|
50
|
+
passOpen = true
|
|
51
|
+
pass++
|
|
52
|
+
created.clear()
|
|
53
|
+
queueMicrotask(() => void (passOpen = false))
|
|
54
|
+
}
|
|
55
|
+
return pass
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const nullish = (value: unknown) => value === undefined || value === null
|
|
59
|
+
|
|
60
|
+
function owned(): Instance | undefined {
|
|
61
|
+
const instance = stack[stack.length - 1]
|
|
62
|
+
return instance && instance.owner === getOwner() ? instance : undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The value a shimmed `createSignal`/`createStore` starts from: the previous render's value at this position, or `initial`. */
|
|
66
|
+
export function restored<T>(initial: T): T {
|
|
67
|
+
const instance = owned()
|
|
68
|
+
if (!instance?.restore || instance.broken) return initial
|
|
69
|
+
const index = instance.reads.length
|
|
70
|
+
if (index >= instance.restore.length) return initial
|
|
71
|
+
const previous = instance.restore[index]
|
|
72
|
+
if (!nullish(previous) && !nullish(initial) && typeof previous !== typeof initial) {
|
|
73
|
+
instance.broken = true
|
|
74
|
+
return initial
|
|
75
|
+
}
|
|
76
|
+
return previous as T
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Records how to read a signal or store the current instance's own body just created. */
|
|
80
|
+
export function track(read: () => unknown): void {
|
|
81
|
+
owned()?.reads.push(read)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function open(key: string, module: string): Instance {
|
|
85
|
+
const at = currentPass()
|
|
86
|
+
const index = created.get(key) ?? 0
|
|
87
|
+
created.set(key, index + 1)
|
|
88
|
+
const restore = snapshot?.get(`${key}:${index}`)
|
|
89
|
+
const instance: Instance = {
|
|
90
|
+
key,
|
|
91
|
+
module,
|
|
92
|
+
owner: getOwner(),
|
|
93
|
+
parent: stack[stack.length - 1],
|
|
94
|
+
pass: at,
|
|
95
|
+
reads: [],
|
|
96
|
+
restore,
|
|
97
|
+
broken: false,
|
|
98
|
+
}
|
|
99
|
+
if (restore) restoring.push(instance)
|
|
100
|
+
let instances = live.get(key)
|
|
101
|
+
if (!instances) live.set(key, (instances = []))
|
|
102
|
+
instances.push(instance)
|
|
103
|
+
onCleanup(() => {
|
|
104
|
+
const list = live.get(key)
|
|
105
|
+
const position = list?.indexOf(instance) ?? -1
|
|
106
|
+
if (position === -1) return
|
|
107
|
+
list!.splice(position, 1)
|
|
108
|
+
if (list!.length === 0) shuffled.delete(key)
|
|
109
|
+
else shuffled.add(key)
|
|
110
|
+
})
|
|
111
|
+
return instance
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Values to carry over, by `<key>:<position>`. Position only identifies an instance among siblings rendered in one
|
|
116
|
+
* pass under one parent instance; a key whose instances are not (rows of a `<For>`, an instance added or removed since,
|
|
117
|
+
* siblings whose parent is unknown because Solid rendered them after the parent's body returned) starts fresh, since
|
|
118
|
+
* a wrong value is worse than a lost one.
|
|
119
|
+
*/
|
|
120
|
+
function capture(skip: (instance: Instance) => boolean): Map<string, unknown[]> {
|
|
121
|
+
const values = new Map<string, unknown[]>()
|
|
122
|
+
for (const [key, instances] of live) {
|
|
123
|
+
const first = instances[0]
|
|
124
|
+
if (
|
|
125
|
+
!first ||
|
|
126
|
+
shuffled.has(key) ||
|
|
127
|
+
instances.some(
|
|
128
|
+
(instance) =>
|
|
129
|
+
instance.pass !== first.pass ||
|
|
130
|
+
instance.parent !== first.parent ||
|
|
131
|
+
(instances.length > 1 && !instance.parent),
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
continue
|
|
135
|
+
instances.forEach((instance, position) => {
|
|
136
|
+
if (!skip(instance))
|
|
137
|
+
values.set(
|
|
138
|
+
`${key}:${position}`,
|
|
139
|
+
instance.reads.map((read) => untrack(read)),
|
|
140
|
+
)
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
return values
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function remount(values: Map<string, unknown[]>): void {
|
|
147
|
+
snapshot = values
|
|
148
|
+
restoring.length = 0
|
|
149
|
+
passOpen = false
|
|
150
|
+
try {
|
|
151
|
+
remountRoot()
|
|
152
|
+
} finally {
|
|
153
|
+
snapshot = undefined
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function flush(): void {
|
|
158
|
+
const reset = new Set<string>()
|
|
159
|
+
for (const module of queued) {
|
|
160
|
+
const { signature, version, dependencies } = module.next!
|
|
161
|
+
const dependencyChanged =
|
|
162
|
+
version === module.version && dependencies.some((namespace, index) => namespace !== module.dependencies[index])
|
|
163
|
+
module.next = undefined
|
|
164
|
+
if (signature !== module.signature || dependencyChanged) reset.add(module.id)
|
|
165
|
+
module.signature = signature
|
|
166
|
+
module.version = version
|
|
167
|
+
module.dependencies = dependencies
|
|
168
|
+
}
|
|
169
|
+
queued.clear()
|
|
170
|
+
try {
|
|
171
|
+
remount(capture((instance) => reset.has(instance.module)))
|
|
172
|
+
const mismatched = new Set(
|
|
173
|
+
restoring
|
|
174
|
+
.filter((instance) => instance.reads.length !== instance.restore!.length)
|
|
175
|
+
.map((instance) => instance.key),
|
|
176
|
+
)
|
|
177
|
+
if (mismatched.size > 0) remount(capture((instance) => mismatched.has(instance.key)))
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error(error)
|
|
180
|
+
location.reload()
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** One per module version, `previous` is what the module's last version returned. */
|
|
185
|
+
export function hotModule(
|
|
186
|
+
previous: HotModule | undefined,
|
|
187
|
+
id: string,
|
|
188
|
+
signature: string,
|
|
189
|
+
version: string,
|
|
190
|
+
dependencies: readonly unknown[] = [],
|
|
191
|
+
): HotModule {
|
|
192
|
+
if (previous) {
|
|
193
|
+
previous.next = { signature, version, dependencies }
|
|
194
|
+
return previous
|
|
195
|
+
}
|
|
196
|
+
const module: HotModule = {
|
|
197
|
+
id,
|
|
198
|
+
signature,
|
|
199
|
+
version,
|
|
200
|
+
dependencies,
|
|
201
|
+
slots: new Map(),
|
|
202
|
+
component(name, impl) {
|
|
203
|
+
let slot = module.slots.get(name)
|
|
204
|
+
if (slot) {
|
|
205
|
+
slot.impl = impl
|
|
206
|
+
} else {
|
|
207
|
+
const key = `${id}#${name}`
|
|
208
|
+
const fresh: Slot = {
|
|
209
|
+
impl,
|
|
210
|
+
wrapper: (props) =>
|
|
211
|
+
untrack(() => {
|
|
212
|
+
stack.push(open(key, id))
|
|
213
|
+
try {
|
|
214
|
+
return fresh.impl(props)
|
|
215
|
+
} finally {
|
|
216
|
+
stack.pop()
|
|
217
|
+
}
|
|
218
|
+
}),
|
|
219
|
+
}
|
|
220
|
+
module.slots.set(name, (slot = fresh))
|
|
221
|
+
}
|
|
222
|
+
return slot.wrapper
|
|
223
|
+
},
|
|
224
|
+
commit() {
|
|
225
|
+
if (!module.next || queued.has(module)) return
|
|
226
|
+
if (queued.size === 0) queueMicrotask(flush)
|
|
227
|
+
queued.add(module)
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
return module
|
|
231
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { snapshotHydration } from "hamur/adapter/dev"
|
|
2
|
+
import { devHydration } from "./hydration.ts"
|
|
3
|
+
|
|
4
|
+
// @solidjs/web throws it when the client renders a node the server markup lacks and has no template (getNextElement).
|
|
5
|
+
const MISMATCH = "Hydration Mismatch. Unable to find DOM nodes for hydration key"
|
|
6
|
+
// With a template it only warns and creates the element detached, so the page keeps the server's markup (web.dev.js).
|
|
7
|
+
const MISS = "Hydration key miss for "
|
|
8
|
+
|
|
9
|
+
/** Solid keeps the server's text and attributes while it hydrates, so only the nodes it cannot find are reported. */
|
|
10
|
+
devHydration.watch = (root) => {
|
|
11
|
+
const snapshot = snapshotHydration(root)
|
|
12
|
+
let signalled = false
|
|
13
|
+
let missed = false
|
|
14
|
+
let finished = false
|
|
15
|
+
const warn = console.warn
|
|
16
|
+
console.warn = (...args: unknown[]) => {
|
|
17
|
+
if (typeof args[0] === "string" && args[0].startsWith(MISS)) missed = true
|
|
18
|
+
warn.apply(console, args)
|
|
19
|
+
}
|
|
20
|
+
const settle = () => {
|
|
21
|
+
if (!signalled || !finished) return
|
|
22
|
+
signalled = false
|
|
23
|
+
snapshot.compare(undefined, undefined, true)
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
failed(error) {
|
|
27
|
+
if (!(error instanceof Error) || !error.message.startsWith(MISMATCH)) return
|
|
28
|
+
signalled = true
|
|
29
|
+
settle()
|
|
30
|
+
},
|
|
31
|
+
done(remount) {
|
|
32
|
+
console.warn = warn
|
|
33
|
+
finished = true
|
|
34
|
+
if (missed) {
|
|
35
|
+
remount()
|
|
36
|
+
signalled = true
|
|
37
|
+
}
|
|
38
|
+
settle()
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** What `hydration-dev.ts` hands the client runtime in `hamur dev`; a production client never sets it. */
|
|
2
|
+
export interface HydrationWatch {
|
|
3
|
+
/** An error the root boundary caught; Solid's own hydration mismatch error is reported. */
|
|
4
|
+
failed(error: unknown): void
|
|
5
|
+
/** `hydrate` returned; `remount` renders the root again on the client, for a mismatch Solid only warned about. */
|
|
6
|
+
done(remount: () => void): void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const devHydration: { watch?: (root: Element) => HydrationWatch } = {}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Errored, createComponent, createSignal, untrack } from "solid-js"
|
|
2
|
+
import type { Accessor } from "solid-js"
|
|
3
|
+
import { hydrate, render } from "@solidjs/web"
|
|
4
|
+
import type { JSX } from "@solidjs/web"
|
|
5
|
+
import { reportClientError } from "hamur/adapter"
|
|
6
|
+
import type { IslandContext } from "hamur/adapter"
|
|
7
|
+
import { Root, islandState } from "../context.ts"
|
|
8
|
+
import type { AppState, IslandSlot } from "../context.ts"
|
|
9
|
+
import { trustTemplates } from "./trusted.ts"
|
|
10
|
+
|
|
11
|
+
const disposers = new WeakMap<Element, () => void>()
|
|
12
|
+
|
|
13
|
+
const updaters = new WeakMap<Element, (props: Record<string, unknown>) => void>()
|
|
14
|
+
|
|
15
|
+
/** The island's root inside the `Errored` the server's `renderComponent` also renders, so the keys agree; a failure reports and renders nothing. */
|
|
16
|
+
const root = (el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext) => {
|
|
17
|
+
const kept: IslandSlot = {}
|
|
18
|
+
const [state, setState] = createSignal<AppState>(islandState(component, props, ctx, kept))
|
|
19
|
+
updaters.set(el, (own) => setState(islandState(component, own, ctx, kept, true)))
|
|
20
|
+
return (): JSX.Element =>
|
|
21
|
+
createComponent(Errored, {
|
|
22
|
+
fallback: (error: Accessor<unknown>) => {
|
|
23
|
+
reportClientError(ctx.app, untrack(error), { render: true })
|
|
24
|
+
return undefined
|
|
25
|
+
},
|
|
26
|
+
get children() {
|
|
27
|
+
return createComponent(Root, { state })
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Hydrates the island's server HTML in `el` as a root of its own, its hydration keys under `<idPrefix>-`. */
|
|
33
|
+
export function hydrateIsland(
|
|
34
|
+
el: Element,
|
|
35
|
+
component: unknown,
|
|
36
|
+
props: Record<string, unknown>,
|
|
37
|
+
ctx: IslandContext,
|
|
38
|
+
): void {
|
|
39
|
+
trustTemplates()
|
|
40
|
+
// Solid marks the page's hydration done once it settles, after which `hydrate` renders anew; each island still hydrates its own server HTML.
|
|
41
|
+
const marks = globalThis as { _$HY?: { done?: boolean } }
|
|
42
|
+
if (marks._$HY) marks._$HY.done = false
|
|
43
|
+
disposers.set(el, hydrate(root(el, component, props, ctx), el, { renderId: `${ctx.idPrefix}-` }))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Renders the island into `el` in place of the fallback it holds. */
|
|
47
|
+
export function mountIsland(el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext): void {
|
|
48
|
+
trustTemplates()
|
|
49
|
+
el.replaceChildren()
|
|
50
|
+
disposers.set(el, render(root(el, component, props, ctx), el))
|
|
51
|
+
// Solid's hydration script holds back a click whose nearest `_hk` element has not hydrated until it does; the island's element and the page around it never hydrate.
|
|
52
|
+
const marks = globalThis as { _$HY?: { completed?: WeakSet<Element> | null } }
|
|
53
|
+
const held = el.closest("[_hk]")
|
|
54
|
+
if (held) marks._$HY?.completed?.add(held)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function unmountIsland(el: Element): void {
|
|
58
|
+
disposers.get(el)?.()
|
|
59
|
+
disposers.delete(el)
|
|
60
|
+
updaters.delete(el)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Gives the island's root a new state with the new props, which keeps the state its components hold. */
|
|
64
|
+
export function updateIsland(el: Element, props: Record<string, unknown>): void {
|
|
65
|
+
updaters.get(el)?.(props)
|
|
66
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { NoHydration, createComponent, getOwner, runWithOwner, untrack, useContext } from "solid-js"
|
|
2
|
+
import type { Element as SolidElement } from "solid-js"
|
|
3
|
+
import { createOwner } from "solid-js/internal"
|
|
4
|
+
import { escape, resolveSSRNode } from "@solidjs/web"
|
|
5
|
+
import { AppContext } from "../context.ts"
|
|
6
|
+
|
|
7
|
+
/** Where the server `Portal` writes: one entry per target selector (`#teleports`, `body`), in render order. */
|
|
8
|
+
export type TeleportSink = Record<string, string>
|
|
9
|
+
|
|
10
|
+
class Mount {
|
|
11
|
+
constructor(readonly selector: string) {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const mountDocument = {
|
|
15
|
+
body: new Mount("body"),
|
|
16
|
+
getElementById: (id: string) => new Mount(`#${id}`),
|
|
17
|
+
querySelector: (selector: string) => new Mount(selector),
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The selector a `mount` getter names: it reads `document`, which the server does not have, so it sees a stand-in for this one synchronous read. */
|
|
21
|
+
function targetOf(mount: () => unknown): string {
|
|
22
|
+
const saved = Object.getOwnPropertyDescriptor(globalThis, "document")
|
|
23
|
+
Object.defineProperty(globalThis, "document", { value: mountDocument, configurable: true, writable: true })
|
|
24
|
+
try {
|
|
25
|
+
const value = mount()
|
|
26
|
+
return value instanceof Mount ? value.selector : "body"
|
|
27
|
+
} catch {
|
|
28
|
+
return "body"
|
|
29
|
+
} finally {
|
|
30
|
+
if (saved) Object.defineProperty(globalThis, "document", saved)
|
|
31
|
+
else delete (globalThis as { document?: unknown }).document
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The server `Portal`: @solidjs/web's renders nothing. Like the client's `Portal`, it takes one child owner of
|
|
37
|
+
* the owner it renders in, so the hydration keys after it are the client's; its children render inside that owner with
|
|
38
|
+
* no hydration keys, as the client builds a portal after hydration, and land in a `div` that the client entry removes
|
|
39
|
+
* before it hydrates; the client `Portal` then creates its own.
|
|
40
|
+
*/
|
|
41
|
+
export function Portal(props: { mount?: unknown; children?: SolidElement }): string {
|
|
42
|
+
const state = useContext(AppContext)
|
|
43
|
+
const sink = state ? untrack(state).teleports : undefined
|
|
44
|
+
if (!getOwner()) return ""
|
|
45
|
+
return runWithOwner(createOwner(), () => {
|
|
46
|
+
if (!sink) return ""
|
|
47
|
+
const target = targetOf(() => props.mount)
|
|
48
|
+
const node = resolveSSRNode(
|
|
49
|
+
escape(
|
|
50
|
+
createComponent(NoHydration, {
|
|
51
|
+
get children() {
|
|
52
|
+
return props.children
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
55
|
+
),
|
|
56
|
+
) as { t: string[]; h: unknown[] }
|
|
57
|
+
if (node.h.length)
|
|
58
|
+
throw new Error(
|
|
59
|
+
"A Portal's children render synchronously on the server: put what they wait for in a Loading boundary inside the Portal's own component.",
|
|
60
|
+
)
|
|
61
|
+
sink[target] = `${sink[target] ?? ""}<div data-hamur-portal>${node.t[0]}</div>`
|
|
62
|
+
return ""
|
|
63
|
+
})
|
|
64
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Errored, NoHydration, createComponent, createUniqueId, omit, untrack } from "solid-js"
|
|
2
|
+
import type { Accessor, Component } from "solid-js"
|
|
3
|
+
import { dynamic, generateHydrationScript, markSafeError, renderToStream, renderToString } from "@solidjs/web"
|
|
4
|
+
import { sharedConfig } from "solid-js/internal"
|
|
5
|
+
import type { ServerErrorHook } from "@solidjs/web"
|
|
6
|
+
import {
|
|
7
|
+
cspHash,
|
|
8
|
+
renderInPasses,
|
|
9
|
+
serverComponentAttributes,
|
|
10
|
+
serverComponentProps,
|
|
11
|
+
setAdapterInstance,
|
|
12
|
+
} from "hamur/adapter"
|
|
13
|
+
import type { ComponentRenderContext, RenderContext, RenderResult } from "hamur/adapter"
|
|
14
|
+
import type { AppContext } from "hamur/app"
|
|
15
|
+
import { Root, appState, installKeepAlive, islandState, originalOf, redirectOf, serializableError } from "../context.ts"
|
|
16
|
+
import type { AppState } from "../context.ts"
|
|
17
|
+
import * as keepAlive from "../keepalive.ts"
|
|
18
|
+
import { collectingIslands, islandsSettled, renderSettled } from "../island-html.ts"
|
|
19
|
+
import { lazyModules } from "../lazy-hydration.ts"
|
|
20
|
+
import type { TeleportSink } from "./portal.ts"
|
|
21
|
+
|
|
22
|
+
installKeepAlive(keepAlive)
|
|
23
|
+
|
|
24
|
+
const ServerComponentElement = dynamic(() => "hamur-component", { static: true }) as Component<Record<string, unknown>>
|
|
25
|
+
|
|
26
|
+
/** A failure Solid handles reaches the HTML and the hydration data masked; one that fails the render stays a throw for `render`. */
|
|
27
|
+
const onErrorOf =
|
|
28
|
+
(app: AppContext): ServerErrorHook =>
|
|
29
|
+
(error, { handling }) =>
|
|
30
|
+
handling === "failed" ? undefined : serializableError(app, error)
|
|
31
|
+
|
|
32
|
+
type Serialize = (id: string, value: unknown, ...rest: unknown[]) => void
|
|
33
|
+
|
|
34
|
+
const maskedContexts = new WeakSet<object>()
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Solid encodes a rejected async source's serialized rejection before any boundary hears it, with its
|
|
38
|
+
* default `new Error("Internal Server Error")`, which on Bun carries `sourceURL`, `line` and `column`. The render's own
|
|
39
|
+
* context writes that rejection as the masked error instead.
|
|
40
|
+
*/
|
|
41
|
+
function maskRejections(app: AppContext): void {
|
|
42
|
+
const context = sharedConfig.context as { serialize?: Serialize } | undefined
|
|
43
|
+
const serialize = context?.serialize
|
|
44
|
+
if (!context || !serialize || maskedContexts.has(context)) return
|
|
45
|
+
maskedContexts.add(context)
|
|
46
|
+
context.serialize = (id, value, ...rest) => {
|
|
47
|
+
const thenable =
|
|
48
|
+
typeof value === "object" && value !== null && typeof (value as PromiseLike<unknown>).then === "function"
|
|
49
|
+
const masked = thenable
|
|
50
|
+
? Promise.resolve(value as PromiseLike<unknown>).then(undefined, (error: unknown) =>
|
|
51
|
+
Promise.reject(markSafeError(serializableError(app, error))),
|
|
52
|
+
)
|
|
53
|
+
: value
|
|
54
|
+
serialize.call(context, id, masked, ...rest)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The client's root guard, which the server renders too so the hydration keys agree; here it lets every failure through. */
|
|
59
|
+
export function guardedRoot(state: Accessor<AppState>) {
|
|
60
|
+
return createComponent(Errored, {
|
|
61
|
+
fallback: (error: Accessor<unknown>) => {
|
|
62
|
+
throw originalOf(untrack(error))
|
|
63
|
+
},
|
|
64
|
+
get children() {
|
|
65
|
+
return createComponent(Root, { state })
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function render(ctx: RenderContext): Promise<RenderResult> {
|
|
71
|
+
await setAdapterInstance(ctx.app, null)
|
|
72
|
+
const base = appState(ctx)
|
|
73
|
+
const bare = ctx.route?.meta?.scripts === false
|
|
74
|
+
const options = { ...(ctx.nonce ? { nonce: ctx.nonce } : {}), onError: onErrorOf(ctx.app) }
|
|
75
|
+
const head = bare ? "" : generateHydrationScript(ctx.nonce ? { nonce: ctx.nonce } : {})
|
|
76
|
+
const hydration = ctx.scriptHashes && head.match(/^<script>([\s\S]*?)<\/script>/)?.[1]
|
|
77
|
+
if (hydration) ctx.scriptHashes!.push(await cspHash(hydration))
|
|
78
|
+
try {
|
|
79
|
+
return await renderInPasses<RenderResult>({
|
|
80
|
+
app: ctx.app,
|
|
81
|
+
head: base.head,
|
|
82
|
+
async render(tracked) {
|
|
83
|
+
await lazyModules()
|
|
84
|
+
const teleports: TeleportSink = {}
|
|
85
|
+
const app = () => {
|
|
86
|
+
maskRejections(ctx.app)
|
|
87
|
+
const root = () => guardedRoot(() => ({ ...base, head: tracked, teleports }))
|
|
88
|
+
return bare
|
|
89
|
+
? createComponent(NoHydration, {
|
|
90
|
+
get children() {
|
|
91
|
+
return root()
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
: root()
|
|
95
|
+
}
|
|
96
|
+
if (!ctx.stream) {
|
|
97
|
+
const pending: Promise<void>[] = []
|
|
98
|
+
const body = collectingIslands(pending, () => renderToString(app, options))
|
|
99
|
+
await islandsSettled(ctx.app, pending)
|
|
100
|
+
return { body, head, ...(Object.keys(teleports).length ? { teleports } : {}) }
|
|
101
|
+
}
|
|
102
|
+
const pending: Promise<void>[] = []
|
|
103
|
+
const body = collectingIslands(pending, () => renderToStream(app, options).readable)
|
|
104
|
+
if (pending.length) await islandsSettled(ctx.app, pending)
|
|
105
|
+
return { body, head, teleports }
|
|
106
|
+
},
|
|
107
|
+
drop: ({ body }) => void (typeof body === "string" ? undefined : body.cancel()),
|
|
108
|
+
})
|
|
109
|
+
} catch (error) {
|
|
110
|
+
throw redirectOf(error) ?? error
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** `component` as a root of its own, its hydration keys under `<idPrefix>-` as `hydrateIsland` reads them, with each island inside it rendered in place. */
|
|
115
|
+
export async function renderComponent(
|
|
116
|
+
component: unknown,
|
|
117
|
+
props: Record<string, unknown>,
|
|
118
|
+
ctx: ComponentRenderContext,
|
|
119
|
+
): Promise<{ html: string }> {
|
|
120
|
+
// `renderIsland` calls this inside the page's synchronous render, and Solid keeps one render context at a time.
|
|
121
|
+
await Promise.resolve()
|
|
122
|
+
await lazyModules()
|
|
123
|
+
const state = islandState(component, props, ctx)
|
|
124
|
+
try {
|
|
125
|
+
const html = await renderSettled(() =>
|
|
126
|
+
renderToString(() => guardedRoot(() => state), {
|
|
127
|
+
renderId: `${ctx.idPrefix}-`,
|
|
128
|
+
...(ctx.nonce ? { nonce: ctx.nonce } : {}),
|
|
129
|
+
onError: onErrorOf(ctx.app),
|
|
130
|
+
}),
|
|
131
|
+
)
|
|
132
|
+
return { html }
|
|
133
|
+
} catch (error) {
|
|
134
|
+
throw redirectOf(error) ?? error
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The server side of a client-only component renders nothing. */
|
|
139
|
+
export function clientComponent(): Component<Record<string, unknown>> {
|
|
140
|
+
return () => null
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
|
|
144
|
+
export function serverComponent(
|
|
145
|
+
name: string,
|
|
146
|
+
component: Component<Record<string, unknown>>,
|
|
147
|
+
): Component<Record<string, unknown>> {
|
|
148
|
+
return (props) => {
|
|
149
|
+
const uid = createUniqueId()
|
|
150
|
+
serverComponentProps(name, { ...omit(props, "children") }, props.children !== undefined)
|
|
151
|
+
return createComponent(ServerComponentElement, {
|
|
152
|
+
name,
|
|
153
|
+
uid,
|
|
154
|
+
...serverComponentAttributes(name),
|
|
155
|
+
get children() {
|
|
156
|
+
return createComponent(component, props)
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { setTrustedTypesPolicy } from "@solidjs/web"
|
|
2
|
+
|
|
3
|
+
type PolicyFactory = {
|
|
4
|
+
createPolicy(name: string, rules: { createHTML(html: string): string }): { createHTML(html: string): unknown }
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
let trusted = false
|
|
8
|
+
|
|
9
|
+
/** Under `server.csp.trustedTypes`, Solid writes its compiled templates through `solid-trusted-html` (`Adapter.trustedTypesPolicies`); called before the first `render` or `hydrate`. */
|
|
10
|
+
export function trustTemplates(): void {
|
|
11
|
+
if (trusted || (import.meta as { trustedTypes?: boolean }).trustedTypes !== true) return
|
|
12
|
+
const factory = (globalThis as { trustedTypes?: PolicyFactory }).trustedTypes
|
|
13
|
+
if (!factory) return
|
|
14
|
+
trusted = true
|
|
15
|
+
setTrustedTypesPolicy(factory.createPolicy("solid-trusted-html", { createHTML: (html) => html }))
|
|
16
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createComponent, createUniqueId, useContext } from "solid-js"
|
|
2
|
+
import type { Component } from "solid-js"
|
|
3
|
+
import { dynamic, isServer } from "@solidjs/web"
|
|
4
|
+
import type { JSX } from "@solidjs/web"
|
|
5
|
+
import { SERVER_ISLAND_ELEMENT, renderServerIsland, serverIslandProps } from "hamur/adapter"
|
|
6
|
+
import { AppContext } from "./context.ts"
|
|
7
|
+
import { settledIsland } from "./island-html.ts"
|
|
8
|
+
|
|
9
|
+
const ServerIslandElement = dynamic(() => SERVER_ISLAND_ELEMENT, { static: true }) as Component<Record<string, unknown>>
|
|
10
|
+
|
|
11
|
+
export interface ServerIslandProps<P extends Record<string, unknown> = Record<string, unknown>> {
|
|
12
|
+
/** The name of a component under `app/server-islands/`. */
|
|
13
|
+
name: string
|
|
14
|
+
props?: P
|
|
15
|
+
/** Rendered with the page; replaced by the island's HTML once the browser has it. */
|
|
16
|
+
fallback?: JSX.Element
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A component rendered on the server in its own request; the page holds its fallback, the core swaps the answer in, and Solid never renders or hydrates it. */
|
|
20
|
+
export function ServerIsland<P extends Record<string, unknown> = Record<string, unknown>>(
|
|
21
|
+
props: ServerIslandProps<P>,
|
|
22
|
+
): JSX.Element {
|
|
23
|
+
const state = useContext(AppContext)
|
|
24
|
+
if (!state) throw new Error("ServerIsland must be rendered inside a Hamur app")
|
|
25
|
+
const id = createUniqueId()
|
|
26
|
+
const app = state().app
|
|
27
|
+
if (isServer) {
|
|
28
|
+
const attributes = settledIsland(renderServerIsland(app, id, props.name, props.props).attributes) ?? {}
|
|
29
|
+
return createComponent(ServerIslandElement, {
|
|
30
|
+
...attributes,
|
|
31
|
+
get children() {
|
|
32
|
+
return props.fallback
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
// While hydrating, the server's element keeps its attributes and nodes: an element with no props and no children is left as it is.
|
|
37
|
+
if (app.asyncData.hydrating) return createComponent(ServerIslandElement, {})
|
|
38
|
+
const given =
|
|
39
|
+
props.props && Object.keys(props.props).length ? { props: serverIslandProps(props.name, props.props) } : {}
|
|
40
|
+
return createComponent(ServerIslandElement, {
|
|
41
|
+
name: props.name,
|
|
42
|
+
...given,
|
|
43
|
+
get children() {
|
|
44
|
+
return props.fallback
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
}
|
package/src/solid.d.ts
ADDED