Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const config = new Config<InertiaAppConfig>({
preserveEqualProps: false,
useDataInertiaHeadAttribute: false,
useDialogForErrorModal: false,
useScriptElementForInitialPage: false,
},
prefetch: {
cacheFor: 30_000,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ export type InertiaAppConfig = {
preserveEqualProps: boolean
useDataInertiaHeadAttribute: boolean
useDialogForErrorModal: boolean
useScriptElementForInitialPage: boolean
}
prefetch: {
cacheFor: CacheForOption | CacheForOption[]
Expand Down
41 changes: 29 additions & 12 deletions packages/react/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
router,
setupProgress,
} from '@inertiajs/core'
import { ReactElement, createElement } from 'react'
import { Fragment, ReactElement, createElement } from 'react'
import { renderToString } from 'react-dom/server'
import App, { InertiaAppProps, type InertiaApp } from './App'
import { config } from './index'
Expand Down Expand Up @@ -61,8 +61,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
config.replace(defaults)

const isServer = typeof window === 'undefined'
const useScriptElementForInitialPage = config.get('future.useScriptElementForInitialPage')
const el = isServer ? null : document.getElementById(id)
const initialPage = page || JSON.parse(el?.dataset.page || '{}')
const elPage = isServer || !useScriptElementForInitialPage ? null : document.getElementById(id + '_page')
const initialPage = page || JSON.parse(el?.dataset.page || elPage?.textContent || '{}')

// @ts-expect-error - This can be improved once we remove the 'unknown' type from the resolver...
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
Expand Down Expand Up @@ -104,16 +106,31 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
}

if (isServer && render) {
const body = await render(
createElement(
'div',
{
id,
'data-page': JSON.stringify(initialPage),
},
reactApp as ReactElement,
),
)
const element = () => {
if (!useScriptElementForInitialPage) {
return createElement(
'div',
{
id,
'data-page': JSON.stringify(initialPage),
},
reactApp as ReactElement,
)
}

return createElement(
Fragment,
null,
createElement('script', {
id: id + '_page',
type: 'application/json',
dangerouslySetInnerHTML: { __html: JSON.stringify(initialPage) },
}),
createElement('div', { id }, reactApp as ReactElement),
)
}

const body = await render(element())

return { head, body }
}
Expand Down
8 changes: 6 additions & 2 deletions packages/svelte/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
config.replace(defaults)

const isServer = typeof window === 'undefined'
const useScriptElementForInitialPage = config.get('future.useScriptElementForInitialPage')
const el = isServer ? null : document.getElementById(id)
const initialPage = page || JSON.parse(el?.dataset.page || '{}')
const elPage = isServer || !useScriptElementForInitialPage ? null : document.getElementById(id + '_page')
const initialPage = page || JSON.parse(elPage?.textContent || el?.dataset.page || '{}')

const resolveComponent = (name: string) => Promise.resolve(resolve(name))

Expand All @@ -59,7 +61,9 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
const { html, head, css } = svelteApp

return {
body: `<div data-server-rendered="true" id="${id}" data-page="${escape(JSON.stringify(initialPage))}">${html}</div>`,
body: useScriptElementForInitialPage
? `<script type="application/json" id="${id}_page">${JSON.stringify(initialPage)}</script><div data-server-rendered="true" id="${id}">${html}</div>`
: `<div data-server-rendered="true" id="${id}" data-page="${escape(JSON.stringify(initialPage))}">${html}</div>`,
head: [head, css ? `<style data-vite-css>${css.code}</style>` : ''],
}
}
Expand Down
33 changes: 26 additions & 7 deletions packages/vue3/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
config.replace(defaults)

const isServer = typeof window === 'undefined'
const useScriptElementForInitialPage = config.get('future.useScriptElementForInitialPage')
const el = isServer ? null : document.getElementById(id)
const initialPage = page || JSON.parse(el?.dataset.page || '{}')
const elPage = isServer || !useScriptElementForInitialPage ? null : document.getElementById(id + '_page')
const initialPage = page || JSON.parse(elPage?.textContent || el?.dataset.page || '{}')

const resolveComponent = (name: string) => Promise.resolve(resolve(name)).then((module) => module.default || module)

Expand Down Expand Up @@ -103,14 +105,31 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
}

if (isServer && render) {
const element = () => {
if (!useScriptElementForInitialPage) {
return h('div', {
id,
'data-page': JSON.stringify(initialPage),
innerHTML: vueApp ? render(vueApp) : '',
})
}

return [
h('script', {
id: id + '_page',
type: 'application/json',
innerHTML: JSON.stringify(initialPage),
}),
h('div', {
id,
innerHTML: vueApp ? render(vueApp) : '',
}),
]
}

const body = await render(
createSSRApp({
render: () =>
h('div', {
id,
'data-page': JSON.stringify(initialPage),
innerHTML: vueApp ? render(vueApp) : '',
}),
render: () => element(),
}),
)

Expand Down