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 )
@@ -80,11 +85,20 @@ export default async function createInertiaApp({
80
85
return
81
86
}
82
87
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>` ] ,
88
+ if ( isServer ) {
89
+ if ( ! ssr ) {
90
+ throw new Error ( `createInertiaApp must provide ssr(...) for server-side rendering.` )
91
+ }
92
+
93
+ const { html, head, css } = ssr ( SSR , { id, initialPage } )
94
+
95
+ return {
96
+ body : html ,
97
+ head : [
98
+ head ,
99
+ // Note: Svelte 5 no longer output CSS
100
+ ...( css ?. code ? [ `<style data-vite-css>${ css ?. code } </style>` ] : [ ] ) ,
101
+ ] ,
102
+ }
89
103
}
90
104
}
0 commit comments