Skip to content

Commit e2af9c6

Browse files
committed
Allow passing initialData callback
1 parent da297ac commit e2af9c6

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

packages/react/src/createInertiaApp.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type BaseInertiaAppOptions = {
3535
type CreateInertiaAppSetupReturnType = ReactInstance | void
3636
type InertiaAppOptionsForCSR<SharedProps extends PageProps> = BaseInertiaAppOptions & {
3737
id?: string
38-
page?: Page | string
38+
page?: Page
3939
render?: undefined
40+
initialData?: (el: HTMLElement) => Page
4041
progress?:
4142
| false
4243
| {
@@ -51,8 +52,9 @@ type InertiaAppOptionsForCSR<SharedProps extends PageProps> = BaseInertiaAppOpti
5152
type CreateInertiaAppSSRContent = { head: string[]; body: string }
5253
type InertiaAppOptionsForSSR<SharedProps extends PageProps> = BaseInertiaAppOptions & {
5354
id?: undefined
54-
page: Page | string
55+
page: Page
5556
render: typeof renderToString
57+
initialData?: undefined
5658
progress?: undefined
5759
setup(options: SetupOptions<null, SharedProps>): ReactInstance
5860
}
@@ -68,6 +70,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
6870
resolve,
6971
setup,
7072
title,
73+
initialData = (el) => JSON.parse(el.dataset.page),
7174
progress = {},
7275
page,
7376
render,
@@ -76,7 +79,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
7679
> {
7780
const isServer = typeof window === 'undefined'
7881
const el = isServer ? null : document.getElementById(id)
79-
const initialPage = page || JSON.parse(el.dataset.page)
82+
const initialPage = page || initialData(el)
8083
// @ts-expect-error
8184
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
8285

@@ -88,6 +91,7 @@ export default async function createInertiaApp<SharedProps extends PageProps = P
8891
el,
8992
App,
9093
props: {
94+
// @ts-expect-error
9195
initialPage,
9296
initialComponent,
9397
resolveComponent,

packages/svelte/src/lib/createInertiaApp.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface CreateInertiaAppProps {
1717
resolveComponent: ComponentResolver
1818
}
1919
}) => void | SvelteApp
20+
initialData?: (el: HTMLElement | null) => Page
2021
progress?:
2122
| false
2223
| {
@@ -32,12 +33,13 @@ export default async function createInertiaApp({
3233
id = 'app',
3334
resolve,
3435
setup,
36+
initialData = (el) => JSON.parse(el?.dataset.page ?? '{}'),
3537
progress = {},
3638
page,
3739
}: CreateInertiaAppProps): InertiaAppResponse {
3840
const isServer = typeof window === 'undefined'
3941
const el = isServer ? null : document.getElementById(id)
40-
const initialPage = page || JSON.parse(el?.dataset.page ?? '{}')
42+
const initialPage = page || initialData(el)
4143
const resolveComponent = (name: string) => Promise.resolve(resolve(name))
4244

4345
await resolveComponent(initialPage.component).then((initialComponent) => {

packages/vue2/src/createInertiaApp.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface CreateInertiaAppProps {
1515
plugin: PluginObject<any>
1616
}) => void | Vue
1717
title?: (title: string) => string
18+
initialData?: (el: HTMLElement) => Page
1819
progress?:
1920
| false
2021
| {
@@ -32,13 +33,14 @@ export default async function createInertiaApp({
3233
resolve,
3334
setup,
3435
title,
36+
initialData = (el) => JSON.parse(el.dataset.page),
3537
progress = {},
3638
page,
3739
render,
3840
}: CreateInertiaAppProps): Promise<{ head: string[]; body: string } | void> {
3941
const isServer = typeof window === 'undefined'
4042
const el = isServer ? null : document.getElementById(id)
41-
const initialPage = page || JSON.parse(el.dataset.page)
43+
const initialPage = page || initialData(el)
4244
// @ts-expect-error
4345
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
4446

packages/vue3/src/createInertiaApp.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface CreateInertiaAppProps {
77
resolve: (name: string) => DefineComponent | Promise<DefineComponent> | { default: DefineComponent }
88
setup: (props: { el: Element; App: InertiaApp; props: InertiaAppProps; plugin: Plugin }) => void | VueApp
99
title?: (title: string) => string
10+
initialData?: (el: HTMLElement) => Page
1011
progress?:
1112
| false
1213
| {
@@ -24,13 +25,14 @@ export default async function createInertiaApp({
2425
resolve,
2526
setup,
2627
title,
28+
initialData = (el) => JSON.parse(el.dataset.page),
2729
progress = {},
2830
page,
2931
render,
3032
}: CreateInertiaAppProps): Promise<{ head: string[]; body: string }> {
3133
const isServer = typeof window === 'undefined'
3234
const el = isServer ? null : document.getElementById(id)
33-
const initialPage = page || JSON.parse(el.dataset.page)
35+
const initialPage = page || initialData(el)
3436
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
3537

3638
let head = []

0 commit comments

Comments
 (0)