@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,33 @@
|
|
|
1
|
+
import type { PropType } from "vue";
|
|
2
|
+
export interface ServerIslandProps {
|
|
3
|
+
/** The name of a component under `app/server-islands/`. */
|
|
4
|
+
name: string;
|
|
5
|
+
props?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A component rendered on the server in its own request; the page holds the `fallback` slot, the core swaps the answer in,
|
|
9
|
+
* and Vue never renders or hydrates it.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ServerIsland: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
12
|
+
name: {
|
|
13
|
+
type: StringConstructor;
|
|
14
|
+
required: true;
|
|
15
|
+
};
|
|
16
|
+
props: {
|
|
17
|
+
type: PropType<ServerIslandProps["props"]>;
|
|
18
|
+
default: undefined;
|
|
19
|
+
};
|
|
20
|
+
}>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
}>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
23
|
+
name: {
|
|
24
|
+
type: StringConstructor;
|
|
25
|
+
required: true;
|
|
26
|
+
};
|
|
27
|
+
props: {
|
|
28
|
+
type: PropType<ServerIslandProps["props"]>;
|
|
29
|
+
default: undefined;
|
|
30
|
+
};
|
|
31
|
+
}>> & Readonly<{}>, {
|
|
32
|
+
props: Record<string, unknown> | undefined;
|
|
33
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/adapter-vue",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/productdevbook/hamur.git",
|
|
9
|
+
"directory": "packages/adapter-vue"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"hamur-source": "./src/index.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./adapter": {
|
|
20
|
+
"types": "./dist/types/src/adapter.d.ts",
|
|
21
|
+
"bun": "./src/adapter.ts",
|
|
22
|
+
"hamur-source": "./src/adapter.ts",
|
|
23
|
+
"node": "./dist/src/adapter.js",
|
|
24
|
+
"default": "./src/adapter.ts"
|
|
25
|
+
},
|
|
26
|
+
"./error-page": "./src/components/DefaultErrorPage.vue",
|
|
27
|
+
"./runtime/server": {
|
|
28
|
+
"types": "./dist/types/src/runtime/server.d.ts",
|
|
29
|
+
"bun": "./src/runtime/server.ts",
|
|
30
|
+
"hamur-source": "./src/runtime/server.ts",
|
|
31
|
+
"node": "./dist/src/runtime/server.js",
|
|
32
|
+
"default": "./src/runtime/server.ts"
|
|
33
|
+
},
|
|
34
|
+
"./runtime/client": {
|
|
35
|
+
"types": "./dist/types/src/runtime/client.d.ts",
|
|
36
|
+
"bun": "./src/runtime/client.ts",
|
|
37
|
+
"hamur-source": "./src/runtime/client.ts",
|
|
38
|
+
"node": "./dist/src/runtime/client.js",
|
|
39
|
+
"default": "./src/runtime/client.ts"
|
|
40
|
+
},
|
|
41
|
+
"./runtime/keepalive": {
|
|
42
|
+
"types": "./dist/types/src/keepalive.d.ts",
|
|
43
|
+
"bun": "./src/keepalive.ts",
|
|
44
|
+
"hamur-source": "./src/keepalive.ts",
|
|
45
|
+
"node": "./dist/src/keepalive.js",
|
|
46
|
+
"default": "./src/keepalive.ts"
|
|
47
|
+
},
|
|
48
|
+
"./runtime/island": {
|
|
49
|
+
"types": "./dist/types/src/runtime/island.d.ts",
|
|
50
|
+
"bun": "./src/runtime/island.ts",
|
|
51
|
+
"hamur-source": "./src/runtime/island.ts",
|
|
52
|
+
"node": "./dist/src/runtime/island.js",
|
|
53
|
+
"default": "./src/runtime/island.ts"
|
|
54
|
+
},
|
|
55
|
+
"./package.json": "./package.json"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"dist",
|
|
59
|
+
"src"
|
|
60
|
+
],
|
|
61
|
+
"dependencies": {},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"vue": "^3.5.44",
|
|
64
|
+
"hamur": "^0.0.2"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ADAPTER_CONTRACT, defineAdapter } from "hamur/adapter"
|
|
2
|
+
import { clientComponentModule, serverComponentModule } from "hamur/adapter/build"
|
|
3
|
+
import { vueFeatureFlags, vueHydrationPlugin, vuePlugin } from "./plugin.ts"
|
|
4
|
+
import { svgComponent } from "./svg.ts"
|
|
5
|
+
import { markdownComponent } from "./markdown-page.ts"
|
|
6
|
+
|
|
7
|
+
export default defineAdapter({
|
|
8
|
+
name: "vue",
|
|
9
|
+
contract: ADAPTER_CONTRACT,
|
|
10
|
+
extensions: [".vue"],
|
|
11
|
+
clientEntry: "@hamur/adapter-vue/runtime/client",
|
|
12
|
+
serverEntry: "@hamur/adapter-vue/runtime/server",
|
|
13
|
+
keepAliveOutlet: "@hamur/adapter-vue/runtime/keepalive",
|
|
14
|
+
islandEntry: "@hamur/adapter-vue/runtime/island",
|
|
15
|
+
plugins: (ctx) => [vuePlugin(ctx)],
|
|
16
|
+
transforms: (ctx) => (ctx.dev ? [vueFeatureFlags(), ...(ctx.target === "client" ? [vueHydrationPlugin()] : [])] : []),
|
|
17
|
+
errorPage: "@hamur/adapter-vue/error-page",
|
|
18
|
+
peers: ["vue"],
|
|
19
|
+
stream: false,
|
|
20
|
+
trustedTypesPolicies: ["vue"],
|
|
21
|
+
svgComponent,
|
|
22
|
+
markdownComponent,
|
|
23
|
+
serverComponent: { module: (component) => serverComponentModule(component, "@hamur/adapter-vue/runtime") },
|
|
24
|
+
clientComponent: { module: (component) => clientComponentModule(component, "@hamur/adapter-vue/runtime") },
|
|
25
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { isRef, toValue } from "vue"
|
|
2
|
+
import type { MaybeRefOrGetter } from "vue"
|
|
3
|
+
import type { AppContext, AsyncDataOptions } from "hamur/app"
|
|
4
|
+
import { isPreviewMode } from "hamur/adapter"
|
|
5
|
+
import type { AsyncDataHandler, GetCachedData, GetCachedDataCause } from "hamur/adapter"
|
|
6
|
+
|
|
7
|
+
const isPlain = (value: unknown): value is Record<string, unknown> => {
|
|
8
|
+
if (value === null || typeof value !== "object") return false
|
|
9
|
+
const prototype = Object.getPrototypeOf(value)
|
|
10
|
+
return prototype === Object.prototype || prototype === null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A `titleTemplate` and a script's `innerHTML` may be functions of their own, not getters. */
|
|
14
|
+
const plainFunction = (key: string | undefined, list: string | undefined) =>
|
|
15
|
+
key === "titleTemplate" || (list === "script" && key === "innerHTML")
|
|
16
|
+
|
|
17
|
+
function unwrap(value: unknown, getters: boolean, key?: string, list?: string): unknown {
|
|
18
|
+
if (isRef(value)) return unwrap(value.value, getters, key, list)
|
|
19
|
+
if (getters && typeof value === "function" && !plainFunction(key, list)) return unwrap(value(), getters, key, list)
|
|
20
|
+
if (Array.isArray(value)) return value.map((item) => unwrap(item, getters, undefined, key))
|
|
21
|
+
if (isPlain(value))
|
|
22
|
+
return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, unwrap(item, getters, name, list)]))
|
|
23
|
+
return value
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Whether head input holds a ref or a getter at any depth (a `titleTemplate` function and a script's `innerHTML` function are plain values). */
|
|
27
|
+
export function hasReactive(value: unknown, key?: string, list?: string): boolean {
|
|
28
|
+
if (isRef(value) || (typeof value === "function" && !plainFunction(key, list))) return true
|
|
29
|
+
if (Array.isArray(value)) return value.some((item) => hasReactive(item, undefined, key))
|
|
30
|
+
if (isPlain(value)) return Object.entries(value).some(([name, item]) => hasReactive(item, name, list))
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `toValue` of an option, then every ref nested in plain objects and arrays replaced by its value, in a new object. With `getters` (head input) a nested function is called too, except `titleTemplate`. */
|
|
35
|
+
export const deepToValue = (source: MaybeRefOrGetter<unknown>, getters = false): unknown =>
|
|
36
|
+
unwrap(toValue(source), getters)
|
|
37
|
+
|
|
38
|
+
/** The restored payload entry's data while the app hydrates, else `undefined`. */
|
|
39
|
+
export function defaultGetCachedData<T>(key: string, app: AppContext): T | undefined {
|
|
40
|
+
if (!app.asyncData.hydrating || isPreviewMode(app)) return undefined
|
|
41
|
+
const entry = app.asyncData.get<T>(key)
|
|
42
|
+
return entry?.status === "success" ? entry.data : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `refresh()`, `execute()` and a `watch` change: ask `getCachedData` with their cause, run `handler` when it has nothing. */
|
|
46
|
+
export function refreshCached<T, R>(
|
|
47
|
+
app: AppContext,
|
|
48
|
+
key: string,
|
|
49
|
+
handler: AsyncDataHandler<R>,
|
|
50
|
+
options: AsyncDataOptions<T, R>,
|
|
51
|
+
getCachedData: GetCachedData<T>,
|
|
52
|
+
cause: GetCachedDataCause,
|
|
53
|
+
): Promise<void> {
|
|
54
|
+
const cached = getCachedData(key, app, { cause })
|
|
55
|
+
if (cached !== undefined) {
|
|
56
|
+
app.asyncData.fetch<T, R>(key, handler, { ...options, cached })
|
|
57
|
+
return Promise.resolve()
|
|
58
|
+
}
|
|
59
|
+
if (!app.asyncData.has(key)) app.asyncData.fetch<T, R>(key, handler, { ...options, immediate: false })
|
|
60
|
+
return app.asyncData.refresh(key, handler)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const kept = new WeakMap<object, Set<string>>()
|
|
64
|
+
|
|
65
|
+
// Nuxt purges no entry whose caller reads it back through its own getCachedData: the entry is that cache.
|
|
66
|
+
export function keepCached<T>(app: AppContext, key: string, getCachedData: GetCachedData<T>): void {
|
|
67
|
+
if (getCachedData === defaultGetCachedData) return
|
|
68
|
+
let keys = kept.get(app.asyncData)
|
|
69
|
+
if (!keys) kept.set(app.asyncData, (keys = new Set()))
|
|
70
|
+
if (keys.has(key)) return
|
|
71
|
+
keys.add(key)
|
|
72
|
+
app.asyncData.subscribe(key, () => {})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The run's signal, combined with a `signal` the caller gave to `useFetch`. */
|
|
76
|
+
export const signalOf = (own: AbortSignal | undefined, run: AbortSignal): AbortSignal =>
|
|
77
|
+
own ? AbortSignal.any([own, run]) : run
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Suspense, defineAsyncComponent, defineComponent, h, inject, onMounted, ref, shallowRef } from "vue"
|
|
2
|
+
import type { Component, InjectionKey, Slot } from "vue"
|
|
3
|
+
|
|
4
|
+
/** Provided by `ClientOnly` with its `fallback` slot; a client-only component below shows it while its chunk loads. */
|
|
5
|
+
export const ClientOnlyKey: InjectionKey<{ fallback: Slot | undefined }> = Symbol("hamur.clientOnly")
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The client side of a client-only component: a comment in the hydration render, the file's chunk once mounted. A `ref` on it
|
|
9
|
+
* reaches the loaded component's instance through `expose`.
|
|
10
|
+
*/
|
|
11
|
+
export function clientComponent(load: () => Promise<Component | { default: Component }>): Component {
|
|
12
|
+
const Loaded = defineAsyncComponent(load)
|
|
13
|
+
return defineComponent({
|
|
14
|
+
name: "ClientComponent",
|
|
15
|
+
inheritAttrs: false,
|
|
16
|
+
setup(_, { attrs, slots, expose }) {
|
|
17
|
+
const scope = inject(ClientOnlyKey, null)
|
|
18
|
+
const hydrated = ref(scope !== null)
|
|
19
|
+
const inner = shallowRef<Record<PropertyKey, unknown> | null>(null)
|
|
20
|
+
onMounted(() => {
|
|
21
|
+
hydrated.value = true
|
|
22
|
+
})
|
|
23
|
+
expose(
|
|
24
|
+
new Proxy(
|
|
25
|
+
{},
|
|
26
|
+
{
|
|
27
|
+
get: (_target, key) => inner.value?.[key],
|
|
28
|
+
has: (_target, key) => inner.value !== null && key in inner.value,
|
|
29
|
+
},
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
return () => {
|
|
33
|
+
if (!hydrated.value) return null
|
|
34
|
+
const loaded = () => h(Loaded, { ...attrs, ref: inner }, slots)
|
|
35
|
+
return scope ? h(Suspense, null, { default: loaded, fallback: () => scope.fallback?.() }) : loaded()
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script setup lang="ts" generic="T">
|
|
2
|
+
import AwaitValue from "./AwaitValue.ts"
|
|
3
|
+
|
|
4
|
+
const props = defineProps<{ resolve: T }>()
|
|
5
|
+
defineSlots<{
|
|
6
|
+
default(props: { value: Awaited<T> }): unknown
|
|
7
|
+
fallback?(): unknown
|
|
8
|
+
error?(props: { error: Error }): unknown
|
|
9
|
+
}>()
|
|
10
|
+
|
|
11
|
+
const keys = new WeakMap<object, number>()
|
|
12
|
+
let count = 0
|
|
13
|
+
const keyOf = (value: unknown): string | number => {
|
|
14
|
+
if (typeof value !== "object" && typeof value !== "function") return String(value)
|
|
15
|
+
if (value === null) return "null"
|
|
16
|
+
let key = keys.get(value)
|
|
17
|
+
if (key === undefined) keys.set(value, (key = count++))
|
|
18
|
+
return key
|
|
19
|
+
}
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<template>
|
|
23
|
+
<Suspense>
|
|
24
|
+
<AwaitValue :key="keyOf(props.resolve)" :resolve="props.resolve">
|
|
25
|
+
<template #default="{ value }"><slot :value="value" /></template>
|
|
26
|
+
<template v-if="$slots.error" #error="{ error }"><slot name="error" :error="error" /></template>
|
|
27
|
+
</AwaitValue>
|
|
28
|
+
<template #fallback><slot name="fallback" /></template>
|
|
29
|
+
</Suspense>
|
|
30
|
+
</template>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defineComponent } from "vue"
|
|
2
|
+
import type { PropType, SetupContext } from "vue"
|
|
3
|
+
import { settledOf } from "hamur/adapter"
|
|
4
|
+
import type { Settled } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
const isThenable = (value: unknown): value is PromiseLike<unknown> =>
|
|
7
|
+
typeof (value as { then?: unknown } | null)?.then === "function"
|
|
8
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
9
|
+
let warned = false
|
|
10
|
+
|
|
11
|
+
function show(outcome: Settled, slots: SetupContext["slots"]) {
|
|
12
|
+
if (outcome.status === "rejected") {
|
|
13
|
+
if (!slots.error) throw outcome.reason
|
|
14
|
+
return () => slots.error!({ error: outcome.reason })
|
|
15
|
+
}
|
|
16
|
+
if (!slots.default) {
|
|
17
|
+
if (isDev && !warned) {
|
|
18
|
+
warned = true
|
|
19
|
+
console.warn("Await has nothing to render for its value")
|
|
20
|
+
}
|
|
21
|
+
return () => null
|
|
22
|
+
}
|
|
23
|
+
return () => slots.default!({ value: outcome.value })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default defineComponent({
|
|
27
|
+
name: "AwaitValue",
|
|
28
|
+
props: { resolve: { type: null as unknown as PropType<unknown>, required: true } },
|
|
29
|
+
setup(props, { slots }) {
|
|
30
|
+
const resolve = props.resolve
|
|
31
|
+
if (!isThenable(resolve)) return show({ status: "fulfilled", value: resolve }, slots)
|
|
32
|
+
const known = settledOf(resolve)
|
|
33
|
+
if (known) return show(known, slots)
|
|
34
|
+
return Promise.resolve(resolve)
|
|
35
|
+
.then(
|
|
36
|
+
(value): Settled => ({ status: "fulfilled", value }),
|
|
37
|
+
(reason): Settled => ({ status: "rejected", reason }),
|
|
38
|
+
)
|
|
39
|
+
.then((outcome) => show(outcome, slots))
|
|
40
|
+
},
|
|
41
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ComponentInternalInstance } from "vue"
|
|
2
|
+
import { ssrRenderSlot, ssrRenderSlotInner } from "vue/server-renderer"
|
|
3
|
+
import { clientFallback } from "hamur/adapter"
|
|
4
|
+
import type { AppContext } from "hamur/app"
|
|
5
|
+
import { AppKey } from "../context.ts"
|
|
6
|
+
import { flatten } from "./ErrorBoundary.server.ts"
|
|
7
|
+
|
|
8
|
+
interface FallbackState {
|
|
9
|
+
id: string
|
|
10
|
+
caught: { error?: unknown; failed: boolean }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Vue's server renderer writes output as it goes, so the slot is rendered into a buffer and kept only if nothing in it failed.
|
|
15
|
+
* A failed boundary writes its `fallback` slot and records its id for `payload.clientFallback`; the client hydrates the fallback,
|
|
16
|
+
* then renders the children.
|
|
17
|
+
*/
|
|
18
|
+
export function ssrRender(
|
|
19
|
+
_ctx: unknown,
|
|
20
|
+
push: (item: unknown) => void,
|
|
21
|
+
instance: ComponentInternalInstance,
|
|
22
|
+
_attrs: unknown,
|
|
23
|
+
props: { onSsrError?: (error: unknown) => unknown },
|
|
24
|
+
state: FallbackState,
|
|
25
|
+
): void {
|
|
26
|
+
const items: unknown[] = []
|
|
27
|
+
ssrRenderSlotInner(instance.slots, "default", {}, null, (item) => items.push(item), instance)
|
|
28
|
+
push(
|
|
29
|
+
(async () => {
|
|
30
|
+
let thrown: { error: unknown } | undefined
|
|
31
|
+
const html = await flatten(items).catch((error: unknown) => void (thrown = { error }))
|
|
32
|
+
if (state.caught.failed) thrown = { error: state.caught.error }
|
|
33
|
+
if (!thrown) return `<!--[-->${html}<!--]-->`
|
|
34
|
+
const app: AppContext | undefined = instance.appContext.provides[AppKey as symbol]
|
|
35
|
+
if (app) clientFallback(app, state.id, thrown.error, props.onSsrError)
|
|
36
|
+
const buffer: unknown[] = []
|
|
37
|
+
ssrRenderSlot(instance.slots, "fallback", {}, null, (item) => buffer.push(item), instance)
|
|
38
|
+
return flatten(buffer)
|
|
39
|
+
})(),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onErrorCaptured, onMounted, ref, useId } from "vue"
|
|
3
|
+
import { clientFallbackIds } from "../client-fallback.ts"
|
|
4
|
+
|
|
5
|
+
defineProps<{ keepFallback?: boolean; onSsrError?: (error: unknown) => unknown }>()
|
|
6
|
+
|
|
7
|
+
const id = useId()
|
|
8
|
+
const rendered = clientFallbackIds.has(id)
|
|
9
|
+
const fresh = ref(!rendered)
|
|
10
|
+
const caught: { error?: unknown; failed: boolean } = { failed: false }
|
|
11
|
+
// Vue hands a child's setup or render throw to the parents' hooks, not to the caller of the server renderer; the browser leaves it to the app.
|
|
12
|
+
onErrorCaptured((error) => {
|
|
13
|
+
if (typeof window !== "undefined") return
|
|
14
|
+
if (!caught.failed) Object.assign(caught, { error, failed: true })
|
|
15
|
+
return false
|
|
16
|
+
})
|
|
17
|
+
onMounted(() => {
|
|
18
|
+
if (!rendered) return
|
|
19
|
+
clientFallbackIds.delete(id)
|
|
20
|
+
fresh.value = true
|
|
21
|
+
})
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<template>
|
|
25
|
+
<slot v-if="!fresh" name="fallback" />
|
|
26
|
+
<Suspense v-else-if="rendered && keepFallback">
|
|
27
|
+
<slot />
|
|
28
|
+
<template #fallback><slot name="fallback" /></template>
|
|
29
|
+
</Suspense>
|
|
30
|
+
<slot v-else />
|
|
31
|
+
</template>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onMounted, provide, ref, useSlots } from "vue"
|
|
3
|
+
import { ClientOnlyKey } from "../client-component.ts"
|
|
4
|
+
|
|
5
|
+
const hydrated = ref(false)
|
|
6
|
+
const slots = useSlots()
|
|
7
|
+
provide(ClientOnlyKey, { fallback: slots.fallback })
|
|
8
|
+
onMounted(() => {
|
|
9
|
+
hydrated.value = true
|
|
10
|
+
})
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<slot v-if="hydrated" />
|
|
15
|
+
<slot v-else name="fallback" />
|
|
16
|
+
</template>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
3
|
+
import { codeURL } from "./error-code.ts"
|
|
4
|
+
import Link from "./Link.vue"
|
|
5
|
+
|
|
6
|
+
defineProps<{ error: ErrorPageProps }>()
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
<template>
|
|
10
|
+
<main style="font-family: system-ui, sans-serif; text-align: center; padding: 4rem 1rem">
|
|
11
|
+
<h1 style="font-size: 4rem; margin: 0">{{ String(error.status) }}</h1>
|
|
12
|
+
<p>{{ error.message }}</p>
|
|
13
|
+
<p v-if="codeURL && error.code">
|
|
14
|
+
<a :href="codeURL(error.code)" target="_blank" rel="noopener">{{ error.code }}</a>
|
|
15
|
+
</p>
|
|
16
|
+
<pre v-if="error.stack">{{ error.stack }}</pre>
|
|
17
|
+
<Link to="/">Back home</Link>
|
|
18
|
+
</main>
|
|
19
|
+
</template>
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Fragment, createTextVNode, h, isVNode } from "vue"
|
|
2
|
+
import { type AppContext, type State } from "hamur/app"
|
|
3
|
+
import { publicError } from "hamur/adapter"
|
|
4
|
+
import type { ComponentInternalInstance, VNode } from "vue"
|
|
5
|
+
import { ssrRenderSlot, ssrRenderSlotInner, ssrRenderVNode } from "vue/server-renderer"
|
|
6
|
+
import type { ErrorBoundaryFallbackProps } from "hamur/adapter"
|
|
7
|
+
import { AppKey } from "../context.ts"
|
|
8
|
+
|
|
9
|
+
interface BoundaryState {
|
|
10
|
+
key: string
|
|
11
|
+
state: State | null
|
|
12
|
+
failed: boolean
|
|
13
|
+
error: unknown
|
|
14
|
+
reset: () => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function flatten(item: unknown): Promise<string> {
|
|
18
|
+
if (typeof item === "string") return item
|
|
19
|
+
if (Array.isArray(item)) {
|
|
20
|
+
let out = ""
|
|
21
|
+
for (const part of item) out += await flatten(part)
|
|
22
|
+
return out
|
|
23
|
+
}
|
|
24
|
+
return flatten(await (item as Promise<unknown>))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function toVNode(value: unknown): VNode {
|
|
28
|
+
if (isVNode(value)) return value
|
|
29
|
+
return Array.isArray(value) ? h(Fragment, null, value) : createTextVNode(String(value ?? ""))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const remember = ({ state, key }: BoundaryState, error: unknown) => {
|
|
33
|
+
if (error instanceof Error) state?.set(key, { name: error.name, message: error.message })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Vue's server renderer writes output as it goes, so the slot is rendered into a buffer and kept only if nothing in it failed.
|
|
38
|
+
* A failed boundary is written under its id to the app state, which the payload carries to the client.
|
|
39
|
+
* The client then starts in the fallback while it hydrates (the markup matches) instead of rendering the child again.
|
|
40
|
+
*/
|
|
41
|
+
export function ssrRender(
|
|
42
|
+
_ctx: unknown,
|
|
43
|
+
push: (item: unknown) => void,
|
|
44
|
+
instance: ComponentInternalInstance,
|
|
45
|
+
_attrs: unknown,
|
|
46
|
+
props: { fallback?: (props: ErrorBoundaryFallbackProps) => unknown },
|
|
47
|
+
state: BoundaryState,
|
|
48
|
+
): void {
|
|
49
|
+
const items: unknown[] = []
|
|
50
|
+
ssrRenderSlotInner(instance.slots, "default", {}, null, (item) => items.push(item), instance)
|
|
51
|
+
push(
|
|
52
|
+
(async () => {
|
|
53
|
+
const html = await flatten(items).catch((error: unknown) => {
|
|
54
|
+
state.error = error
|
|
55
|
+
state.failed = true
|
|
56
|
+
})
|
|
57
|
+
if (!state.failed) return `<!--[-->${html}<!--]-->`
|
|
58
|
+
const app: AppContext | undefined = instance.appContext.provides[AppKey as symbol]
|
|
59
|
+
if (app) state.error = publicError(app, state.error)
|
|
60
|
+
remember(state, state.error)
|
|
61
|
+
const buffer: unknown[] = []
|
|
62
|
+
const out = (item: unknown) => buffer.push(item)
|
|
63
|
+
const slotProps = { error: state.error, reset: state.reset }
|
|
64
|
+
if (props.fallback) ssrRenderVNode(out, toVNode(props.fallback(slotProps)), instance)
|
|
65
|
+
else ssrRenderSlot(instance.slots, "fallback", slotProps, null, out, instance)
|
|
66
|
+
return flatten(buffer)
|
|
67
|
+
})(),
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { inject, onErrorCaptured, ref, shallowRef, useId } from "vue"
|
|
3
|
+
import { StateKey } from "../context.ts"
|
|
4
|
+
import type { ErrorBoundaryFallbackProps } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
const props = defineProps<{ fallback?: (props: ErrorBoundaryFallbackProps) => unknown }>()
|
|
7
|
+
|
|
8
|
+
const state = inject(StateKey, null)
|
|
9
|
+
const key = `$boundary:${useId()}`
|
|
10
|
+
const seeded = state?.get<{ name: string; message: string }>(key)
|
|
11
|
+
if (seeded) state?.set(key, undefined)
|
|
12
|
+
|
|
13
|
+
const failed = ref(seeded !== undefined)
|
|
14
|
+
const error = shallowRef<unknown>(seeded && Object.assign(new Error(seeded.message), { name: seeded.name }))
|
|
15
|
+
const reset = () => {
|
|
16
|
+
failed.value = false
|
|
17
|
+
error.value = undefined
|
|
18
|
+
}
|
|
19
|
+
const Fallback = () => props.fallback?.({ error: error.value, reset }) as never
|
|
20
|
+
|
|
21
|
+
onErrorCaptured((caught) => {
|
|
22
|
+
error.value = caught
|
|
23
|
+
failed.value = true
|
|
24
|
+
return false
|
|
25
|
+
})
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<template>
|
|
29
|
+
<slot v-if="!failed" />
|
|
30
|
+
<Fallback v-else-if="fallback" />
|
|
31
|
+
<slot v-else name="fallback" :error="error" :reset="reset" />
|
|
32
|
+
</template>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, inject, onBeforeUnmount, onMounted, provide, ref, type PropType } from "vue"
|
|
3
|
+
import { enhance, type FormSubmitArgs, type RouteLocation } from "hamur/app"
|
|
4
|
+
import { assertFormMethod, createFormStatus, functionFormAction, runFunctionForm } from "hamur/adapter"
|
|
5
|
+
import { AppKey, FormStatusKey, HrefKey, RouteKey, RouterKey, detachedHref } from "../context.ts"
|
|
6
|
+
|
|
7
|
+
defineOptions({ inheritAttrs: false })
|
|
8
|
+
|
|
9
|
+
const props = defineProps({
|
|
10
|
+
action: {
|
|
11
|
+
type: [String, Object, Function] as PropType<
|
|
12
|
+
string | RouteLocation | (((...args: never[]) => unknown) & { readonly url: string })
|
|
13
|
+
>,
|
|
14
|
+
default: undefined,
|
|
15
|
+
},
|
|
16
|
+
method: { type: String as PropType<"get" | "post">, default: undefined },
|
|
17
|
+
encType: {
|
|
18
|
+
type: String as PropType<"application/x-www-form-urlencoded" | "multipart/form-data">,
|
|
19
|
+
default: undefined,
|
|
20
|
+
},
|
|
21
|
+
replace: Boolean,
|
|
22
|
+
reset: Boolean,
|
|
23
|
+
reloadDocument: Boolean,
|
|
24
|
+
refresh: { type: Boolean, default: undefined },
|
|
25
|
+
instance: { type: [String, Number] as PropType<string | number>, default: undefined },
|
|
26
|
+
restore: { type: Boolean, default: true },
|
|
27
|
+
onSubmit: { type: Function as PropType<(args: FormSubmitArgs) => void>, default: undefined },
|
|
28
|
+
})
|
|
29
|
+
const resolve = inject(HrefKey, detachedHref)
|
|
30
|
+
const route = inject(RouteKey, null)
|
|
31
|
+
const element = ref<HTMLFormElement>()
|
|
32
|
+
assertFormMethod(props.action, props.method)
|
|
33
|
+
const target = computed(() => {
|
|
34
|
+
const page = route ? route.fullPath.replace(/#.*$/, "") : undefined
|
|
35
|
+
if (typeof props.action === "function")
|
|
36
|
+
return functionFormAction(props.action as never, resolve(page ?? "/"), props.instance)
|
|
37
|
+
return props.action !== undefined ? resolve(props.action) : page !== undefined ? resolve(page) : undefined
|
|
38
|
+
})
|
|
39
|
+
const status = createFormStatus()
|
|
40
|
+
provide(FormStatusKey, status)
|
|
41
|
+
const app = inject(AppKey, null)
|
|
42
|
+
const router = inject(RouterKey, null)
|
|
43
|
+
let stop: (() => void) | undefined
|
|
44
|
+
onMounted(() => {
|
|
45
|
+
stop = enhance(element.value!, {
|
|
46
|
+
onSubmit: (args) => {
|
|
47
|
+
status.submitted(args.formData)
|
|
48
|
+
props.onSubmit?.(args)
|
|
49
|
+
},
|
|
50
|
+
...(typeof props.action === "function" && app
|
|
51
|
+
? {
|
|
52
|
+
call: (input: Record<string, unknown>, formData: FormData) =>
|
|
53
|
+
runFunctionForm(
|
|
54
|
+
app,
|
|
55
|
+
props.action as never,
|
|
56
|
+
input,
|
|
57
|
+
formData,
|
|
58
|
+
() => router?.refresh() ?? Promise.resolve(),
|
|
59
|
+
status,
|
|
60
|
+
{ refresh: props.refresh, instance: props.instance },
|
|
61
|
+
),
|
|
62
|
+
}
|
|
63
|
+
: {}),
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
onBeforeUnmount(() => stop?.())
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<template>
|
|
70
|
+
<form
|
|
71
|
+
ref="element"
|
|
72
|
+
v-bind="$attrs"
|
|
73
|
+
:method="typeof action === 'function' ? 'post' : 'get'"
|
|
74
|
+
:action="target"
|
|
75
|
+
:enctype="encType"
|
|
76
|
+
data-hamur-form=""
|
|
77
|
+
:data-hamur-replace="replace ? '' : undefined"
|
|
78
|
+
:data-hamur-reset="reset ? '' : undefined"
|
|
79
|
+
:data-hamur-reload="reloadDocument ? '' : undefined"
|
|
80
|
+
:data-hamur-refresh="refresh === false ? 'false' : undefined"
|
|
81
|
+
:data-hamur-restore="restore === false ? 'false' : undefined"
|
|
82
|
+
>
|
|
83
|
+
<slot />
|
|
84
|
+
</form>
|
|
85
|
+
</template>
|