1
1
import { router , setupProgress , type InertiaAppResponse , type Page } from '@inertiajs/core'
2
2
import type { ComponentType } from 'svelte'
3
3
import SvelteApp from './components/App.svelte'
4
- import SSR from './components/SSR.svelte'
4
+ import SSR , { type SSRProps } from './components/SSR.svelte'
5
5
import store from './store'
6
6
import type { ComponentResolver , InertiaComponentType } from './types'
7
7
8
+ type SvelteRenderResult = { html : string ; head : string ; css ?: { code : string } }
9
+ type SSRComponent = ComponentType < SSR > & { render ?: ( props : SSRProps ) => SvelteRenderResult }
10
+
8
11
interface CreateInertiaAppProps {
9
12
id ?: string
10
13
resolve : ComponentResolver
@@ -26,6 +29,7 @@ interface CreateInertiaAppProps {
26
29
showSpinner ?: boolean
27
30
}
28
31
page ?: Page
32
+ ssr ?: ( AppSSR : SSRComponent , props : SSRProps ) => SvelteRenderResult
29
33
}
30
34
31
35
export default async function createInertiaApp ( {
@@ -34,6 +38,7 @@ export default async function createInertiaApp({
34
38
setup,
35
39
progress = { } ,
36
40
page,
41
+ ssr,
37
42
} : CreateInertiaAppProps ) : InertiaAppResponse {
38
43
const isServer = typeof window === 'undefined'
39
44
const el = isServer ? null : document . getElementById ( id )
@@ -76,15 +81,22 @@ export default async function createInertiaApp({
76
81
resolveComponent,
77
82
} ,
78
83
} )
79
-
80
- return
81
84
}
82
85
83
- // Svelte types are written for the DOM API and not the SSR API.
84
- const { html, head, css } = ( SSR as any ) . render ( { id, initialPage } )
85
-
86
- return {
87
- body : html ,
88
- head : [ head , `<style data-vite-css>${ css . code } </style>` ] ,
86
+ if ( isServer ) {
87
+ if ( ! ssr ) {
88
+ throw new Error ( `createInertiaApp must provide ssr(...) for server-side rendering.` )
89
+ }
90
+
91
+ const { html, head, css } = ssr ( SSR , { id, initialPage } )
92
+
93
+ return {
94
+ body : html ,
95
+ head : [
96
+ head ,
97
+ // Note: Svelte 5 no longer output CSS
98
+ ...( css ?. code ? [ `<style data-vite-css>${ css ?. code } </style>` ] : [ ] ) ,
99
+ ] ,
100
+ }
89
101
}
90
102
}
0 commit comments