11import { router , setupProgress , type InertiaAppResponse , type Page } from '@inertiajs/core'
22import escape from 'html-escape'
33import type { ComponentType } from 'svelte'
4- import { version as SVELTE_VERSION } from 'svelte/package.json'
5- import App from './components/App.svelte'
6- import store from './store'
7- import type { ComponentResolver , ResolvedComponent } from './types'
4+ import App , { type InertiaAppProps } from './components/App.svelte'
5+ import type { ComponentResolver } from './types'
86
97type SvelteRenderResult = { html : string ; head : string ; css ?: { code : string } }
10- type AppComponent = ComponentType < App > & { render : ( ) => SvelteRenderResult }
8+ type AppComponent = ComponentType < App > & { render : ( props : InertiaAppProps ) => SvelteRenderResult }
119
1210interface CreateInertiaAppProps {
1311 id ?: string
1412 resolve : ComponentResolver
15- setup ?: ( props : {
16- el : HTMLElement
17- App : ComponentType < App >
18- props : {
19- initialPage : Page
20- resolveComponent : ComponentResolver
21- }
22- } ) => void | App
13+ setup : ( props : {
14+ el : HTMLElement | null
15+ App : AppComponent
16+ props : InertiaAppProps
17+ } ) => void | App | SvelteRenderResult
2318 progress ?:
2419 | false
2520 | {
@@ -40,75 +35,32 @@ export default async function createInertiaApp({
4035} : CreateInertiaAppProps ) : InertiaAppResponse {
4136 const isServer = typeof window === 'undefined'
4237 const el = isServer ? null : document . getElementById ( id )
43- const initialPage : Page = page || JSON . parse ( el ?. dataset ? .page || '{}' )
38+ const initialPage : Page = page || JSON . parse ( el ?. dataset . page || '{}' )
4439 const resolveComponent = ( name : string ) => Promise . resolve ( resolve ( name ) )
4540
46- await Promise . all ( [ resolveComponent ( initialPage . component ) , router . decryptHistory ( ) . catch ( ( ) => { } ) ] ) . then (
47- ( [ initialComponent ] ) => {
48- store . set ( {
49- component : initialComponent ,
50- page : initialPage ,
51- key : null ,
52- } )
53- } ,
54- )
41+ const [ initialComponent ] = await Promise . all ( [
42+ resolveComponent ( initialPage . component ) ,
43+ router . decryptHistory ( ) . catch ( ( ) => { } ) ,
44+ ] )
5545
56- if ( isServer ) {
57- const isSvelte5 = SVELTE_VERSION . startsWith ( '5' )
58- const { html, head, css } = await ( async ( ) => {
59- if ( isSvelte5 ) {
60- const { render } = await dynamicImport ( 'svelte/server' )
61- if ( typeof render === 'function' ) {
62- return render ( App ) as SvelteRenderResult
63- }
64- }
46+ const props : InertiaAppProps = { initialPage, initialComponent, resolveComponent }
6547
66- return ( App as AppComponent ) . render ( )
67- } ) ( )
48+ const svelteApp = setup ( {
49+ el,
50+ App : App as unknown as AppComponent ,
51+ props
52+ } )
53+
54+ if ( isServer ) {
55+ const { html, head, css } = svelteApp as SvelteRenderResult
6856
6957 return {
7058 body : `<div data-server-rendered="true" id="${ id } " data-page="${ escape ( JSON . stringify ( initialPage ) ) } ">${ html } </div>` ,
7159 head : [ head , css ? `<style data-vite-css>${ css . code } </style>` : '' ] ,
7260 }
7361 }
7462
75- if ( ! el ) {
76- throw new Error ( `Element with ID "${ id } " not found.` )
77- }
78-
79- router . init ( {
80- initialPage,
81- resolveComponent,
82- swapComponent : async ( { component, page, preserveState } ) => {
83- store . update ( ( current ) => ( {
84- component : component as ResolvedComponent ,
85- page,
86- key : preserveState ? current . key : Date . now ( ) ,
87- } ) )
88- } ,
89- } )
90-
9163 if ( progress ) {
9264 setupProgress ( progress )
9365 }
94-
95- setup ( {
96- el,
97- App,
98- props : {
99- initialPage,
100- resolveComponent,
101- } ,
102- } )
103- }
104-
105- // Loads the module dynamically during execution instead of at
106- // build time. The `@vite-ignore` flag prevents Vite from
107- // analyzing or pre-bundling this import.
108- async function dynamicImport ( module : string ) {
109- try {
110- return await import ( /* @vite -ignore */ module )
111- } catch {
112- return null
113- }
11466}
0 commit comments