@hamur/adapter-svelte 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-324qgh1c.js +190 -0
- package/dist/chunks/browser-index-evqp5a71.js +64 -0
- package/dist/chunks/browser-index-xc6081e3.js +9 -0
- package/dist/src/adapter.js +480 -0
- package/dist/src/index.js +1161 -0
- package/dist/src/runtime/client.js +65 -0
- package/dist/src/runtime/island.js +72 -0
- package/dist/src/runtime/server.js +142 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/boundary-error.d.ts +9 -0
- package/dist/types/src/client-only.d.ts +6 -0
- package/dist/types/src/context.d.ts +87 -0
- package/dist/types/src/deferred.svelte.d.ts +6 -0
- package/dist/types/src/effect.svelte.d.ts +2 -0
- package/dist/types/src/hydrating.d.ts +4 -0
- package/dist/types/src/index.d.ts +270 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/keepalive.d.ts +11 -0
- package/dist/types/src/lazy-hydration.d.ts +25 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/plugin.d.ts +52 -0
- package/dist/types/src/runtime/client.d.ts +12 -0
- package/dist/types/src/runtime/hmr.d.ts +28 -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-component.d.ts +3 -0
- package/dist/types/src/store.d.ts +7 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +43 -4
- package/src/Announcer.svelte +26 -0
- package/src/Await.svelte +43 -0
- package/src/AwaitAsync.svelte +42 -0
- package/src/BodyAttrs.svelte +7 -0
- package/src/Caught.svelte +41 -0
- package/src/ClientComponent.svelte +35 -0
- package/src/ClientFallback.svelte +46 -0
- package/src/ClientOnly.svelte +22 -0
- package/src/DefaultErrorPage.svelte +15 -0
- package/src/DevOnly.svelte +12 -0
- package/src/ErrorBoundary.svelte +14 -0
- package/src/FallbackEffect.svelte +9 -0
- package/src/Form.svelte +96 -0
- package/src/HeadLink.svelte +7 -0
- package/src/HeadMeta.svelte +7 -0
- package/src/HeadScript.svelte +7 -0
- package/src/HeadStyle.svelte +7 -0
- package/src/HeadTitle.svelte +6 -0
- package/src/HtmlAttrs.svelte +7 -0
- package/src/HttpHeader.svelte +7 -0
- package/src/HttpStatusCode.svelte +7 -0
- package/src/Island.svelte +59 -0
- package/src/IslandChildren.svelte +7 -0
- package/src/KeptOutlet.svelte +46 -0
- package/src/KeptPage.svelte +55 -0
- package/src/LazyHydration.svelte +54 -0
- package/src/Link.svelte +209 -0
- package/src/LoadingIndicator.svelte +18 -0
- package/src/MarkdownNodes.svelte +36 -0
- package/src/MarkdownRenderer.svelte +20 -0
- package/src/MarkdownSlots.svelte +33 -0
- package/src/Navigate.svelte +10 -0
- package/src/Nest.svelte +83 -0
- package/src/Outlet.svelte +31 -0
- package/src/Rethrow.svelte +7 -0
- package/src/Root.svelte +29 -0
- package/src/RouteAnnouncer.svelte +47 -0
- package/src/Scoped.svelte +11 -0
- package/src/ServerComponent.svelte +19 -0
- package/src/ServerComponentClient.svelte +64 -0
- package/src/ServerIsland.svelte +26 -0
- package/src/Slotted.svelte +11 -0
- package/src/Thrown.svelte +9 -0
- package/src/Time.svelte +23 -0
- package/src/activation.ts +51 -0
- package/src/adapter.ts +24 -0
- package/src/async-data.ts +105 -0
- package/src/boundary-error.ts +77 -0
- package/src/client-only.ts +8 -0
- package/src/context.ts +227 -0
- package/src/deferred.svelte.ts +33 -0
- package/src/effect.svelte.ts +7 -0
- package/src/env.d.ts +3 -0
- package/src/hydrating.ts +18 -0
- package/src/index.ts +1337 -0
- package/src/island-html.ts +58 -0
- package/src/keepalive.ts +12 -0
- package/src/lazy-hydration.ts +89 -0
- package/src/markdown-page.ts +17 -0
- package/src/plugin.ts +558 -0
- package/src/runtime/client.ts +61 -0
- package/src/runtime/hmr.ts +177 -0
- package/src/runtime/hydration-dev.ts +22 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +94 -0
- package/src/runtime/server.ts +87 -0
- package/src/server-component.ts +13 -0
- package/src/store.ts +31 -0
- package/src/svelte.d.ts +5 -0
- package/src/svg.ts +30 -0
- package/src/webmcp.d.ts +18 -0
package/src/Link.svelte
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends string = string">
|
|
2
|
+
import { getContext, setContext, type Snippet } from "svelte"
|
|
3
|
+
import type { HTMLAnchorAttributes } from "svelte/elements"
|
|
4
|
+
import { withoutBase, type HamurLinkProps, type RouteLocation } from "hamur/app"
|
|
5
|
+
import { createSubscriber } from "svelte/reactivity"
|
|
6
|
+
import {
|
|
7
|
+
LINK_DEFAULTS,
|
|
8
|
+
createLinkSlot,
|
|
9
|
+
linkAttributes,
|
|
10
|
+
linkDefaultsOf,
|
|
11
|
+
linkExtraAttributes,
|
|
12
|
+
linkPending,
|
|
13
|
+
linkPrefetchProps,
|
|
14
|
+
linkState,
|
|
15
|
+
prefetchAttribute,
|
|
16
|
+
viewTransitionAttribute,
|
|
17
|
+
relativeTarget,
|
|
18
|
+
resolveLinkTarget,
|
|
19
|
+
searchTarget,
|
|
20
|
+
setLinkState,
|
|
21
|
+
type CheckedTarget,
|
|
22
|
+
type LinkDefaults,
|
|
23
|
+
type LinkSlot,
|
|
24
|
+
type Navigation,
|
|
25
|
+
type PrefetchOn,
|
|
26
|
+
warnRemovedLinkProps,
|
|
27
|
+
} from "hamur/adapter"
|
|
28
|
+
import { APP_KEY, LEVEL_KEY, LINK_KEY, detachedHref, type AppState, type Level } from "./context.ts"
|
|
29
|
+
import { track, type Source } from "./store.ts"
|
|
30
|
+
|
|
31
|
+
interface Props extends Omit<HTMLAnchorAttributes, "href" | "children">, HamurLinkProps {
|
|
32
|
+
to: CheckedTarget<T> | RouteLocation
|
|
33
|
+
/** Removed: write `to`. */
|
|
34
|
+
href?: never
|
|
35
|
+
replace?: boolean
|
|
36
|
+
prefetch?: boolean
|
|
37
|
+
prefetchOn?: PrefetchOn
|
|
38
|
+
/** Removed: write `prefetch={false}`. */
|
|
39
|
+
noPrefetch?: never
|
|
40
|
+
viewTransition?: boolean | { types: readonly string[] }
|
|
41
|
+
query?: (prev: never) => Record<string, unknown>
|
|
42
|
+
state?: Record<string, unknown>
|
|
43
|
+
relative?: "route"
|
|
44
|
+
mask?: string
|
|
45
|
+
unmaskOnReload?: boolean
|
|
46
|
+
activeClass?: string
|
|
47
|
+
exactActiveClass?: string
|
|
48
|
+
pendingClass?: string
|
|
49
|
+
ariaCurrentValue?: "page" | "step" | "location" | "date" | "time" | "true"
|
|
50
|
+
external?: boolean
|
|
51
|
+
noRel?: boolean
|
|
52
|
+
prefetchedClass?: string
|
|
53
|
+
scroll?: boolean
|
|
54
|
+
prefetchDelay?: number
|
|
55
|
+
onBeforeNavigate?: (nav: Navigation) => void
|
|
56
|
+
/** Renders only the `children` snippet, given the `LinkSlot`, instead of an `<a>`. */
|
|
57
|
+
custom?: boolean
|
|
58
|
+
/** Set by `defineLink`; read over `app.link`. */
|
|
59
|
+
defaults?: LinkDefaults
|
|
60
|
+
children?: Snippet<[LinkSlot]>
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let {
|
|
64
|
+
to,
|
|
65
|
+
href: removedHref,
|
|
66
|
+
query,
|
|
67
|
+
state: entryState,
|
|
68
|
+
relative,
|
|
69
|
+
mask,
|
|
70
|
+
unmaskOnReload,
|
|
71
|
+
replace = false,
|
|
72
|
+
prefetch,
|
|
73
|
+
prefetchOn,
|
|
74
|
+
noPrefetch: removedNoPrefetch,
|
|
75
|
+
viewTransition,
|
|
76
|
+
activeClass,
|
|
77
|
+
exactActiveClass,
|
|
78
|
+
pendingClass,
|
|
79
|
+
ariaCurrentValue = "page",
|
|
80
|
+
external,
|
|
81
|
+
noRel,
|
|
82
|
+
prefetchedClass,
|
|
83
|
+
scroll,
|
|
84
|
+
prefetchDelay,
|
|
85
|
+
onBeforeNavigate,
|
|
86
|
+
custom = false,
|
|
87
|
+
defaults,
|
|
88
|
+
children,
|
|
89
|
+
...rest
|
|
90
|
+
}: Props = $props()
|
|
91
|
+
warnRemovedLinkProps({ href: removedHref, noPrefetch: removedNoPrefetch, prefetch })
|
|
92
|
+
const source = getContext<Source<AppState> | undefined>(APP_KEY)
|
|
93
|
+
const read = source && track(source)
|
|
94
|
+
const level = getContext<Level | undefined>(LEVEL_KEY)
|
|
95
|
+
const baseURL = source?.get().baseURL ?? "/"
|
|
96
|
+
const resolved = $derived(linkDefaultsOf(source?.get().app, defaults))
|
|
97
|
+
const classes = $derived({
|
|
98
|
+
active: activeClass ?? resolved.activeClass,
|
|
99
|
+
exact: exactActiveClass ?? resolved.exactActiveClass,
|
|
100
|
+
pending: pendingClass ?? resolved.pendingClass,
|
|
101
|
+
prefetched: prefetchedClass ?? resolved.prefetchedClass,
|
|
102
|
+
})
|
|
103
|
+
const mode = $derived(external ? "false" : prefetchAttribute(linkPrefetchProps({ prefetch, prefetchOn }, resolved)))
|
|
104
|
+
const given = $derived(to)
|
|
105
|
+
const linked = $derived(
|
|
106
|
+
resolveLinkTarget(
|
|
107
|
+
read?.().app,
|
|
108
|
+
read ? searchTarget(relativeTarget(given, level?.route ?? read().route, relative), read().route, query) : given,
|
|
109
|
+
rest,
|
|
110
|
+
read?.().href,
|
|
111
|
+
external,
|
|
112
|
+
),
|
|
113
|
+
)
|
|
114
|
+
const target = $derived(linked.to)
|
|
115
|
+
const attrs = $derived(linked.props)
|
|
116
|
+
const href = $derived(read ? read().href(target) : detachedHref(target))
|
|
117
|
+
setContext(LINK_KEY, () => href)
|
|
118
|
+
const current = $derived(
|
|
119
|
+
read
|
|
120
|
+
? linkState(typeof target === "string" ? target : withoutBase(href, baseURL), read().route)
|
|
121
|
+
: { active: false, exact: false },
|
|
122
|
+
)
|
|
123
|
+
let anchor: HTMLAnchorElement | undefined = $state()
|
|
124
|
+
$effect(() => {
|
|
125
|
+
const element = anchor
|
|
126
|
+
const before = onBeforeNavigate
|
|
127
|
+
if (!element || (entryState === undefined && !before)) return
|
|
128
|
+
setLinkState(element, entryState, before ? (nav) => onBeforeNavigate?.(nav) : undefined)
|
|
129
|
+
return () => setLinkState(element, undefined)
|
|
130
|
+
})
|
|
131
|
+
const extras = $derived(
|
|
132
|
+
linkExtraAttributes({
|
|
133
|
+
href,
|
|
134
|
+
external,
|
|
135
|
+
noRel,
|
|
136
|
+
rel: rest.rel,
|
|
137
|
+
target: rest.target,
|
|
138
|
+
prefetchedClass: classes.prefetched,
|
|
139
|
+
scroll,
|
|
140
|
+
prefetchDelay: prefetchDelay ?? resolved.prefetchDelay,
|
|
141
|
+
externalRelAttribute: resolved.externalRelAttribute,
|
|
142
|
+
}),
|
|
143
|
+
)
|
|
144
|
+
const subscribeNavigation = createSubscriber((update) => source?.get().navigation?.subscribe(update) ?? (() => {}))
|
|
145
|
+
const slot = $derived.by(() => {
|
|
146
|
+
subscribeNavigation()
|
|
147
|
+
const app = read?.()
|
|
148
|
+
return createLinkSlot({
|
|
149
|
+
href,
|
|
150
|
+
to: target,
|
|
151
|
+
attrs: linkAttributes({
|
|
152
|
+
href,
|
|
153
|
+
external,
|
|
154
|
+
...linkPrefetchProps({ prefetch, prefetchOn }, resolved),
|
|
155
|
+
replace,
|
|
156
|
+
viewTransition,
|
|
157
|
+
pendingClass: classes.pending,
|
|
158
|
+
trim: !!query,
|
|
159
|
+
mask: mask === undefined ? undefined : app ? app.href(mask) : detachedHref(mask),
|
|
160
|
+
unmaskOnReload,
|
|
161
|
+
ariaCurrent: current.exact ? ariaCurrentValue : undefined,
|
|
162
|
+
rel: rest.rel,
|
|
163
|
+
target: rest.target,
|
|
164
|
+
noRel,
|
|
165
|
+
prefetchedClass: classes.prefetched,
|
|
166
|
+
scroll,
|
|
167
|
+
prefetchDelay: prefetchDelay ?? resolved.prefetchDelay,
|
|
168
|
+
externalRelAttribute: resolved.externalRelAttribute,
|
|
169
|
+
}),
|
|
170
|
+
active: current.active,
|
|
171
|
+
exact: current.exact,
|
|
172
|
+
pending: !!app && linkPending(href, app.navigation.get(), app.baseURL),
|
|
173
|
+
external,
|
|
174
|
+
options: {
|
|
175
|
+
replace,
|
|
176
|
+
state: entryState,
|
|
177
|
+
mask,
|
|
178
|
+
unmaskOnReload,
|
|
179
|
+
viewTransition,
|
|
180
|
+
scroll: scroll === false ? false : undefined,
|
|
181
|
+
},
|
|
182
|
+
router: app?.router ?? null,
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
const plain = $derived(children as Snippet | undefined)
|
|
186
|
+
const marked = $derived({
|
|
187
|
+
...attrs,
|
|
188
|
+
...extras,
|
|
189
|
+
...(current.active ? { class: [rest.class, classes.active, current.exact && classes.exact] } : {}),
|
|
190
|
+
...(current.exact && attrs["aria-current"] === undefined ? { "aria-current": ariaCurrentValue } : {}),
|
|
191
|
+
})
|
|
192
|
+
</script>
|
|
193
|
+
|
|
194
|
+
{#if custom}
|
|
195
|
+
{@render children?.(slot)}
|
|
196
|
+
{:else}
|
|
197
|
+
<a
|
|
198
|
+
bind:this={anchor}
|
|
199
|
+
{...marked}
|
|
200
|
+
{href}
|
|
201
|
+
data-hamur-prefetch={mode}
|
|
202
|
+
data-hamur-replace={replace ? "" : undefined}
|
|
203
|
+
data-hamur-view-transition={viewTransitionAttribute(viewTransition)}
|
|
204
|
+
data-hamur-pending-class={classes.pending === LINK_DEFAULTS.pendingClass ? undefined : classes.pending}
|
|
205
|
+
data-hamur-trim={query ? "" : undefined}
|
|
206
|
+
data-hamur-mask={mask === undefined ? undefined : read ? read().href(mask) : detachedHref(mask)}
|
|
207
|
+
data-hamur-unmask={mask !== undefined && unmaskOnReload === false ? "false" : undefined}>{@render plain?.()}</a
|
|
208
|
+
>
|
|
209
|
+
{/if}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { loadingIndicatorOptions, paintLoadingIndicator, type LoadingIndicatorProps } from "hamur/adapter"
|
|
3
|
+
import { useLoadingIndicator } from "./index.ts"
|
|
4
|
+
|
|
5
|
+
let props: LoadingIndicatorProps = $props()
|
|
6
|
+
const indicator = useLoadingIndicator(loadingIndicatorOptions(props))
|
|
7
|
+
let element: HTMLElement | undefined = $state()
|
|
8
|
+
$effect(() => {
|
|
9
|
+
if (element)
|
|
10
|
+
paintLoadingIndicator(
|
|
11
|
+
element,
|
|
12
|
+
{ progress: indicator.progress, isLoading: indicator.isLoading, error: indicator.error },
|
|
13
|
+
{ color: props.color, errorColor: props.errorColor, height: props.height },
|
|
14
|
+
)
|
|
15
|
+
})
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<div bind:this={element} data-hamur-loading-indicator aria-hidden="true"></div>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { markdownLink, type MdView, type MdViewComponent } from "hamur/adapter"
|
|
4
|
+
import Link from "./Link.svelte"
|
|
5
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
6
|
+
import MarkdownSlots from "./MarkdownSlots.svelte"
|
|
7
|
+
|
|
8
|
+
let { views, components }: { views: MdView[]; components: Record<string, Component<any>> } = $props()
|
|
9
|
+
const VOID = new Set(["hr", "br", "img", "input"])
|
|
10
|
+
const resolve = (view: MdViewComponent) =>
|
|
11
|
+
Object.hasOwn(components, view.component) ? components[view.component] : undefined
|
|
12
|
+
const slotsOf = (view: MdViewComponent): Array<[string, MdView[]]> => [
|
|
13
|
+
...(view.children.length ? [["children", view.children] as [string, MdView[]]] : []),
|
|
14
|
+
...Object.entries(view.slots),
|
|
15
|
+
]
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
{#each views as view}{@const link =
|
|
19
|
+
typeof view === "object" && "tag" in view ? markdownLink(view) : undefined}{@const target =
|
|
20
|
+
typeof view === "object" && "component" in view
|
|
21
|
+
? resolve(view)
|
|
22
|
+
: undefined}{#if typeof view === "string"}{view}{:else if "html" in view}<hamur-html style="display:contents"
|
|
23
|
+
>{@html view.html}</hamur-html
|
|
24
|
+
>{:else if "component" in view}{#if target}<MarkdownSlots
|
|
25
|
+
component={target}
|
|
26
|
+
{view}
|
|
27
|
+
{components}
|
|
28
|
+
slots={slotsOf(view)}
|
|
29
|
+
/>{:else if view.attrs}<svelte:element this={view.component} {...view.attrs}
|
|
30
|
+
><MarkdownNodes views={view.children} {components} /></svelte:element
|
|
31
|
+
>{:else}<MarkdownNodes views={view.children} {components} />{/if}{:else if link}<Link {...link.attrs} to={link.to}
|
|
32
|
+
><MarkdownNodes views={view.children} {components} /></Link
|
|
33
|
+
>{:else if VOID.has(view.tag)}<svelte:element this={view.tag} {...view.attrs} />{:else}<svelte:element
|
|
34
|
+
this={view.tag}
|
|
35
|
+
{...view.attrs}><MarkdownNodes views={view.children} {components} /></svelte:element
|
|
36
|
+
>{/if}{/each}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { markdownProse, markdownView } from "hamur/adapter"
|
|
4
|
+
import type { MarkdownDocument, MdNode } from "hamur/markdown"
|
|
5
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
value,
|
|
9
|
+
components = {},
|
|
10
|
+
raw,
|
|
11
|
+
}: {
|
|
12
|
+
value: MdNode | MdNode[] | MarkdownDocument
|
|
13
|
+
components?: Record<string, Component<any>>
|
|
14
|
+
raw?: (html: string) => string
|
|
15
|
+
} = $props()
|
|
16
|
+
const prose = $derived(markdownProse(value, components))
|
|
17
|
+
const views = $derived(markdownView(prose.value, { html: raw !== undefined, components: prose.components }))
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<MarkdownNodes {views} components={prose.components} />
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component, Snippet } from "svelte"
|
|
3
|
+
import type { MdView, MdViewComponent } from "hamur/adapter"
|
|
4
|
+
import MarkdownNodes from "./MarkdownNodes.svelte"
|
|
5
|
+
import MarkdownSlots from "./MarkdownSlots.svelte"
|
|
6
|
+
|
|
7
|
+
// One level per slot: a snippet takes no arguments when the component renders it, so each level closes over its own slot.
|
|
8
|
+
let {
|
|
9
|
+
component: Target,
|
|
10
|
+
view,
|
|
11
|
+
components,
|
|
12
|
+
slots,
|
|
13
|
+
index = 0,
|
|
14
|
+
snippets = {},
|
|
15
|
+
}: {
|
|
16
|
+
component: Component<any>
|
|
17
|
+
view: MdViewComponent
|
|
18
|
+
components: Record<string, Component<any>>
|
|
19
|
+
slots: Array<[string, MdView[]]>
|
|
20
|
+
index?: number
|
|
21
|
+
snippets?: Record<string, Snippet>
|
|
22
|
+
} = $props()
|
|
23
|
+
const entry = $derived(slots[index])
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
{#snippet slot()}<MarkdownNodes views={entry![1]} {components} />{/snippet}{#if entry}<MarkdownSlots
|
|
27
|
+
component={Target}
|
|
28
|
+
{view}
|
|
29
|
+
{components}
|
|
30
|
+
{slots}
|
|
31
|
+
index={index + 1}
|
|
32
|
+
snippets={{ ...snippets, [entry[0]]: slot }}
|
|
33
|
+
/>{:else}<Target {...view.props} {...snippets} />{/if}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends string = string">
|
|
2
|
+
import { onMount, untrack } from "svelte"
|
|
3
|
+
import { navigateTo, redirect, type RouteLocation } from "hamur/app"
|
|
4
|
+
import type { CheckedTarget } from "hamur/adapter"
|
|
5
|
+
|
|
6
|
+
let { to, replace }: { to: CheckedTarget<T, false> | RouteLocation; replace?: boolean } = $props()
|
|
7
|
+
|
|
8
|
+
if (typeof document === "undefined") throw redirect<string>(untrack(() => to) as string | RouteLocation)
|
|
9
|
+
onMount(() => void navigateTo<string>(to as string | RouteLocation, { replace }))
|
|
10
|
+
</script>
|
package/src/Nest.svelte
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import type { Component as SvelteComponent } from "svelte"
|
|
4
|
+
import type { KeptUnit, RenderTree } from "hamur/adapter"
|
|
5
|
+
import { LEVEL_KEY, keepAliveBase, keptOutlet } from "./context.ts"
|
|
6
|
+
import type { RouteValue } from "hamur/adapter"
|
|
7
|
+
import Nest from "./Nest.svelte"
|
|
8
|
+
import Caught from "./Caught.svelte"
|
|
9
|
+
import Thrown from "./Thrown.svelte"
|
|
10
|
+
|
|
11
|
+
let {
|
|
12
|
+
components,
|
|
13
|
+
index = 0,
|
|
14
|
+
route,
|
|
15
|
+
top,
|
|
16
|
+
bare = false,
|
|
17
|
+
}: {
|
|
18
|
+
components: RenderTree["components"]
|
|
19
|
+
index?: number
|
|
20
|
+
route?: RouteValue
|
|
21
|
+
top?: { kept?: KeptUnit[] }
|
|
22
|
+
bare?: boolean
|
|
23
|
+
} = $props()
|
|
24
|
+
const entry = $derived(components[index])
|
|
25
|
+
const Component = $derived(
|
|
26
|
+
((import.meta.hot
|
|
27
|
+
? (globalThis as Record<symbol, ((c: unknown) => unknown) | undefined>)[Symbol.for("hamur.svelte.hot")]?.(
|
|
28
|
+
entry?.component,
|
|
29
|
+
)
|
|
30
|
+
: undefined) ?? entry?.component) as SvelteComponent<any>,
|
|
31
|
+
)
|
|
32
|
+
const thrown = $derived(entry?.caught !== undefined)
|
|
33
|
+
const last = $derived(index >= components.length - 1)
|
|
34
|
+
const kept = $derived(bare ? undefined : (index === 0 ? top : components[index - 1])?.kept)
|
|
35
|
+
const Kept = $derived(kept ? keptOutlet() : undefined)
|
|
36
|
+
setContext(LEVEL_KEY, {
|
|
37
|
+
get id() {
|
|
38
|
+
return entry?.id
|
|
39
|
+
},
|
|
40
|
+
get components() {
|
|
41
|
+
return components
|
|
42
|
+
},
|
|
43
|
+
get index() {
|
|
44
|
+
return index
|
|
45
|
+
},
|
|
46
|
+
get route() {
|
|
47
|
+
return route
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
{#snippet level()}
|
|
53
|
+
<Component {...entry?.props}>
|
|
54
|
+
{#if !last}
|
|
55
|
+
<Nest {components} {route} index={index + 1} />
|
|
56
|
+
{/if}
|
|
57
|
+
</Component>
|
|
58
|
+
{/snippet}
|
|
59
|
+
|
|
60
|
+
{#snippet body(entry: RenderTree["components"][number])}
|
|
61
|
+
{#if entry.boundary || thrown}
|
|
62
|
+
<svelte:boundary>
|
|
63
|
+
{#if thrown}
|
|
64
|
+
<Thrown error={entry.props?.error as never} />
|
|
65
|
+
{:else}
|
|
66
|
+
{@render level()}
|
|
67
|
+
{/if}
|
|
68
|
+
{#snippet failed(error, reset)}
|
|
69
|
+
<Caught {entry} {error} {reset} {thrown} />
|
|
70
|
+
{/snippet}
|
|
71
|
+
</svelte:boundary>
|
|
72
|
+
{:else}
|
|
73
|
+
{@render level()}
|
|
74
|
+
{/if}
|
|
75
|
+
{/snippet}
|
|
76
|
+
|
|
77
|
+
{#if kept && Kept}
|
|
78
|
+
<Kept {kept} {entry} {components} {index} {route} {Nest} base={keepAliveBase} />
|
|
79
|
+
{:else if entry}
|
|
80
|
+
{#key entry.key}
|
|
81
|
+
{@render body(entry)}
|
|
82
|
+
{/key}
|
|
83
|
+
{/if}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, setContext } from "svelte"
|
|
3
|
+
import { noteOutlet } from "hamur/adapter"
|
|
4
|
+
import { LEVEL_KEY, OUTLET_VALUE_KEY, interceptedRoute, useAppState } from "./context.ts"
|
|
5
|
+
import type { Level } from "./context.ts"
|
|
6
|
+
import Nest from "./Nest.svelte"
|
|
7
|
+
|
|
8
|
+
let { context, name }: { context?: unknown; name?: string } = $props()
|
|
9
|
+
const level = getContext<Level | undefined>(LEVEL_KEY)
|
|
10
|
+
if (!level) throw new Error("Outlet must be rendered inside a Hamur app")
|
|
11
|
+
const above = getContext<{ readonly value: unknown } | undefined>(OUTLET_VALUE_KEY)
|
|
12
|
+
setContext(OUTLET_VALUE_KEY, {
|
|
13
|
+
get value() {
|
|
14
|
+
return context !== undefined ? context : above?.value
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
const slot = $derived(name === undefined ? undefined : level.components[level.index]?.outlets?.[name])
|
|
18
|
+
const read = useAppState("Outlet")
|
|
19
|
+
const route = $derived(slot?.route ? interceptedRoute(read(), slot) : undefined)
|
|
20
|
+
const isDev = (import.meta as { dev?: boolean }).dev === true
|
|
21
|
+
if (isDev && name === undefined) noteOutlet(level.id)
|
|
22
|
+
if (isDev && name !== undefined && !slot) console.warn(`Outlet name "${name}" has no slot in ${level.id}`)
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
{#if name !== undefined}
|
|
26
|
+
{#if slot?.components.length}
|
|
27
|
+
<Nest components={slot.components} {route} index={0} />
|
|
28
|
+
{/if}
|
|
29
|
+
{:else if level.index < level.components.length - 1}
|
|
30
|
+
<Nest components={level.components} index={level.index + 1} />
|
|
31
|
+
{/if}
|
package/src/Root.svelte
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import { reportClientError } from "hamur/adapter"
|
|
4
|
+
import { APP_KEY, IDS_KEY, rootIds } from "./context.ts"
|
|
5
|
+
import type { AppState } from "./context.ts"
|
|
6
|
+
import { track } from "./store.ts"
|
|
7
|
+
import type { Source } from "./store.ts"
|
|
8
|
+
import Nest from "./Nest.svelte"
|
|
9
|
+
|
|
10
|
+
let { source, idPrefix = "" }: { source: Source<AppState>; idPrefix?: string } = $props()
|
|
11
|
+
setContext(APP_KEY, source)
|
|
12
|
+
setContext(IDS_KEY, rootIds(idPrefix))
|
|
13
|
+
const read = track(source)
|
|
14
|
+
const components = $derived(read().components)
|
|
15
|
+
const tree = $derived(read().tree)
|
|
16
|
+
|
|
17
|
+
// No `failed` snippet: the server then renders no boundary and its errors still reach the error page.
|
|
18
|
+
function onerror(error: unknown, reset: () => void): void {
|
|
19
|
+
const before = source.get()
|
|
20
|
+
queueMicrotask(() => {
|
|
21
|
+
reportClientError(before.app, error, { render: true })
|
|
22
|
+
if (source.get() !== before) reset()
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<svelte:boundary {onerror}>
|
|
28
|
+
<Nest {components} top={tree} />
|
|
29
|
+
</svelte:boundary>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import {
|
|
3
|
+
ROUTE_ANNOUNCER_ID,
|
|
4
|
+
announcerRole,
|
|
5
|
+
createRouteAnnouncer,
|
|
6
|
+
hideAnnouncer,
|
|
7
|
+
type RouteAnnouncerPoliteness,
|
|
8
|
+
} from "hamur/adapter"
|
|
9
|
+
import { onMount } from "svelte"
|
|
10
|
+
import { useApp, useRouteAnnouncer } from "./index.ts"
|
|
11
|
+
|
|
12
|
+
let {
|
|
13
|
+
politeness,
|
|
14
|
+
class: className,
|
|
15
|
+
style,
|
|
16
|
+
}: { politeness?: RouteAnnouncerPoliteness; class?: string; style?: string } = $props()
|
|
17
|
+
const store = createRouteAnnouncer(useApp())
|
|
18
|
+
const announcer = useRouteAnnouncer()
|
|
19
|
+
const key = {}
|
|
20
|
+
let tick = $state(0)
|
|
21
|
+
const owner = $derived.by(() => {
|
|
22
|
+
void tick
|
|
23
|
+
return store.claim(key, () => tick++)
|
|
24
|
+
})
|
|
25
|
+
let element: HTMLElement | undefined = $state()
|
|
26
|
+
const live = $derived(politeness ?? announcer.politeness)
|
|
27
|
+
onMount(() => () => store.release(key))
|
|
28
|
+
$effect(() => {
|
|
29
|
+
if (!owner) return
|
|
30
|
+
if (!className && !style && element) hideAnnouncer(element)
|
|
31
|
+
return store.mount()
|
|
32
|
+
})
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
{#if owner}
|
|
36
|
+
<div
|
|
37
|
+
bind:this={element}
|
|
38
|
+
id={ROUTE_ANNOUNCER_ID}
|
|
39
|
+
aria-live={live}
|
|
40
|
+
aria-atomic="true"
|
|
41
|
+
role={announcerRole(live)}
|
|
42
|
+
class={className}
|
|
43
|
+
{style}>{announcer.message}</div
|
|
44
|
+
>
|
|
45
|
+
{:else}
|
|
46
|
+
<div aria-live="off" aria-atomic="true"></div>
|
|
47
|
+
{/if}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { setContext } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
5
|
+
import { ERROR_KEY } from "./context.ts"
|
|
6
|
+
|
|
7
|
+
let { error, children }: { error: ErrorPageProps; children?: Snippet } = $props()
|
|
8
|
+
setContext(ERROR_KEY, error)
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
{@render children?.()}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component } from "svelte"
|
|
3
|
+
import { serverComponentAttributes, serverComponentProps } from "hamur/adapter"
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
server,
|
|
7
|
+
children,
|
|
8
|
+
...rest
|
|
9
|
+
}: { server: { name: string; component: Component<Record<string, unknown>> }; children?: unknown } & Record<
|
|
10
|
+
string,
|
|
11
|
+
unknown
|
|
12
|
+
> = $props()
|
|
13
|
+
const uid = $props.id()
|
|
14
|
+
const name = $derived(server.name)
|
|
15
|
+
const Inner = $derived(server.component)
|
|
16
|
+
serverComponentProps(server.name, rest, children !== undefined && !server.name.startsWith("page:"))
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<hamur-component {name} {uid} {...serverComponentAttributes(server.name)}><Inner {...rest} /></hamur-component>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from "svelte"
|
|
3
|
+
import {
|
|
4
|
+
fetchServerComponent,
|
|
5
|
+
serverComponentAttributes,
|
|
6
|
+
serverComponentProps,
|
|
7
|
+
setServerHTML,
|
|
8
|
+
takeServerHTML,
|
|
9
|
+
} from "hamur/adapter"
|
|
10
|
+
import type { ServerComponentView } from "hamur/adapter"
|
|
11
|
+
import type { HeadEntry } from "hamur/app"
|
|
12
|
+
import { APP_KEY } from "./context.ts"
|
|
13
|
+
import type { AppState } from "./context.ts"
|
|
14
|
+
import type { Source } from "./store.ts"
|
|
15
|
+
|
|
16
|
+
let { server, children, ...rest }: { server: { name: string }; children?: unknown } & Record<string, unknown> =
|
|
17
|
+
$props()
|
|
18
|
+
const uid = $props.id()
|
|
19
|
+
const { name } = server
|
|
20
|
+
const app = getContext<Source<AppState>>(APP_KEY).get().app
|
|
21
|
+
const text = $derived(serverComponentProps(name, rest, children !== undefined && !name.startsWith("page:")))
|
|
22
|
+
const taken = takeServerHTML(app, uid, name)
|
|
23
|
+
let html = $state.raw<ServerComponentView["html"]>(taken ?? "")
|
|
24
|
+
let failed = $state.raw<{ error: unknown }>()
|
|
25
|
+
let requested = taken === undefined ? undefined : text
|
|
26
|
+
let applied: string | undefined
|
|
27
|
+
let head: HeadEntry | undefined
|
|
28
|
+
|
|
29
|
+
$effect(() => {
|
|
30
|
+
const current = text
|
|
31
|
+
if (current === requested) return
|
|
32
|
+
const controller = new AbortController()
|
|
33
|
+
fetchServerComponent(app, name, current, controller.signal).then(
|
|
34
|
+
(view) => {
|
|
35
|
+
if (controller.signal.aborted) return
|
|
36
|
+
head?.dispose()
|
|
37
|
+
head = view.head ? app.head.push(view.head) : undefined
|
|
38
|
+
requested = current
|
|
39
|
+
html = view.html
|
|
40
|
+
},
|
|
41
|
+
(error: unknown) => {
|
|
42
|
+
if (!controller.signal.aborted) failed = { error }
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
return () => controller.abort()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
$effect(() => {
|
|
49
|
+
if (failed) throw failed.error
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
onDestroy(() => head?.dispose())
|
|
53
|
+
|
|
54
|
+
// The element has no children of its own, so hydration leaves the server's nodes as they are; a new element has none to keep.
|
|
55
|
+
function show(node: HTMLElement): void {
|
|
56
|
+
const next = html
|
|
57
|
+
if (applied === undefined && taken !== undefined && node.hasChildNodes()) applied = String(next)
|
|
58
|
+
if (applied === String(next)) return
|
|
59
|
+
applied = String(next)
|
|
60
|
+
setServerHTML(node, next)
|
|
61
|
+
}
|
|
62
|
+
</script>
|
|
63
|
+
|
|
64
|
+
<hamur-component {name} {uid} {...serverComponentAttributes(name)} {@attach show}></hamur-component>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext } from "svelte"
|
|
3
|
+
import type { Snippet } from "svelte"
|
|
4
|
+
import { renderServerIsland, serverIslandProps } from "hamur/adapter"
|
|
5
|
+
import { APP_KEY, PENDING_ISLANDS_KEY } from "./context.ts"
|
|
6
|
+
import type { AppState } from "./context.ts"
|
|
7
|
+
import type { Source } from "./store.ts"
|
|
8
|
+
import { settledIsland } from "./island-html.ts"
|
|
9
|
+
|
|
10
|
+
let { name, props = {}, fallback }: { name: string; props?: Record<string, unknown>; fallback?: Snippet } = $props()
|
|
11
|
+
const id = $props.id()
|
|
12
|
+
const app = getContext<Source<AppState> | undefined>(APP_KEY)?.get().app
|
|
13
|
+
if (!app) throw new Error("ServerIsland must be rendered inside a Hamur app")
|
|
14
|
+
// While hydrating, the server's element keeps its attributes and Svelte hydrates the fallback it wrote, so the spread is empty.
|
|
15
|
+
const attributes =
|
|
16
|
+
typeof document === "undefined"
|
|
17
|
+
? (settledIsland(
|
|
18
|
+
renderServerIsland(app, id, name, props).attributes,
|
|
19
|
+
getContext<Promise<void>[] | undefined>(PENDING_ISLANDS_KEY),
|
|
20
|
+
) ?? {})
|
|
21
|
+
: app.asyncData.hydrating
|
|
22
|
+
? {}
|
|
23
|
+
: { name, ...(Object.keys(props).length ? { props: serverIslandProps(name, props) } : {}) }
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<hamur-server-island {...attributes}>{@render fallback?.()}</hamur-server-island>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component, Snippet } from "svelte"
|
|
3
|
+
|
|
4
|
+
let {
|
|
5
|
+
component: Island,
|
|
6
|
+
props = {},
|
|
7
|
+
slot,
|
|
8
|
+
}: { component: Component<any>; props?: Record<string, unknown>; slot: Snippet } = $props()
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<Island {...props} children={slot} />
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createError } from "hamur/app"
|
|
3
|
+
import type { ErrorPageProps } from "hamur/app"
|
|
4
|
+
|
|
5
|
+
let { error }: { error: ErrorPageProps } = $props()
|
|
6
|
+
|
|
7
|
+
// A boundary entry sits in the same svelte:boundary markup that the server writes for a render error, so hydration finds it.
|
|
8
|
+
throw createError({ status: error.status, message: error.message })
|
|
9
|
+
</script>
|