@hamur/adapter-html 0.0.0-stage → 0.0.1
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 +31 -2
- package/dist/chunks/browser-index-2h1699xv.js +148 -0
- package/dist/chunks/browser-index-n6x04zrf.js +28 -0
- package/dist/chunks/browser-index-naq02zzj.js +4 -0
- package/dist/chunks/browser-index-qx74bd1y.js +302 -0
- package/dist/src/adapter.js +141 -0
- package/dist/src/error-page.js +335 -0
- package/dist/src/index.js +1241 -0
- package/dist/src/runtime/client.js +330 -0
- package/dist/src/runtime/island.js +40 -0
- package/dist/src/runtime/server.js +59 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +13 -0
- package/dist/types/src/context.d.ts +109 -0
- package/dist/types/src/error-page.d.ts +1 -0
- package/dist/types/src/index.d.ts +343 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/island.d.ts +40 -0
- package/dist/types/src/lazy-hydration.d.ts +11 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +11 -0
- package/dist/types/src/plugin.d.ts +11 -0
- package/dist/types/src/runtime/client.d.ts +15 -0
- package/dist/types/src/runtime/hmr.d.ts +8 -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/server.d.ts +8 -0
- package/dist/types/src/runtime/svg-id.d.ts +2 -0
- package/dist/types/src/server-component.d.ts +4 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +44 -4
- package/src/adapter.ts +25 -0
- package/src/async-data.ts +65 -0
- package/src/context.ts +445 -0
- package/src/env.d.ts +3 -0
- package/src/error-page.ts +1 -0
- package/src/index.ts +1620 -0
- package/src/island-html.ts +58 -0
- package/src/island.ts +126 -0
- package/src/lazy-hydration.ts +56 -0
- package/src/markdown-page.ts +17 -0
- package/src/markdown.ts +29 -0
- package/src/plugin.ts +70 -0
- package/src/runtime/client.ts +363 -0
- package/src/runtime/hmr.ts +38 -0
- package/src/runtime/hydration-dev.ts +12 -0
- package/src/runtime/hydration.ts +11 -0
- package/src/runtime/island.ts +52 -0
- package/src/runtime/server.ts +56 -0
- package/src/runtime/svg-id.ts +12 -0
- package/src/server-component.ts +30 -0
- package/src/svg.ts +54 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { AppContext, AsyncDataOptions, FetchOptions } from "hamur/app"
|
|
2
|
+
import { isPreviewMode } from "hamur/adapter"
|
|
3
|
+
import type { AsyncDataHandler, FetchRequest, GetCachedData, GetCachedDataCause, HandlerReference } from "hamur/adapter"
|
|
4
|
+
|
|
5
|
+
export const valueOf = <T>(value: T | (() => T)): T => (typeof value === "function" ? (value as () => T)() : value)
|
|
6
|
+
|
|
7
|
+
// A function is never a body to send, so a function `query` or `body` is a getter.
|
|
8
|
+
export function requestOf({ query, body, ...rest }: FetchRequest): FetchOptions {
|
|
9
|
+
const request: FetchOptions = rest
|
|
10
|
+
const resolvedQuery = valueOf(query)
|
|
11
|
+
const resolvedBody = valueOf(body)
|
|
12
|
+
if (resolvedQuery !== undefined) request.query = resolvedQuery
|
|
13
|
+
if (resolvedBody !== undefined) request.body = resolvedBody
|
|
14
|
+
return request
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The restored payload entry's data while the app hydrates, else `undefined`. */
|
|
18
|
+
export function defaultGetCachedData<T>(key: string, app: AppContext): T | undefined {
|
|
19
|
+
if (!app.asyncData.hydrating || isPreviewMode(app)) return undefined
|
|
20
|
+
const entry = app.asyncData.get<T>(key)
|
|
21
|
+
return entry?.status === "success" ? entry.data : undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `refresh()`, `execute()` and a `watch` change: ask `getCachedData` with their cause, run `handler` when it has nothing. */
|
|
25
|
+
export function refreshCached<T, R>(
|
|
26
|
+
app: AppContext,
|
|
27
|
+
key: string,
|
|
28
|
+
handler: AsyncDataHandler<R>,
|
|
29
|
+
options: AsyncDataOptions<T, R>,
|
|
30
|
+
getCachedData: GetCachedData<T>,
|
|
31
|
+
cause: GetCachedDataCause,
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const cached = getCachedData(key, app, { cause })
|
|
34
|
+
if (cached !== undefined) {
|
|
35
|
+
app.asyncData.fetch<T, R>(key, handler, { ...options, cached })
|
|
36
|
+
return Promise.resolve()
|
|
37
|
+
}
|
|
38
|
+
if (!app.asyncData.has(key)) app.asyncData.fetch<T, R>(key, handler, { ...options, immediate: false })
|
|
39
|
+
return app.asyncData.refresh(key, handler)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const kept = new WeakMap<object, Set<string>>()
|
|
43
|
+
|
|
44
|
+
// Nuxt purges no entry whose caller reads it back through its own getCachedData: the entry is that cache.
|
|
45
|
+
export function keepCached<T>(app: AppContext, key: string, getCachedData: GetCachedData<T>): void {
|
|
46
|
+
if (getCachedData === defaultGetCachedData) return
|
|
47
|
+
let keys = kept.get(app.asyncData)
|
|
48
|
+
if (!keys) kept.set(app.asyncData, (keys = new Set()))
|
|
49
|
+
if (keys.has(key)) return
|
|
50
|
+
keys.add(key)
|
|
51
|
+
app.asyncData.subscribe(key, () => {})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The run's signal, combined with a `signal` the caller gave to `useFetch`. */
|
|
55
|
+
export const signalOf = (own: AbortSignal | undefined, run: AbortSignal): AbortSignal =>
|
|
56
|
+
own ? AbortSignal.any([own, run]) : run
|
|
57
|
+
|
|
58
|
+
/** The call of `useAsyncData(fn, input)`: in process with the request's event on the server, through the stub in the browser. */
|
|
59
|
+
export const callHandler =
|
|
60
|
+
<R>(app: AppContext, fn: HandlerReference, input: unknown): AsyncDataHandler<R> =>
|
|
61
|
+
({ signal }) =>
|
|
62
|
+
(fn as (input: unknown, init: { signal: AbortSignal; event: AppContext["event"] }) => Promise<R>)(input, {
|
|
63
|
+
signal,
|
|
64
|
+
event: app.event,
|
|
65
|
+
})
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { withBase } from "hamur/app"
|
|
2
|
+
import type {
|
|
3
|
+
AppContext,
|
|
4
|
+
ClearErrorOptions,
|
|
5
|
+
ErrorPageProps,
|
|
6
|
+
Head,
|
|
7
|
+
HeadEntry,
|
|
8
|
+
HeadInput,
|
|
9
|
+
RouteLocation,
|
|
10
|
+
} from "hamur/app"
|
|
11
|
+
import type {
|
|
12
|
+
ClientContext,
|
|
13
|
+
Navigation,
|
|
14
|
+
NavigationState,
|
|
15
|
+
RenderContext,
|
|
16
|
+
RenderEntry,
|
|
17
|
+
RenderResult,
|
|
18
|
+
RenderTree,
|
|
19
|
+
RouteValue,
|
|
20
|
+
RouterApi,
|
|
21
|
+
} from "hamur/adapter"
|
|
22
|
+
import {
|
|
23
|
+
APP_ROOT_ID,
|
|
24
|
+
createLoadingIndicator,
|
|
25
|
+
createRouterApi,
|
|
26
|
+
routeBoundary,
|
|
27
|
+
type LoadingIndicatorSource,
|
|
28
|
+
type RouterSource,
|
|
29
|
+
} from "hamur/adapter"
|
|
30
|
+
import { routeStateOf } from "hamur/adapter"
|
|
31
|
+
|
|
32
|
+
/** The app state key a server render sets to ask `hydrate` to run the page again: a composable that must act in the browser (`useSyncedState` opens its socket, `usePwa` attaches its listener) sets it. */
|
|
33
|
+
export const RERUN_STATE_KEY = "hamur:rerun"
|
|
34
|
+
/** The inner level of a layout. It renders when it is turned into text: use `${children}` or `String(children)` before you compare it or test it for emptiness. */
|
|
35
|
+
export interface HtmlChildren {
|
|
36
|
+
toString(): string
|
|
37
|
+
valueOf(): string
|
|
38
|
+
toJSON(): string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface HtmlProps extends Record<string, unknown> {
|
|
42
|
+
route: RouteValue
|
|
43
|
+
children?: string | HtmlChildren
|
|
44
|
+
error?: RenderTree["error"]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type HtmlComponent = (props: HtmlProps) => string
|
|
48
|
+
|
|
49
|
+
type TreeContext = Pick<RenderContext | ClientContext, "route" | "tree"> &
|
|
50
|
+
Pick<RenderContext | ClientContext, "app" | "head"> &
|
|
51
|
+
Partial<Pick<RenderContext | ClientContext, "payload" | "baseURL">> &
|
|
52
|
+
Pick<RenderContext | ClientContext, "href"> & { navigator?: object }
|
|
53
|
+
|
|
54
|
+
/** A component's head input, pushed once its level rendered; `server` for `useServerHead`. */
|
|
55
|
+
export interface HeadPending {
|
|
56
|
+
input: HeadInput
|
|
57
|
+
options?: { server: true }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface Current {
|
|
61
|
+
route: RouteValue
|
|
62
|
+
data: Record<string, unknown>
|
|
63
|
+
handlerState?: unknown
|
|
64
|
+
tree: RenderTree
|
|
65
|
+
head: Head
|
|
66
|
+
entries?: HeadEntry[]
|
|
67
|
+
completesNavigation?: boolean
|
|
68
|
+
pending?: HeadPending[]
|
|
69
|
+
navigator?: { get?(): NavigationState } & Partial<RouterApi>
|
|
70
|
+
baseURL: string
|
|
71
|
+
href: RenderContext["href"]
|
|
72
|
+
app: AppContext
|
|
73
|
+
id?: string
|
|
74
|
+
/** The error of the route boundary being rendered, read by `useError()`. */
|
|
75
|
+
boundaryError?: ErrorPageProps
|
|
76
|
+
outlet?: () => string
|
|
77
|
+
/** The slots of the entry being rendered, each rendered once when its `Outlet({ name })` asks. */
|
|
78
|
+
slots?: Record<string, () => string>
|
|
79
|
+
/** The `context` of the nearest `Outlet({ context })` being rendered, read by `useOutletContext()`. */
|
|
80
|
+
outletValue?: unknown
|
|
81
|
+
/** The `state` of each `Link` this render wrote, by `linkStateKey`. */
|
|
82
|
+
linkStates?: Map<string, LinkEntry>
|
|
83
|
+
/** The removers of what this client render registered (guards, blockers, snapshots); they run before the next paint. */
|
|
84
|
+
scoped?: Array<() => void>
|
|
85
|
+
/** The placeholders of a streamed server render, whose values the core renders when they settle. */
|
|
86
|
+
deferred?: DeferredPart[]
|
|
87
|
+
/** Shared by the copies `renderIn` makes, so a deferred part continues the ids of the page. */
|
|
88
|
+
ids: { next: number }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type DeferredPart = NonNullable<RenderResult["deferred"]>[number]
|
|
92
|
+
|
|
93
|
+
/** What a render collects: a client render for the paint that follows it, a streamed server render its deferred parts. */
|
|
94
|
+
export interface RenderOut {
|
|
95
|
+
linkStates?: Map<string, LinkEntry>
|
|
96
|
+
scoped?: Array<() => void>
|
|
97
|
+
deferred?: DeferredPart[]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What a `Link` keeps for its anchor: its history `state` and its `onBeforeNavigate` handler. */
|
|
101
|
+
export interface LinkEntry {
|
|
102
|
+
state?: Record<string, unknown>
|
|
103
|
+
onBeforeNavigate?: (nav: Navigation) => void
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The occurrence of a `Link`'s `href` among the links with a `state` or an `onBeforeNavigate` of the render that wrote it; the client entry hands the state to `setLinkState` and removes the attribute. */
|
|
107
|
+
export const LINK_STATE_ATTRIBUTE = "data-hamur-link"
|
|
108
|
+
|
|
109
|
+
export const linkStateKey = (href: string, occurrence: string | number): string => `${href} ${occurrence}`
|
|
110
|
+
|
|
111
|
+
export const PAGE_MARKERS = ["hamur-page", "/hamur-page"] as const
|
|
112
|
+
export const LAYOUT_MARKERS = ["hamur-layout", "/hamur-layout"] as const
|
|
113
|
+
export const CLIENT_MARKERS = ["hamur-client", "/hamur-client"] as const
|
|
114
|
+
export const CLIENT_TEMPLATE = "template[data-hamur-client]"
|
|
115
|
+
|
|
116
|
+
const wrap = (markers: readonly [string, string], html: string): string =>
|
|
117
|
+
`<!--${markers[0]}-->${html}<!--${markers[1]}-->`
|
|
118
|
+
const isLayoutEntry = (id: string): boolean => id === APP_ROOT_ID || id.startsWith("layout:")
|
|
119
|
+
|
|
120
|
+
/** The entry whose output the layout markers wrap (the layout, else the app root) and the one the page markers wrap (the deepest entry that is neither). */
|
|
121
|
+
function markedEntries(components: RenderEntry[]): { layout: number; page: number } {
|
|
122
|
+
const layout = components.findIndex((entry) => entry.id.startsWith("layout:"))
|
|
123
|
+
return {
|
|
124
|
+
layout: layout >= 0 ? layout : components.findIndex((entry) => entry.id === APP_ROOT_ID),
|
|
125
|
+
page: components.findLastIndex((entry) => !isLayoutEntry(entry.id)),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let current: Current | undefined
|
|
130
|
+
let repaint: (() => void) | undefined
|
|
131
|
+
let paints = 0
|
|
132
|
+
let keepingData = false
|
|
133
|
+
|
|
134
|
+
export function setRepaint(next: (() => void) | undefined): void {
|
|
135
|
+
repaint = next
|
|
136
|
+
paints++
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function repaintPage(): void {
|
|
140
|
+
repaint?.()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function paintCount(): number {
|
|
144
|
+
return paints
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Paints again for data that settled after paint `count` unless another paint came since; its composables reuse their entries. */
|
|
148
|
+
export function repaintWithData(count: number): void {
|
|
149
|
+
if (count !== paints || !repaint) return
|
|
150
|
+
keepingData = true
|
|
151
|
+
try {
|
|
152
|
+
repaint()
|
|
153
|
+
} finally {
|
|
154
|
+
keepingData = false
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function isRepaintingWithData(): boolean {
|
|
159
|
+
return keepingData
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function routerOf(
|
|
163
|
+
navigator: unknown,
|
|
164
|
+
baseURL = "/",
|
|
165
|
+
page?: Omit<RouterSource, "navigator" | "baseURL">,
|
|
166
|
+
): RouterApi {
|
|
167
|
+
return createRouterApi({
|
|
168
|
+
...page,
|
|
169
|
+
navigator,
|
|
170
|
+
baseURL,
|
|
171
|
+
unavailable: () => new Error("useRouter() navigates only in the browser"),
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function currentOf(ctx: TreeContext): Current {
|
|
176
|
+
return {
|
|
177
|
+
route: routeValue(ctx),
|
|
178
|
+
data: (ctx.payload as { data?: Record<string, unknown> } | undefined)?.data ?? {},
|
|
179
|
+
handlerState: (ctx.payload as { handlerState?: unknown } | undefined)?.handlerState,
|
|
180
|
+
tree: ctx.tree,
|
|
181
|
+
head: ctx.head,
|
|
182
|
+
app: ctx.app,
|
|
183
|
+
baseURL: ctx.baseURL ?? "/",
|
|
184
|
+
href: ctx.href,
|
|
185
|
+
ids: { next: 0 },
|
|
186
|
+
...(ctx.navigator ? { navigator: ctx.navigator } : {}),
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function detachedHref(to: string | RouteLocation): string {
|
|
191
|
+
if (typeof to !== "string") throw new Error(`Link to the route "${to.name}" must be rendered inside a Hamur app`)
|
|
192
|
+
return withBase(to, "/")
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let indicatorRendered = false
|
|
196
|
+
|
|
197
|
+
/** Set by `LoadingIndicator()`; the runtime paints indicators only after a render that wrote one. */
|
|
198
|
+
export function markIndicatorRendered(): void {
|
|
199
|
+
indicatorRendered = true
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function takeIndicatorRendered(): boolean {
|
|
203
|
+
const rendered = indicatorRendered
|
|
204
|
+
indicatorRendered = false
|
|
205
|
+
return rendered
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let announcerRendered = false
|
|
209
|
+
|
|
210
|
+
/** Set by `RouteAnnouncer()` and `Announcer()`; the runtime binds the regions only after a render that wrote one. */
|
|
211
|
+
export function markAnnouncerRendered(): void {
|
|
212
|
+
announcerRendered = true
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function takeAnnouncerRendered(): boolean {
|
|
216
|
+
const rendered = announcerRendered
|
|
217
|
+
announcerRendered = false
|
|
218
|
+
return rendered
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let announcerClaims: Array<() => void> = []
|
|
222
|
+
|
|
223
|
+
/** html has no unmount: every paint starts by releasing the `RouteAnnouncer` claims of the one before. */
|
|
224
|
+
export function keepAnnouncerClaim(release: () => void): void {
|
|
225
|
+
announcerClaims.push(release)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function releaseAnnouncerClaims(): void {
|
|
229
|
+
const releases = announcerClaims
|
|
230
|
+
announcerClaims = []
|
|
231
|
+
for (const release of releases) release()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const followed = new WeakSet<object>()
|
|
235
|
+
|
|
236
|
+
/** Binds the loading indicator of `app` to `navigator` for the life of the app; html has no component lifecycle to remove it from. */
|
|
237
|
+
export function followNavigation(app: AppContext, navigator: unknown): void {
|
|
238
|
+
const source = navigator as Partial<LoadingIndicatorSource> | undefined
|
|
239
|
+
if (followed.has(app) || typeof source?.get !== "function" || typeof source.subscribe !== "function") return
|
|
240
|
+
followed.add(app)
|
|
241
|
+
createLoadingIndicator(app).follow(source as LoadingIndicatorSource)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function currentState(): Current | undefined {
|
|
245
|
+
return current
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function currentRoute(): RouteValue | undefined {
|
|
249
|
+
return current?.route
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function setCurrent(ctx: TreeContext | undefined): void {
|
|
253
|
+
current = ctx ? currentOf(ctx) : undefined
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Runs `render` in a copy of `state` taken during a render that has ended; it collects no deferred parts of its own. */
|
|
257
|
+
export function renderIn<T>(state: Current, render: () => T): T {
|
|
258
|
+
const previous = current
|
|
259
|
+
current = { ...state, deferred: undefined }
|
|
260
|
+
try {
|
|
261
|
+
return render()
|
|
262
|
+
} finally {
|
|
263
|
+
current = previous
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function routeValue(
|
|
268
|
+
ctx: Pick<RenderContext, "route"> & Partial<Pick<RenderContext, "app" | "payload" | "tree">>,
|
|
269
|
+
): RouteValue {
|
|
270
|
+
const { route } = ctx
|
|
271
|
+
return {
|
|
272
|
+
path: route.path,
|
|
273
|
+
fullPath: route.fullPath,
|
|
274
|
+
params: route.params,
|
|
275
|
+
query: route.query,
|
|
276
|
+
search: route.search,
|
|
277
|
+
hash: route.hash,
|
|
278
|
+
name: route.name,
|
|
279
|
+
meta: route.meta,
|
|
280
|
+
matched: route.matched,
|
|
281
|
+
groups: route.groups ?? [],
|
|
282
|
+
...routeStateOf(ctx),
|
|
283
|
+
} as RouteValue
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Errors a route boundary's own render threw: no route boundary above may catch them. */
|
|
287
|
+
const rethrown = new WeakSet<object>()
|
|
288
|
+
|
|
289
|
+
const lazyStrings = new WeakSet<object>()
|
|
290
|
+
|
|
291
|
+
// A string stand-in for the inner level: it renders the level when something turns it into text, so a layout may
|
|
292
|
+
// destructure `children` and still hand it to an ErrorBoundary, which settles it inside its try.
|
|
293
|
+
function lazyString(render: () => string): string {
|
|
294
|
+
const proxy = new Proxy(new String(""), {
|
|
295
|
+
get(_, key) {
|
|
296
|
+
if (key === Symbol.toPrimitive || key === "toJSON") return () => render()
|
|
297
|
+
const text = render()
|
|
298
|
+
const value = Reflect.get(Object(text), key)
|
|
299
|
+
return typeof value === "function" ? value.bind(text) : value
|
|
300
|
+
},
|
|
301
|
+
})
|
|
302
|
+
lazyStrings.add(proxy)
|
|
303
|
+
return proxy as unknown as string
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Renders a lazy `children` into its string; any other value comes back as given. */
|
|
307
|
+
export function settle(value: string | HtmlChildren): string {
|
|
308
|
+
return lazyStrings.has(value as object) ? String(value) : (value as string)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function renderTree(
|
|
312
|
+
ctx: TreeContext,
|
|
313
|
+
entries?: HeadEntry[],
|
|
314
|
+
completesNavigation = false,
|
|
315
|
+
out?: RenderOut,
|
|
316
|
+
): string {
|
|
317
|
+
const previous = current
|
|
318
|
+
const state = currentOf(ctx)
|
|
319
|
+
if (out?.linkStates) state.linkStates = out.linkStates
|
|
320
|
+
if (out?.scoped) state.scoped = out.scoped
|
|
321
|
+
if (out?.deferred) state.deferred = out.deferred
|
|
322
|
+
state.entries = entries
|
|
323
|
+
if (completesNavigation) state.completesNavigation = true
|
|
324
|
+
current = state
|
|
325
|
+
try {
|
|
326
|
+
const chains: HeadPending[][][] = []
|
|
327
|
+
const once = (render: () => string): (() => string) => {
|
|
328
|
+
let outcome: { value: string } | { error: unknown } | undefined
|
|
329
|
+
return () => {
|
|
330
|
+
if (!outcome) {
|
|
331
|
+
try {
|
|
332
|
+
outcome = { value: render() }
|
|
333
|
+
} catch (error) {
|
|
334
|
+
outcome = { error }
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if ("error" in outcome) throw outcome.error
|
|
338
|
+
return outcome.value
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** An intercepting chain reads the intercepted route with the history entry of the page kept. */
|
|
342
|
+
const intercepted = (tree: RenderTree, render: () => string): string => {
|
|
343
|
+
if (!tree.route) return render()
|
|
344
|
+
const page = state.route
|
|
345
|
+
const { state: entry, key, navigationType } = page
|
|
346
|
+
state.route = routeValue({ ...ctx, route: { ...tree.route, state: entry, key, navigationType }, tree })
|
|
347
|
+
try {
|
|
348
|
+
return render()
|
|
349
|
+
} finally {
|
|
350
|
+
state.route = page
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
const chain = (components: RenderEntry[], marked = false): ((index: number) => string) => {
|
|
354
|
+
const markers = marked ? markedEntries(components) : undefined
|
|
355
|
+
const perComponent: HeadPending[][] = []
|
|
356
|
+
chains.push(perComponent)
|
|
357
|
+
const renderEntry = (
|
|
358
|
+
index: number,
|
|
359
|
+
{ id, component, props, caught, outlets }: RenderEntry,
|
|
360
|
+
leaf = false,
|
|
361
|
+
): string => {
|
|
362
|
+
const input: HtmlProps = { ...props, route: state.route }
|
|
363
|
+
let inner: (() => string) | undefined
|
|
364
|
+
if (!leaf && index + 1 < components.length) {
|
|
365
|
+
inner = once(() => renderLevel(index + 1))
|
|
366
|
+
input.children = lazyString(inner)
|
|
367
|
+
}
|
|
368
|
+
if (ctx.tree.error) input.error = ctx.tree.error
|
|
369
|
+
const slots =
|
|
370
|
+
outlets &&
|
|
371
|
+
Object.fromEntries(
|
|
372
|
+
Object.entries(outlets).map(([name, tree]) => [
|
|
373
|
+
name,
|
|
374
|
+
once(() => intercepted(tree, () => (tree.components.length ? chain(tree.components)(0) : ""))),
|
|
375
|
+
]),
|
|
376
|
+
)
|
|
377
|
+
const outer = {
|
|
378
|
+
id: state.id,
|
|
379
|
+
outlet: state.outlet,
|
|
380
|
+
slots: state.slots,
|
|
381
|
+
pending: state.pending,
|
|
382
|
+
boundaryError: state.boundaryError,
|
|
383
|
+
}
|
|
384
|
+
state.id = id
|
|
385
|
+
state.outlet = inner
|
|
386
|
+
state.slots = slots
|
|
387
|
+
state.pending = perComponent[index] = []
|
|
388
|
+
if (caught) state.boundaryError = props?.error as ErrorPageProps
|
|
389
|
+
try {
|
|
390
|
+
const output = settle((component as HtmlComponent)(input))
|
|
391
|
+
if (typeof output !== "string") throw new TypeError(`"${id}" must return a string`)
|
|
392
|
+
if (index === markers?.page) return wrap(PAGE_MARKERS, output)
|
|
393
|
+
if (index === markers?.layout) return wrap(LAYOUT_MARKERS, output)
|
|
394
|
+
return output
|
|
395
|
+
} catch (error) {
|
|
396
|
+
for (let level = index; level < components.length; level++) perComponent[level] = []
|
|
397
|
+
if (caught && typeof error === "object" && error !== null) rethrown.add(error)
|
|
398
|
+
throw error
|
|
399
|
+
} finally {
|
|
400
|
+
state.id = outer.id
|
|
401
|
+
state.outlet = outer.outlet
|
|
402
|
+
state.slots = outer.slots
|
|
403
|
+
state.pending = outer.pending
|
|
404
|
+
state.boundaryError = outer.boundaryError
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const renderLevel = (index: number): string => {
|
|
408
|
+
const entry = components[index]!
|
|
409
|
+
if (!entry.boundary) return renderEntry(index, entry)
|
|
410
|
+
try {
|
|
411
|
+
return renderEntry(index, entry)
|
|
412
|
+
} catch (error) {
|
|
413
|
+
if (typeof error === "object" && error !== null && rethrown.has(error)) throw error
|
|
414
|
+
const caught = routeBoundary(ctx as RenderContext | ClientContext, entry, error)
|
|
415
|
+
if (!caught) throw error
|
|
416
|
+
const clearError = (options?: ClearErrorOptions) => state.app.error?.clear(options) ?? Promise.resolve()
|
|
417
|
+
return renderEntry(
|
|
418
|
+
index,
|
|
419
|
+
{
|
|
420
|
+
id: caught.id,
|
|
421
|
+
caught: caught.caught,
|
|
422
|
+
component: caught.component,
|
|
423
|
+
props: { error: caught.error, clearError },
|
|
424
|
+
},
|
|
425
|
+
true,
|
|
426
|
+
)
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return renderLevel
|
|
430
|
+
}
|
|
431
|
+
const rendered = ctx.tree.components.length ? chain(ctx.tree.components, true)(0) : ""
|
|
432
|
+
state.pending = undefined
|
|
433
|
+
for (const perComponent of chains) {
|
|
434
|
+
for (const inputs of perComponent) {
|
|
435
|
+
for (const { input, options } of inputs ?? []) {
|
|
436
|
+
const entry = state.head.push(input, options)
|
|
437
|
+
entries?.push(entry)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return rendered
|
|
442
|
+
} finally {
|
|
443
|
+
current = previous
|
|
444
|
+
}
|
|
445
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DefaultErrorPage as default } from "./index.ts"
|