Skip to content

mutateDom stops observing future mutations when its callback throws #4907

Closed

Description

@BillyNoyes

Alpine.js version

v3.17.3

Browser and operating system

Chrome on macOS (the issue is browser-independent)

Describe the issue you're experiencing

Alpine.mutateDom() stops the global mutation observer before invoking its callback and restarts it only after the callback returns. If that callback throws synchronously, control skips the restart call and Alpine remains permanently disconnected from DOM mutations.

After the error is caught, dynamically inserted Alpine components are therefore never initialized. This can also amplify unrelated directive errors: one exception from code running inside mutateDom() prevents Alpine from observing all subsequent DOM changes.

The current implementation in packages/alpinejs/src/mutation.js is effectively:

stopObservingMutations()

let result = callback()

startObservingMutations()

Because observation is global state, it should be restored even when the callback fails.

Code snippets to reproduce the issue

<div id="target"></div>

<script type="module">
    import Alpine from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'

    window.Alpine = Alpine
    Alpine.start()

    try {
        Alpine.mutateDom(() => {
            throw new Error('expected test error')
        })
    } catch (_) {}

    document.querySelector('#target').innerHTML = `
        <div x-data="{ message: 'initialized' }">
            <span id="result" x-text="message"></span>
        </div>
    `

    setTimeout(() => {
        console.log(document.querySelector('#result').textContent)
        // Actual: ""
        // Expected: "initialized"
    })
</script>

Steps:

  1. Start Alpine.
  2. Call Alpine.mutateDom() with a callback that throws, and catch the expected error.
  3. Insert a new x-data component into the document.
  4. Observe that its directives are never initialized.

Screenshots/screen recordings

Not applicable.

How do you expect it to work?

mutateDom() should preserve the callback's existing return and error behavior while always restoring mutation observation. A try/finally around the callback would ensure that one failed DOM mutation does not disable Alpine's handling of future DOM changes.

Please confirm (incomplete submissions will not be addressed)

  • I have provided easy and step-by-step instructions to reproduce the bug.
  • I have provided code samples as text and NOT images.
  • I understand my bug report will be removed if I haven't met the criteria above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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