Skip to content

"A request already contains one of the messages in this array" error when using closeModalWithEvents with Livewire 4 #548

@Epitaffio

Description

@Epitaffio

Environment
• wire-elements/modal version: 3.0
• Livewire version: 4.0
• Laravel version: 12.x
• PHP version: 8.4

When using nested modals with event dispatching via closeModalWithEvents(), an uncaught JavaScript error occurs in Livewire 4:

Uncaught Error: A request already contains one of the messages in this array
    at compileRequest (livewire.js:5200)
    at coordinateNetworkInteractions (livewire.js:4260)
    at sendMessages (livewire.js:5196)
    ...

This worked correctly in Livewire 3 but breaks in Livewire 4 due to -at least that's what Claude figured out- the new request partitioning system.

To reproduce:

// ParentModal.php
public function openChildModal(): void
{
    $this->dispatch('openModal', 'child-modal']);
}

#[On('refreshData')]
public function refreshData(): void
{
    // Refresh logic
}
// ChildModal.php
public function save(): void
{
    // Save logic...
    $this->closeModalWithEvents([
        ['refreshData', []],
    ]);
}

When the child modal closes, the error is thrown.

From the Claude analysis:

The issue is in ModalComponent::closeModalWithEvents():

public function closeModalWithEvents(array $events): void
{
    $this->emitModalEvents($events);  // Dispatches user events (e.g., 'refreshData')
    $this->closeModal();               // Dispatches 'closeModal' event
}

Both emitModalEvents() and closeModal() call $this->dispatch() in the same PHP request.
In Livewire 4, the new coordinateNetworkInteractions() function (in js/request/interactions.js) uses partition interceptors to bundle related messages. When the same message appears in multiple request bundles, it throws the error at line ~10143:

if (Array.from(requests).some((request) => 
    Array.from(request.messages).some((message) => messages.includes(message)))) {
    throw new Error("A request already contains one of the messages in this array");
}

For a workaround it suggest to use $this->js() with the browser requestAnimationFrame but I don't like the idea of it (don't know why, I just don't like the idea).

Any thought or more stable fix?

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