Skip to content

There is no way to react to finished client visits (replace=true) #2325

Open
@Fuzzyma

Description

@Fuzzyma

Versions:

  • @inertiajs/core version: 2.0.4
  • @inertiajs/vue2 version: #.#.#
  • @inertiajs/vue3 version: 2.0.4
  • @inertiajs/react version: #.#.#
  • @inertiajs/svelte version: #.#.#

Describe the problem:

There is currently no way to know, when a client visit is done (with replace: true)
It uses promises internally but those are not exposed. Neither an event is fired.

  • popstate only fires on navigation (not when replaceState or pushState is used).
  • insertia:finish only fires when doing server visits
  • inertia:navigate only fires when replace: false

Steps to reproduce:

Hard to come up with a reproduction that shows that nothing happens :D

document.addEventListener('inertia:finish', console.log)
document.addEventListener('inertia:navigate', console.log)
window.addEventListener('popstate', console.log)

router.replace({
  preserveScroll: true,
  preserveState: true,
  props: {
    foo: 5
  }
})

Possible solutions

Imo, the best solution by far would be to just return the promises by returning it here:

protected clientVisit(params: ClientSideVisitOptions, { replace = false }: { replace?: boolean } = {}): void {
const current = currentPage.get()
const props = typeof params.props === 'function' ? params.props(current.props) : (params.props ?? current.props)
currentPage.set(
{
...current,
...params,
props,
},
{
replace,
preserveScroll: params.preserveScroll,
preserveState: params.preserveState,
},
)
}

and here:

public replace(params: ClientSideVisitOptions): void {
this.clientVisit(params, { replace: true })
}
public push(params: ClientSideVisitOptions): void {
this.clientVisit(params)
}

Another solution could be adding yet another event. However, that is a bit weird to use when you only want to react to one specific router visit. Hence I think that exposing the promises is the superior solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions