@hamur/adapter-vue 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 +32 -2
- package/dist/chunks/browser-index-43pdsfp6.js +69 -0
- package/dist/chunks/browser-index-d1bxd40m.js +320 -0
- package/dist/chunks/browser-index-nhft0ewv.js +161 -0
- package/dist/src/adapter.js +402 -0
- package/dist/src/index.js +1348 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +20 -0
- package/dist/src/runtime/island.js +45 -0
- package/dist/src/runtime/server.js +238 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +14 -0
- package/dist/types/src/client-component.d.ts +12 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/components/AwaitValue.d.ts +15 -0
- package/dist/types/src/components/ClientFallback.server.d.ts +17 -0
- package/dist/types/src/components/ErrorBoundary.server.d.ts +21 -0
- package/dist/types/src/components/error-code.d.ts +1 -0
- package/dist/types/src/context.d.ts +65 -0
- package/dist/types/src/index.d.ts +329 -0
- package/dist/types/src/island.d.ts +68 -0
- package/dist/types/src/keepalive.d.ts +22 -0
- package/dist/types/src/lazy-hydration.d.ts +10 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +45 -0
- package/dist/types/src/plugin.d.ts +45 -0
- package/dist/types/src/route-boundary.d.ts +35 -0
- package/dist/types/src/route-boundary.server.d.ts +5 -0
- package/dist/types/src/runtime/client.d.ts +11 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +12 -0
- package/dist/types/src/server-island.d.ts +33 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +64 -4
- package/src/adapter.ts +25 -0
- package/src/async-data.ts +77 -0
- package/src/client-component.ts +39 -0
- package/src/client-fallback.ts +2 -0
- package/src/components/Await.vue +30 -0
- package/src/components/AwaitValue.ts +41 -0
- package/src/components/ClientFallback.server.ts +41 -0
- package/src/components/ClientFallback.vue +31 -0
- package/src/components/ClientOnly.vue +16 -0
- package/src/components/DefaultErrorPage.vue +19 -0
- package/src/components/DevOnly.vue +8 -0
- package/src/components/ErrorBoundary.server.ts +69 -0
- package/src/components/ErrorBoundary.vue +32 -0
- package/src/components/Form.vue +85 -0
- package/src/components/Link.vue +200 -0
- package/src/components/Navigate.vue +16 -0
- package/src/components/Outlet.vue +28 -0
- package/src/components/error-code.ts +7 -0
- package/src/context.ts +391 -0
- package/src/index.ts +1528 -0
- package/src/island.ts +53 -0
- package/src/keepalive.ts +92 -0
- package/src/lazy-hydration.ts +53 -0
- package/src/markdown-page.ts +21 -0
- package/src/markdown.ts +80 -0
- package/src/plugin.ts +398 -0
- package/src/route-boundary.server.ts +29 -0
- package/src/route-boundary.ts +48 -0
- package/src/runtime/client.ts +137 -0
- package/src/runtime/hydration-dev.ts +38 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +60 -0
- package/src/runtime/server.ts +145 -0
- package/src/server-island.ts +38 -0
- package/src/svg.ts +28 -0
- package/src/vue.d.ts +5 -0
- package/src/webmcp.d.ts +18 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<script setup lang="ts" generic="T extends string = string">
|
|
2
|
+
import {
|
|
3
|
+
computed,
|
|
4
|
+
inject,
|
|
5
|
+
normalizeClass,
|
|
6
|
+
provide,
|
|
7
|
+
onUnmounted,
|
|
8
|
+
useAttrs,
|
|
9
|
+
useTemplateRef,
|
|
10
|
+
watchEffect,
|
|
11
|
+
type PropType,
|
|
12
|
+
} from "vue"
|
|
13
|
+
import { withoutBase, type RouteLocation } from "hamur/app"
|
|
14
|
+
import {
|
|
15
|
+
LINK_DEFAULTS,
|
|
16
|
+
createLinkSlot,
|
|
17
|
+
linkAttributes,
|
|
18
|
+
linkDefaultsOf,
|
|
19
|
+
linkExtraAttributes,
|
|
20
|
+
linkPending,
|
|
21
|
+
linkPrefetchProps,
|
|
22
|
+
linkState,
|
|
23
|
+
prefetchAttribute,
|
|
24
|
+
viewTransitionAttribute,
|
|
25
|
+
relativeTarget,
|
|
26
|
+
resolveLinkTarget,
|
|
27
|
+
searchTarget,
|
|
28
|
+
setLinkState,
|
|
29
|
+
type CheckedTarget,
|
|
30
|
+
type LinkDefaults,
|
|
31
|
+
type Navigation,
|
|
32
|
+
type PrefetchOn,
|
|
33
|
+
warnRemovedLinkProps,
|
|
34
|
+
} from "hamur/adapter"
|
|
35
|
+
import {
|
|
36
|
+
AppKey,
|
|
37
|
+
BaseURLKey,
|
|
38
|
+
HrefKey,
|
|
39
|
+
LinkHrefKey,
|
|
40
|
+
NavigationKey,
|
|
41
|
+
RouteKey,
|
|
42
|
+
RouterKey,
|
|
43
|
+
detachedHref,
|
|
44
|
+
} from "../context.ts"
|
|
45
|
+
|
|
46
|
+
defineOptions({ inheritAttrs: false })
|
|
47
|
+
|
|
48
|
+
const props = defineProps({
|
|
49
|
+
to: { type: [String, Object] as PropType<CheckedTarget<T> | RouteLocation>, required: true },
|
|
50
|
+
replace: Boolean,
|
|
51
|
+
prefetch: { type: [Boolean, String] as PropType<boolean>, default: undefined },
|
|
52
|
+
prefetchOn: { type: [String, Object] as PropType<PrefetchOn>, default: undefined },
|
|
53
|
+
href: { type: null, default: undefined },
|
|
54
|
+
noPrefetch: { type: null, default: undefined },
|
|
55
|
+
viewTransition: { type: [Boolean, Object] as PropType<boolean | { types: readonly string[] }>, default: undefined },
|
|
56
|
+
query: { type: Function as PropType<(prev: never) => Record<string, unknown>>, default: undefined },
|
|
57
|
+
state: { type: Object as PropType<Record<string, unknown>>, default: undefined },
|
|
58
|
+
relative: { type: String as PropType<"route">, default: undefined },
|
|
59
|
+
mask: { type: String, default: undefined },
|
|
60
|
+
unmaskOnReload: { type: Boolean, default: undefined },
|
|
61
|
+
activeClass: { type: String, default: undefined },
|
|
62
|
+
exactActiveClass: { type: String, default: undefined },
|
|
63
|
+
pendingClass: { type: String, default: undefined },
|
|
64
|
+
external: Boolean,
|
|
65
|
+
noRel: Boolean,
|
|
66
|
+
prefetchedClass: { type: String, default: undefined },
|
|
67
|
+
scroll: { type: Boolean, default: undefined },
|
|
68
|
+
prefetchDelay: { type: Number, default: undefined },
|
|
69
|
+
onBeforeNavigate: { type: Function as PropType<(nav: Navigation) => void>, default: undefined },
|
|
70
|
+
custom: Boolean,
|
|
71
|
+
defaults: { type: Object as PropType<LinkDefaults>, default: undefined },
|
|
72
|
+
ariaCurrentValue: {
|
|
73
|
+
type: String as PropType<"page" | "step" | "location" | "date" | "time" | "true">,
|
|
74
|
+
default: "page",
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
warnRemovedLinkProps(props)
|
|
78
|
+
const baseURL = inject(BaseURLKey, "/")
|
|
79
|
+
const app = inject(AppKey, null)
|
|
80
|
+
const resolvedDefaults = computed(() => linkDefaultsOf(app, props.defaults))
|
|
81
|
+
const linkClasses = computed(() => ({
|
|
82
|
+
active: props.activeClass ?? resolvedDefaults.value.activeClass,
|
|
83
|
+
exact: props.exactActiveClass ?? resolvedDefaults.value.exactActiveClass,
|
|
84
|
+
pending: props.pendingClass ?? resolvedDefaults.value.pendingClass,
|
|
85
|
+
prefetched: props.prefetchedClass ?? resolvedDefaults.value.prefetchedClass,
|
|
86
|
+
}))
|
|
87
|
+
const resolve = inject(HrefKey, detachedHref)
|
|
88
|
+
const route = inject(RouteKey, null)
|
|
89
|
+
const given = computed(() => props.to)
|
|
90
|
+
const attrs = useAttrs()
|
|
91
|
+
const linked = computed(() =>
|
|
92
|
+
resolveLinkTarget(
|
|
93
|
+
app,
|
|
94
|
+
route ? searchTarget(relativeTarget(given.value, route, props.relative), route, props.query) : given.value,
|
|
95
|
+
{ ...props, ...attrs },
|
|
96
|
+
resolve,
|
|
97
|
+
props.external,
|
|
98
|
+
),
|
|
99
|
+
)
|
|
100
|
+
const target = computed(() => linked.value.to)
|
|
101
|
+
const resolved = computed(() => resolve(target.value))
|
|
102
|
+
provide(LinkHrefKey, resolved)
|
|
103
|
+
const matched = computed(() =>
|
|
104
|
+
route
|
|
105
|
+
? linkState(typeof target.value === "string" ? target.value : withoutBase(resolved.value, baseURL), route)
|
|
106
|
+
: { active: false, exact: false },
|
|
107
|
+
)
|
|
108
|
+
const element = useTemplateRef<HTMLAnchorElement>("anchor")
|
|
109
|
+
let kept: HTMLAnchorElement | null = null
|
|
110
|
+
watchEffect(() => {
|
|
111
|
+
if (kept && kept !== element.value) setLinkState(kept, undefined)
|
|
112
|
+
kept = element.value
|
|
113
|
+
const before = props.onBeforeNavigate
|
|
114
|
+
if (kept) setLinkState(kept, props.state, before ? (nav) => props.onBeforeNavigate?.(nav) : undefined)
|
|
115
|
+
})
|
|
116
|
+
onUnmounted(() => kept && setLinkState(kept, undefined))
|
|
117
|
+
|
|
118
|
+
function marked() {
|
|
119
|
+
const { active, exact } = matched.value
|
|
120
|
+
const left = linked.value.props as Record<string, unknown>
|
|
121
|
+
const bound: Record<string, unknown> = Object.fromEntries(Object.entries(attrs).filter(([name]) => name in left))
|
|
122
|
+
const classes = normalizeClass([attrs.class, active && linkClasses.value.active, exact && linkClasses.value.exact])
|
|
123
|
+
if (classes) bound.class = classes
|
|
124
|
+
if (exact && bound["aria-current"] === undefined) bound["aria-current"] = props.ariaCurrentValue
|
|
125
|
+
return Object.assign(
|
|
126
|
+
bound,
|
|
127
|
+
linkExtraAttributes({
|
|
128
|
+
href: resolved.value,
|
|
129
|
+
external: props.external,
|
|
130
|
+
noRel: props.noRel,
|
|
131
|
+
rel: attrs.rel,
|
|
132
|
+
target: attrs.target,
|
|
133
|
+
prefetchedClass: linkClasses.value.prefetched,
|
|
134
|
+
scroll: props.scroll,
|
|
135
|
+
prefetchDelay: props.prefetchDelay ?? resolvedDefaults.value.prefetchDelay,
|
|
136
|
+
externalRelAttribute: resolvedDefaults.value.externalRelAttribute,
|
|
137
|
+
}),
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const router = inject(RouterKey, null)
|
|
142
|
+
const navigation = inject(NavigationKey, null)
|
|
143
|
+
const linkSlot = computed(() =>
|
|
144
|
+
createLinkSlot({
|
|
145
|
+
href: resolved.value,
|
|
146
|
+
to: target.value,
|
|
147
|
+
attrs: linkAttributes({
|
|
148
|
+
href: resolved.value,
|
|
149
|
+
external: props.external,
|
|
150
|
+
...linkPrefetchProps(props, resolvedDefaults.value),
|
|
151
|
+
replace: props.replace,
|
|
152
|
+
viewTransition: props.viewTransition,
|
|
153
|
+
pendingClass: linkClasses.value.pending,
|
|
154
|
+
trim: !!props.query,
|
|
155
|
+
mask: props.mask === undefined ? undefined : resolve(props.mask),
|
|
156
|
+
unmaskOnReload: props.unmaskOnReload,
|
|
157
|
+
ariaCurrent: matched.value.exact ? props.ariaCurrentValue : undefined,
|
|
158
|
+
rel: attrs.rel,
|
|
159
|
+
target: attrs.target,
|
|
160
|
+
noRel: props.noRel,
|
|
161
|
+
prefetchedClass: linkClasses.value.prefetched,
|
|
162
|
+
scroll: props.scroll,
|
|
163
|
+
prefetchDelay: props.prefetchDelay ?? resolvedDefaults.value.prefetchDelay,
|
|
164
|
+
externalRelAttribute: resolvedDefaults.value.externalRelAttribute,
|
|
165
|
+
}),
|
|
166
|
+
active: matched.value.active,
|
|
167
|
+
exact: matched.value.exact,
|
|
168
|
+
pending: navigation !== null && linkPending(resolved.value, navigation.value, baseURL),
|
|
169
|
+
external: props.external,
|
|
170
|
+
options: {
|
|
171
|
+
replace: props.replace,
|
|
172
|
+
state: props.state,
|
|
173
|
+
mask: props.mask,
|
|
174
|
+
unmaskOnReload: props.unmaskOnReload,
|
|
175
|
+
viewTransition: props.viewTransition,
|
|
176
|
+
scroll: props.scroll === false ? false : undefined,
|
|
177
|
+
},
|
|
178
|
+
router,
|
|
179
|
+
}),
|
|
180
|
+
)
|
|
181
|
+
</script>
|
|
182
|
+
|
|
183
|
+
<template>
|
|
184
|
+
<slot v-if="custom" v-bind="linkSlot" />
|
|
185
|
+
<a
|
|
186
|
+
v-else
|
|
187
|
+
ref="anchor"
|
|
188
|
+
v-bind="marked()"
|
|
189
|
+
:href="resolved"
|
|
190
|
+
:data-hamur-prefetch="external ? 'false' : prefetchAttribute(linkPrefetchProps(props, resolvedDefaults))"
|
|
191
|
+
:data-hamur-replace="replace ? '' : undefined"
|
|
192
|
+
:data-hamur-view-transition="viewTransitionAttribute(viewTransition)"
|
|
193
|
+
:data-hamur-pending-class="linkClasses.pending === LINK_DEFAULTS.pendingClass ? undefined : linkClasses.pending"
|
|
194
|
+
:data-hamur-trim="query ? '' : undefined"
|
|
195
|
+
:data-hamur-mask="mask === undefined ? undefined : resolve(mask)"
|
|
196
|
+
:data-hamur-unmask="mask !== undefined && unmaskOnReload === false ? 'false' : undefined"
|
|
197
|
+
>
|
|
198
|
+
<slot />
|
|
199
|
+
</a>
|
|
200
|
+
</template>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script setup lang="ts" generic="T extends string = string">
|
|
2
|
+
import { onMounted, type PropType } from "vue"
|
|
3
|
+
import { navigateTo, redirect, type RouteLocation } from "hamur/app"
|
|
4
|
+
import type { CheckedTarget } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
const props = defineProps({
|
|
7
|
+
to: { type: [String, Object] as PropType<CheckedTarget<T, false> | RouteLocation>, required: true },
|
|
8
|
+
replace: Boolean,
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
const target = props.to as string | RouteLocation
|
|
12
|
+
if (typeof window === "undefined") throw redirect<string>(target)
|
|
13
|
+
onMounted(() => void navigateTo<string>(target, { replace: props.replace }))
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<template></template>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { inject, provide } from "vue"
|
|
3
|
+
import { noteOutlet } from "hamur/adapter"
|
|
4
|
+
import { EntryKey, OutletKey, OutletValueKey, SlotsKey } from "../context.ts"
|
|
5
|
+
|
|
6
|
+
const props = defineProps<{ context?: unknown; name?: string }>()
|
|
7
|
+
const outlet = inject(OutletKey, null)
|
|
8
|
+
const slots = inject(SlotsKey, null)
|
|
9
|
+
const owner = inject(EntryKey, null)
|
|
10
|
+
const above = inject(OutletValueKey, null)
|
|
11
|
+
provide(OutletValueKey, () => (props.context !== undefined ? props.context : above?.()))
|
|
12
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
13
|
+
const warned = new Set<string>()
|
|
14
|
+
if (isDev && props.name === undefined) noteOutlet(owner?.())
|
|
15
|
+
const Child = () => {
|
|
16
|
+
if (props.name === undefined) return outlet?.value?.() ?? null
|
|
17
|
+
const all = slots?.()
|
|
18
|
+
if (isDev && !(all && props.name in all) && !warned.has(props.name)) {
|
|
19
|
+
warned.add(props.name)
|
|
20
|
+
console.warn(`Outlet name "${props.name}" has no slot in ${owner?.()}`)
|
|
21
|
+
}
|
|
22
|
+
return all?.[props.name]?.() ?? null
|
|
23
|
+
}
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<template>
|
|
27
|
+
<Child />
|
|
28
|
+
</template>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { diagnosticURL, type DiagnosticCode } from "hamur/adapter"
|
|
2
|
+
|
|
3
|
+
// A script setup's imports are returned to its template, so the import stays out of the SFC for a production build to drop it.
|
|
4
|
+
export const codeURL =
|
|
5
|
+
(import.meta as { dev?: boolean }).dev === true
|
|
6
|
+
? (code: string): string => diagnosticURL(code as DiagnosticCode)
|
|
7
|
+
: undefined
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import {
|
|
2
|
+
KeepAlive,
|
|
3
|
+
Suspense,
|
|
4
|
+
computed,
|
|
5
|
+
defineComponent,
|
|
6
|
+
h,
|
|
7
|
+
nextTick,
|
|
8
|
+
onBeforeUpdate,
|
|
9
|
+
onUnmounted,
|
|
10
|
+
provide,
|
|
11
|
+
shallowReactive,
|
|
12
|
+
shallowRef,
|
|
13
|
+
watchEffect,
|
|
14
|
+
} from "vue"
|
|
15
|
+
import type {
|
|
16
|
+
App,
|
|
17
|
+
Component,
|
|
18
|
+
ComputedRef,
|
|
19
|
+
FunctionalComponent,
|
|
20
|
+
InjectionKey,
|
|
21
|
+
PropType,
|
|
22
|
+
ShallowRef,
|
|
23
|
+
Slot,
|
|
24
|
+
VNode,
|
|
25
|
+
} from "vue"
|
|
26
|
+
import type { AppContext, ClearErrorOptions, ErrorPageProps, ErrorStore, Head, State } from "hamur/app"
|
|
27
|
+
import { APP_ROOT_ID, createRouterApi, layoutOf, routeBoundary, type RouterSource } from "hamur/adapter"
|
|
28
|
+
import type { RenderEntry } from "hamur/adapter"
|
|
29
|
+
import type { KeptOutlet } from "./keepalive.ts"
|
|
30
|
+
import { Guard, RouteBoundary } from "./route-boundary.ts"
|
|
31
|
+
import { routeStateOf, setServerHTML, SLOT_ELEMENT } from "hamur/adapter"
|
|
32
|
+
import type {
|
|
33
|
+
ClientContext,
|
|
34
|
+
FormStatusStore,
|
|
35
|
+
NavigationState,
|
|
36
|
+
RenderContext,
|
|
37
|
+
RenderTree,
|
|
38
|
+
ResolvedRoute,
|
|
39
|
+
RouteValue,
|
|
40
|
+
RouterApi,
|
|
41
|
+
} from "hamur/adapter"
|
|
42
|
+
import { withBase } from "hamur/app"
|
|
43
|
+
import type { RouteLocation } from "hamur/app"
|
|
44
|
+
|
|
45
|
+
export const RouteKey: InjectionKey<RouteValue> = Symbol("hamur-route")
|
|
46
|
+
export const RouterKey: InjectionKey<RouterApi> = Symbol("hamur-router")
|
|
47
|
+
/** Provided as `true` below a `"never"` lazy component, so its async data stays out of the payload. */
|
|
48
|
+
export const NeverHydratedKey: InjectionKey<boolean> = Symbol("hamur-never-hydrated")
|
|
49
|
+
export const BaseURLKey: InjectionKey<string> = Symbol("hamur-base-url")
|
|
50
|
+
export const HrefKey: InjectionKey<RenderContext["href"]> = Symbol("hamur-href")
|
|
51
|
+
export const DataKey: InjectionKey<ShallowRef<Record<string, unknown>>> = Symbol("hamur-data")
|
|
52
|
+
export const EntryKey: InjectionKey<() => string> = Symbol("hamur-entry")
|
|
53
|
+
export interface ErrorSource {
|
|
54
|
+
store?: ErrorStore
|
|
55
|
+
initial?: ErrorPageProps
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const ErrorKey: InjectionKey<ErrorSource> = Symbol("hamur-error")
|
|
59
|
+
export const HeadKey: InjectionKey<Head> = Symbol("hamur-head")
|
|
60
|
+
export const HeadPatchesKey: InjectionKey<Array<() => void>> = Symbol("hamur-head-patches")
|
|
61
|
+
export const StateKey: InjectionKey<State> = Symbol("hamur-state")
|
|
62
|
+
interface NavigationSource {
|
|
63
|
+
get(): NavigationState
|
|
64
|
+
subscribe(listener: () => void): () => void
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface Submitted {
|
|
68
|
+
/** The `{ id, instance?, value | failure | error }` of a no-JS post to a page. */
|
|
69
|
+
handlerState?: unknown
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const SubmittedKey: InjectionKey<ShallowRef<Submitted>> = Symbol("hamur-submitted")
|
|
73
|
+
export const FormStatusKey: InjectionKey<FormStatusStore | null> = Symbol("hamur-form-status")
|
|
74
|
+
export const AppKey: InjectionKey<AppContext> = Symbol("hamur-app")
|
|
75
|
+
/** The `href` of the `Link` whose slot renders, read by `useLinkStatus()`. */
|
|
76
|
+
export const LinkHrefKey: InjectionKey<ComputedRef<string>> = Symbol("hamur-link-href")
|
|
77
|
+
export const NavigationKey: InjectionKey<ShallowRef<NavigationState>> = Symbol("hamur-navigation")
|
|
78
|
+
export const OutletValueKey: InjectionKey<() => unknown> = Symbol("hamur-outlet-value")
|
|
79
|
+
export const OutletKey: InjectionKey<ShallowRef<Slot | undefined>> = Symbol("hamur-outlet")
|
|
80
|
+
export const SlotsKey: InjectionKey<() => Record<string, () => VNode | undefined> | null> = Symbol("hamur-slots")
|
|
81
|
+
export const LayoutKey: InjectionKey<ComputedRef<ReturnType<typeof layoutOf>>> = Symbol("hamur-layout")
|
|
82
|
+
|
|
83
|
+
export function detachedHref(to: string | RouteLocation): string {
|
|
84
|
+
if (typeof to !== "string") throw new Error(`Link to the route "${to.name}" must be rendered inside a Hamur app`)
|
|
85
|
+
return withBase(to, "/")
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function routerOf(
|
|
89
|
+
navigator: unknown,
|
|
90
|
+
baseURL = "/",
|
|
91
|
+
page?: Omit<RouterSource, "navigator" | "baseURL">,
|
|
92
|
+
): RouterApi {
|
|
93
|
+
return createRouterApi({ ...page, navigator, baseURL })
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function routeValue(
|
|
97
|
+
ctx: Pick<RenderContext, "route"> & Partial<Pick<RenderContext, "app" | "payload" | "tree">>,
|
|
98
|
+
): RouteValue {
|
|
99
|
+
const { route } = ctx
|
|
100
|
+
return {
|
|
101
|
+
path: route.path,
|
|
102
|
+
fullPath: route.fullPath,
|
|
103
|
+
params: route.params,
|
|
104
|
+
query: route.query,
|
|
105
|
+
search: route.search,
|
|
106
|
+
hash: route.hash,
|
|
107
|
+
name: route.name,
|
|
108
|
+
meta: route.meta,
|
|
109
|
+
matched: route.matched,
|
|
110
|
+
groups: route.groups ?? [],
|
|
111
|
+
...routeStateOf(ctx),
|
|
112
|
+
} as RouteValue
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const Entry = defineComponent({
|
|
116
|
+
name: "HamurEntry",
|
|
117
|
+
props: {
|
|
118
|
+
component: { type: null, required: true },
|
|
119
|
+
entryProps: { type: null, default: null },
|
|
120
|
+
dataKey: { type: String, required: true },
|
|
121
|
+
outlets: { type: Object as PropType<Record<string, () => VNode | undefined> | null>, default: null },
|
|
122
|
+
caught: { type: Boolean, default: false },
|
|
123
|
+
route: { type: Object as PropType<RouteValue | null>, default: null },
|
|
124
|
+
},
|
|
125
|
+
setup(props, { slots }) {
|
|
126
|
+
provide(EntryKey, () => props.dataKey)
|
|
127
|
+
provide(SlotsKey, () => props.outlets)
|
|
128
|
+
if (props.route) {
|
|
129
|
+
const own = shallowReactive({ ...props.route })
|
|
130
|
+
provide(RouteKey, own)
|
|
131
|
+
watchEffect(() => void Object.assign(own, props.route ?? {}))
|
|
132
|
+
}
|
|
133
|
+
if (props.caught) provide(ErrorKey, { initial: (props.entryProps as { error: ErrorPageProps }).error })
|
|
134
|
+
const outlet = shallowRef(slots.default)
|
|
135
|
+
onBeforeUpdate(() => void (outlet.value = slots.default))
|
|
136
|
+
provide(OutletKey, outlet)
|
|
137
|
+
return () => h(props.component as never, props.entryProps, slots as never)
|
|
138
|
+
},
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
interface Painting {
|
|
142
|
+
pending(boundary: object): void
|
|
143
|
+
done(boundary: object): void
|
|
144
|
+
settled(): Promise<void>
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function createPainting(): Painting {
|
|
148
|
+
const pending = new Set<object>()
|
|
149
|
+
const waiting: Array<() => void> = []
|
|
150
|
+
return {
|
|
151
|
+
pending: (boundary) => void pending.add(boundary),
|
|
152
|
+
done(boundary) {
|
|
153
|
+
pending.delete(boundary)
|
|
154
|
+
if (pending.size === 0) for (const resolve of waiting.splice(0)) resolve()
|
|
155
|
+
},
|
|
156
|
+
settled: () => (pending.size === 0 ? Promise.resolve() : new Promise((resolve) => void waiting.push(resolve))),
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** One `Suspense` per page level: an async `setup` renders once it resolves, and a new page replaces the slot only then. */
|
|
161
|
+
const PageLevel = defineComponent({
|
|
162
|
+
name: "HamurPage",
|
|
163
|
+
props: {
|
|
164
|
+
painting: { type: Object as PropType<Painting>, required: true },
|
|
165
|
+
entry: { type: Object as PropType<VNode>, required: true },
|
|
166
|
+
},
|
|
167
|
+
setup(props) {
|
|
168
|
+
const boundary = {}
|
|
169
|
+
onUnmounted(() => props.painting.done(boundary))
|
|
170
|
+
return () =>
|
|
171
|
+
h(
|
|
172
|
+
Suspense,
|
|
173
|
+
{ onPending: () => props.painting.pending(boundary), onResolve: () => props.painting.done(boundary) },
|
|
174
|
+
{ default: () => props.entry },
|
|
175
|
+
)
|
|
176
|
+
},
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
function boundaryView(ctx: Source, painting: Painting, entry: RenderEntry, error: unknown): VNode {
|
|
180
|
+
const caught = routeBoundary(ctx as RenderContext | ClientContext, entry, error)
|
|
181
|
+
if (!caught) throw error
|
|
182
|
+
const clearError = (options?: ClearErrorOptions) => ctx.app.error?.clear(options) ?? Promise.resolve()
|
|
183
|
+
const { id } = caught
|
|
184
|
+
const view = h(PageLevel, {
|
|
185
|
+
painting,
|
|
186
|
+
entry: h(Entry, {
|
|
187
|
+
key: id,
|
|
188
|
+
component: caught.component as never,
|
|
189
|
+
entryProps: { error: caught.error, clearError },
|
|
190
|
+
dataKey: id,
|
|
191
|
+
caught: true,
|
|
192
|
+
}),
|
|
193
|
+
})
|
|
194
|
+
return h(Guard, null, { default: () => view })
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** An intercepting chain reads the intercepted route with the history entry of the page kept. */
|
|
198
|
+
function interceptedRoute(slot: RenderTree, ctx: Source): RouteValue {
|
|
199
|
+
const { state, key, navigationType } = ctx.route
|
|
200
|
+
return routeValue({ ...ctx, route: { ...slot.route!, state, key, navigationType }, tree: slot })
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let keptOutlet: KeptOutlet | undefined
|
|
204
|
+
|
|
205
|
+
/** The core's keep-alive feature loads the outlet (`Adapter.keepAliveOutlet`) and hands it to `installKeepAlive` before the first tree that holds kept units; it takes what it needs from the object it is built with, so its chunk imports nothing from the base. */
|
|
206
|
+
export function installKeepAlive(module: typeof import("./keepalive.ts")): void {
|
|
207
|
+
keptOutlet ??= module.createKeptOutlet({
|
|
208
|
+
KeepAlive,
|
|
209
|
+
defineComponent,
|
|
210
|
+
h,
|
|
211
|
+
provide,
|
|
212
|
+
shallowReactive,
|
|
213
|
+
shallowRef,
|
|
214
|
+
watchEffect,
|
|
215
|
+
RouteKey,
|
|
216
|
+
DataKey,
|
|
217
|
+
routeValue,
|
|
218
|
+
dataOf,
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function keepAlive(): KeptOutlet {
|
|
223
|
+
if (!keptOutlet)
|
|
224
|
+
throw new Error("A tree with kept pages was rendered before the keep-alive part of the Vue adapter was installed")
|
|
225
|
+
return keptOutlet
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function nest(tree: RenderTree, painting: Painting, source: () => Source): VNode | undefined {
|
|
229
|
+
return level(tree, 0, painting, source)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Built anew on every call: Vue mounts a copy of a mounted vnode by hydrating the element it held, so a remounted `Outlet` needs vnodes of its own. */
|
|
233
|
+
function level(tree: RenderTree, index: number, painting: Painting, source: () => Source): VNode | undefined {
|
|
234
|
+
const item = tree.components[index]
|
|
235
|
+
if (!item) return undefined
|
|
236
|
+
const { id, component, props, caught } = item
|
|
237
|
+
const below = tree.components[index + 1]
|
|
238
|
+
const inner =
|
|
239
|
+
item.kept !== undefined
|
|
240
|
+
? () => keepAlive()(item, below, level(tree, index + 1, painting, source), source)
|
|
241
|
+
: below
|
|
242
|
+
? () => level(tree, index + 1, painting, source)
|
|
243
|
+
: undefined
|
|
244
|
+
const outlets = item.outlets
|
|
245
|
+
? Object.fromEntries(Object.entries(item.outlets).map(([name, slot]) => [name, () => nest(slot, painting, source)]))
|
|
246
|
+
: null
|
|
247
|
+
const entry = h(
|
|
248
|
+
Entry,
|
|
249
|
+
{
|
|
250
|
+
key: item.key === undefined ? id : `${id}\0${item.key}`,
|
|
251
|
+
component: component as never,
|
|
252
|
+
entryProps: props ?? null,
|
|
253
|
+
dataKey: id,
|
|
254
|
+
outlets,
|
|
255
|
+
caught: caught !== undefined,
|
|
256
|
+
route: index === 0 && tree.route ? interceptedRoute(tree, source()) : null,
|
|
257
|
+
},
|
|
258
|
+
(inner ? { default: inner } : undefined) as never,
|
|
259
|
+
)
|
|
260
|
+
let child = id === APP_ROOT_ID || id.startsWith("layout:") ? entry : h(PageLevel, { painting, entry })
|
|
261
|
+
if (caught) {
|
|
262
|
+
const view = child
|
|
263
|
+
child = h(Guard, null, { default: () => view })
|
|
264
|
+
}
|
|
265
|
+
if (item.boundary) {
|
|
266
|
+
const view = child
|
|
267
|
+
child = h(
|
|
268
|
+
RouteBoundary,
|
|
269
|
+
{ key: id, resetOn: item, fallback: (error: unknown) => boundaryView(source(), painting, item, error) },
|
|
270
|
+
{ default: () => view },
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
return child
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export type Source = Pick<RenderContext | ClientContext, "route" | "tree"> &
|
|
277
|
+
Pick<RenderContext, "app" | "head"> &
|
|
278
|
+
Partial<Pick<RenderContext, "payload" | "baseURL">> &
|
|
279
|
+
Pick<RenderContext, "href"> & { navigator?: unknown }
|
|
280
|
+
|
|
281
|
+
export const dataOf = (ctx: Source) => (ctx.payload?.data as Record<string, unknown> | undefined) ?? {}
|
|
282
|
+
const submittedOf = (ctx: Source): Submitted => ({ handlerState: ctx.payload?.handlerState })
|
|
283
|
+
|
|
284
|
+
function navigationOf(navigator: unknown): ShallowRef<NavigationState> {
|
|
285
|
+
const source = navigator as Partial<NavigationSource> | undefined
|
|
286
|
+
if (typeof source?.get !== "function" || typeof source.subscribe !== "function")
|
|
287
|
+
return shallowRef<NavigationState>({ state: "idle", pending: false })
|
|
288
|
+
const navigation = shallowRef(source.get())
|
|
289
|
+
source.subscribe(() => (navigation.value = (source as NavigationSource).get()))
|
|
290
|
+
return navigation
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export interface HamurRoot {
|
|
294
|
+
component: { setup(): () => VNode | undefined }
|
|
295
|
+
install(app: App): void
|
|
296
|
+
/** Resolves after the patch, once every page `Suspense` that went pending in it resolved. */
|
|
297
|
+
update(ctx: Source): Promise<void>
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function createRoot(ctx: Source): HamurRoot {
|
|
301
|
+
const route = shallowReactive(routeValue(ctx))
|
|
302
|
+
const router = routerOf(ctx.navigator, ctx.baseURL, ctx)
|
|
303
|
+
const tree: ShallowRef<RenderTree> = shallowRef(ctx.tree)
|
|
304
|
+
const data = shallowRef(dataOf(ctx))
|
|
305
|
+
const submitted = shallowRef(submittedOf(ctx))
|
|
306
|
+
const navigation = navigationOf(ctx.navigator)
|
|
307
|
+
const error: ErrorSource = {
|
|
308
|
+
store: ctx.app.error,
|
|
309
|
+
initial: routeStateOf({ payload: ctx.payload, tree: ctx.tree }).error,
|
|
310
|
+
}
|
|
311
|
+
const painting = createPainting()
|
|
312
|
+
let current = ctx
|
|
313
|
+
return {
|
|
314
|
+
component: {
|
|
315
|
+
setup: () => () => {
|
|
316
|
+
const child = nest(tree.value, painting, () => current)
|
|
317
|
+
return tree.value.kept === undefined
|
|
318
|
+
? child
|
|
319
|
+
: keepAlive()(tree.value, tree.value.components[0], child, () => current)
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
install(app) {
|
|
323
|
+
app.provide(RouteKey, route)
|
|
324
|
+
app.provide(RouterKey, router)
|
|
325
|
+
app.provide(BaseURLKey, ctx.baseURL ?? "/")
|
|
326
|
+
app.provide(HrefKey, ctx.href)
|
|
327
|
+
app.provide(DataKey, data)
|
|
328
|
+
app.provide(SubmittedKey, submitted)
|
|
329
|
+
app.provide(ErrorKey, error)
|
|
330
|
+
app.provide(NavigationKey, navigation)
|
|
331
|
+
app.provide(
|
|
332
|
+
LayoutKey,
|
|
333
|
+
computed(() => layoutOf(tree.value)),
|
|
334
|
+
)
|
|
335
|
+
const appContext = ctx.app
|
|
336
|
+
if (appContext) app.provide(AppKey, appContext)
|
|
337
|
+
if (ctx.head) app.provide(HeadKey, ctx.head)
|
|
338
|
+
const state = ctx.app.state
|
|
339
|
+
if (state) app.provide(StateKey, state)
|
|
340
|
+
},
|
|
341
|
+
async update(next) {
|
|
342
|
+
Object.assign(route, routeValue(next))
|
|
343
|
+
data.value = dataOf(next)
|
|
344
|
+
submitted.value = submittedOf(next)
|
|
345
|
+
current = next
|
|
346
|
+
tree.value = next.tree
|
|
347
|
+
await nextTick()
|
|
348
|
+
await painting.settled()
|
|
349
|
+
},
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const ISLAND_ENTRY = "island"
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The island with its children as its default slot: the server writes their HTML; in the browser the slot element is
|
|
357
|
+
* bare, which Vue hydrates keeping the nodes the server wrote, and the same HTML is written only into one Vue creates anew.
|
|
358
|
+
*/
|
|
359
|
+
function slotted(component: unknown, uid: string, html: string): FunctionalComponent {
|
|
360
|
+
const fill = (node: unknown) => {
|
|
361
|
+
if (node instanceof Element && html && !node.hasChildNodes()) setServerHTML(node, html)
|
|
362
|
+
}
|
|
363
|
+
const slot = () =>
|
|
364
|
+
typeof document === "undefined" ? h(SLOT_ELEMENT, { uid, innerHTML: html }) : h(SLOT_ELEMENT, { uid, ref: fill })
|
|
365
|
+
return (props) => h(component as Component, props, { default: slot })
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** The root of an island: the app's route, state and head around one entry; a `Link` takes a path, as no route table ships. */
|
|
369
|
+
export function createIslandRoot(
|
|
370
|
+
component: unknown,
|
|
371
|
+
props: Record<string, unknown>,
|
|
372
|
+
ctx: { app: AppContext; route: ResolvedRoute; baseURL: string; idPrefix: string; slots?: { default?: string } },
|
|
373
|
+
): HamurRoot & { setProps(props: Record<string, unknown>): Promise<void> } {
|
|
374
|
+
const href = (to: string | RouteLocation) => {
|
|
375
|
+
if (typeof to !== "string")
|
|
376
|
+
throw new Error(`Link to the route "${to.name}" cannot render in an island; give it a path`)
|
|
377
|
+
return withBase(to, ctx.baseURL)
|
|
378
|
+
}
|
|
379
|
+
const slot = ctx.slots?.default
|
|
380
|
+
const entry = slot === undefined ? component : slotted(component, `s${ctx.idPrefix}`, slot)
|
|
381
|
+
const source = (own: Record<string, unknown>) => ({
|
|
382
|
+
app: ctx.app,
|
|
383
|
+
head: ctx.app.head,
|
|
384
|
+
route: ctx.route,
|
|
385
|
+
baseURL: ctx.baseURL,
|
|
386
|
+
href,
|
|
387
|
+
tree: { components: [{ id: ISLAND_ENTRY, component: entry, props: own }] },
|
|
388
|
+
})
|
|
389
|
+
const root = createRoot(source(props))
|
|
390
|
+
return { ...root, setProps: (own) => root.update(source(own)) }
|
|
391
|
+
}
|