Skip to content

Document that onDocumentPatch must call start in order - #4462

Open
rubas wants to merge 3 commits into
phoenixframework:mainfrom
rubas:fix-deferred-document-patch-order
Open

rubas wants to merge 3 commits into
phoenixframework:mainfrom
rubas:fix-deferred-document-patch-order

Conversation

@rubas

@rubas rubas commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

LiveView does not reorder the start callbacks it passes to onDocumentPatch. When an app delays only some patches, for example to run a view transition only when the server asks for one, a later patch can run before an earlier one. The client then crashes in Rendered.dynamicsToBuffer with Cannot read properties of undefined (reading 'length').

As discussed in #4462 (comment), keeping the order is the app's job. This PR drops the code change and adds the rule to the onDocumentPatch docs.

An onDocumentPatch callback that defers start, for example in a view
transition, could let a later patch run first. The later diff was then
merged into a tree that lacked the earlier one and rendering failed.
Patches now run in the order they were requested, whatever the order
of the start calls.
@SteffenDE

Copy link
Copy Markdown
Member

I would argue that it is the app's responsibility to keep patches in order, so you only call start if your pending transition is done. Something like this:

let transition = false;
let previousTransition = Promise.resolve();

window.addEventListener("phx:start-view-transition", () => (transition = true));

let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket, {
  dom: {
    onDocumentPatch(start) {
      // Capture the flag now, so it applies to this patch even if we wait
      const animate = transition;
      transition = false;

      previousTransition = previousTransition
        .then(() => {
          if (animate && document.startViewTransition) {
            return document.startViewTransition(start).finished;
          }
          start();
        })
        .catch((err) => console.error(err)); // keep the chain alive if a transition fails
    }
  }
});

We can adjust the docs to state that order has to be preserved manually.

@rubas rubas changed the title Fix crash when onDocumentPatch runs start() callbacks out of order Document that onDocumentPatch must call start in order Oct 6, 2026
@rubas

rubas commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks. I changed the PR to a short docs note on onDocumentPatch, but also feel free to close it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants