-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
createInertiaApp.js
45 lines (39 loc) · 1.14 KB
/
createInertiaApp.js
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
import { setupProgress } from '@inertiajs/core'
import { createElement } from 'react'
import App from './App'
export default async function createInertiaApp({ id = 'app', resolve, setup, title, progress = {}, page, render }) {
const isServer = typeof window === 'undefined'
const el = isServer ? null : document.getElementById(id)
const initialPage = page || JSON.parse(el.dataset.page)
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
let head = []
const reactApp = await resolveComponent(initialPage.component).then((initialComponent) => {
return setup({
el,
App,
props: {
initialPage,
initialComponent,
resolveComponent,
titleCallback: title,
onHeadUpdate: isServer ? (elements) => (head = elements) : null,
},
})
})
if (!isServer && progress) {
setupProgress(progress)
}
if (isServer) {
const body = await render(
createElement(
'div',
{
id,
'data-page': JSON.stringify(initialPage),
},
reactApp,
),
)
return { head, body }
}
}