Skip to content

Ability to configure defaults in createInertiaApp() #2419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
45 changes: 38 additions & 7 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mergeWith } from 'es-toolkit'
import { hideProgress, revealProgress } from '.'
import { eventHandler } from './eventHandler'
import { fireBeforeEvent } from './events'
Expand Down Expand Up @@ -44,6 +45,12 @@ export class Router {
interruptible: false,
})

protected defaultVisitOptionsCallback: ((href: string | URL, options: Visit) => Partial<Visit>) | null = null

public withDefaultVisitOptions(cb: (href: string | URL, options: Visit) => Partial<Visit>): void {
this.defaultVisitOptionsCallback = cb
}

public init({ initialPage, resolveComponent, swapComponent }: RouterInitParams): void {
currentPage.init({
initialPage,
Expand Down Expand Up @@ -323,12 +330,8 @@ export class Router {
}
}

protected getPendingVisit(
href: string | URL,
options: VisitOptions,
pendingVisitOptions: Partial<PendingVisitOptions> = {},
): PendingVisit {
const mergedOptions: Visit = {
protected mergeVisitOptions(href: string | URL, mergeOptions: Partial<Visit> = {}): Visit {
const defaultOptions: Visit = {
method: 'get',
data: {},
replace: false,
Expand All @@ -346,9 +349,37 @@ export class Router {
reset: [],
preserveUrl: false,
prefetch: false,
...options,
}

if (!this.defaultVisitOptionsCallback) {
return { ...defaultOptions, ...mergeOptions }
}

const options = {
...defaultOptions,
...this.defaultVisitOptionsCallback(href, defaultOptions),
}

return mergeWith(options, mergeOptions, (originalValue, mergeValue) => {
if (Array.isArray(mergeValue) && Array.isArray(originalValue)) {
return [...originalValue, ...mergeValue]
}

if (typeof mergeValue === 'object' && typeof originalValue === 'object') {
return { ...originalValue, ...mergeValue }
}

return mergeValue
})
}

protected getPendingVisit(
href: string | URL,
options: VisitOptions,
pendingVisitOptions: Partial<PendingVisitOptions> = {},
): PendingVisit {
const mergedOptions: Visit = this.mergeVisitOptions(href, options)

const [url, _data] = transformUrlAndData(
href,
mergedOptions.data,
Expand Down
18 changes: 16 additions & 2 deletions packages/vue3/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHeadManager, Page, PageProps, router } from '@inertiajs/core'
import { createHeadManager, Page, PageProps, router, Visit } from '@inertiajs/core'
import {
computed,
DefineComponent,
Expand All @@ -21,6 +21,9 @@ export interface InertiaAppProps {
resolveComponent?: (name: string) => DefineComponent | Promise<DefineComponent>
titleCallback?: (title: string) => string
onHeadUpdate?: (elements: string[]) => void
defaults?: {
visitOptions?: (href: string | URL, options: Visit) => Partial<Visit>
}
}

export type InertiaApp = DefineComponent<InertiaAppProps>
Expand Down Expand Up @@ -56,8 +59,15 @@ const App: InertiaApp = defineComponent({
required: false,
default: () => () => {},
},
defaults: {
type: Object as PropType<{
visitOptions?: (href: string | URL, options: Visit) => Partial<Visit>
}>,
required: false,
default: () => ({}),
},
},
setup({ initialPage, initialComponent, resolveComponent, titleCallback, onHeadUpdate }) {
setup({ initialPage, initialComponent, resolveComponent, titleCallback, onHeadUpdate, defaults }) {
component.value = initialComponent ? markRaw(initialComponent) : null
page.value = initialPage
key.value = null
Expand All @@ -76,6 +86,10 @@ const App: InertiaApp = defineComponent({
},
})

if (defaults?.visitOptions) {
router.withDefaultVisitOptions(defaults.visitOptions)
}

router.on('navigate', () => headManager.forceUpdate())
}

Expand Down
7 changes: 6 additions & 1 deletion packages/vue3/src/createInertiaApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page, router, setupProgress } from '@inertiajs/core'
import { Page, router, setupProgress, Visit } from '@inertiajs/core'
import { DefineComponent, Plugin, App as VueApp, createSSRApp, h } from 'vue'
import App, { InertiaApp, InertiaAppProps, plugin } from './app'

Expand All @@ -17,6 +17,9 @@ interface CreateInertiaAppProps {
}
page?: Page
render?: (app: VueApp) => Promise<string>
defaults?: {
visitOptions?: (href: string | URL, options: Visit) => Partial<Visit>
}
}

export default async function createInertiaApp({
Expand All @@ -27,6 +30,7 @@ export default async function createInertiaApp({
progress = {},
page,
render,
defaults,
}: CreateInertiaAppProps): Promise<{ head: string[]; body: string }> {
const isServer = typeof window === 'undefined'
const el = isServer ? null : document.getElementById(id)
Expand All @@ -48,6 +52,7 @@ export default async function createInertiaApp({
resolveComponent,
titleCallback: title,
onHeadUpdate: isServer ? (elements) => (head = elements) : null,
defaults,
},
plugin,
})
Expand Down
10 changes: 10 additions & 0 deletions playgrounds/vue3/resources/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ createInertiaApp({
.use(plugin)
.mount(el)
},
defaults: {
visitOptions: (href, options) => {
return {
// queryStringArrayFormat: 'indices',
// headers: {
// 'X-Foo': 'bar',
// },
}
},
},
})