-
Notifications
You must be signed in to change notification settings - Fork 3
/
createInertiaApp.ts
92 lines (78 loc) · 2.51 KB
/
createInertiaApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { type Page, router, setupProgress, type InertiaAppResponse } from '@inertiajs/core'
import type { ComponentResolver, InertiaComponentType } from './types'
import type { RenderOutput } from 'svelte/server'
import SvelteApp from './components/App.svelte'
import SSR from './components/SSR.svelte'
import store from './store'
import type { ComponentType } from 'svelte'
interface CreateInertiaAppProps<T = RenderOutput | SvelteApp | void> {
id?: string
resolve: ComponentResolver
setup: (props: {
el: Element
App: ComponentType
props?: {
id: string
initialPage: Page
}
}) => T
progress?:
| false
| {
delay?: number
color?: string
includeCSS?: boolean
showSpinner?: boolean
}
page?: Page
}
async function makeStore(resolve: ComponentResolver, initialPage: Page) {
const resolveComponent = (name: string) => Promise.resolve(resolve(name))
await resolveComponent(initialPage.component).then((initialComponent) => {
store.set({
component: initialComponent as unknown as InertiaComponentType,
page: initialPage,
})
})
return resolveComponent
}
async function createSpaApp({ id = 'app', resolve, progress, setup }: CreateInertiaAppProps<void | SvelteApp>): InertiaAppResponse {
const el = document.getElementById(id)
const initialPage = JSON.parse(el?.dataset.page ?? '{}')
if (!el) {
throw new Error(`Element with ID "${id}" not found.`)
}
router.init({
initialPage,
resolveComponent: await makeStore(resolve, initialPage),
swapComponent: async ({ component, page, preserveState }) => {
store.update((current) => ({
component: component as InertiaComponentType,
page,
key: preserveState ? current.key : Date.now(),
}))
},
})
if (progress) {
setupProgress(progress)
}
// @ts-ignore
setup({ el, App: SvelteApp })
}
async function createSsrApp({ id = 'app', page, setup, resolve }: CreateInertiaAppProps<RenderOutput>): InertiaAppResponse {
if (!page) {
throw new Error('Page is required for SSR')
}
await makeStore(resolve, page)
// @ts-ignore
const {html, head} = setup({ App: SSR, props: {id, initialPage: page} })
return {
body: html,
head: [head],
}
}
export default async function createInertiaApp(props: CreateInertiaAppProps): InertiaAppResponse {
return typeof window === 'undefined' ?
await createSsrApp(props as CreateInertiaAppProps<RenderOutput>) :
await createSpaApp(props as CreateInertiaAppProps<void | SvelteApp>)
}