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