xyflow: v11 --> v12 breaks edge position and placement when loading images

What platform were you using when you found the bug?

  System:
    OS: Linux 6.6 Manjaro Linux
  Binaries:
    Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v18.19.0/bin/npm
    pnpm: 9.6.0 - ~/.nvm/versions/node/v18.19.0/bin/pnpm
  Browsers:
    Chromium: 127.0.6533.88
  npmPackages:
    @xyflow/react: ^12.0.4 => 12.0.4 
    next: ^14.2.3 => 14.2.3 
    react: ^18.3.1 => 18.3.1 
    typescript: 5.5.4 => 5.5.4 

Live code example

No response

Describe the Bug

I just updated my repo to the latest version of RF and I noticed that the behavior of the edge placement and updating has changed. Specifically when loading dynamic content such as videos and images, the edges used to update automatically. This also applied when dealing with dynamic CSS modifications (e.g changing the height of something in the node).

I deal with unknown content, so there’s no way to prevent layout shifts. Also the scope and the number of images, make it unfeasible to constantly track image loading state and then call useUpdateNodeInternals.

V11

Position is correct, after content is loaded edges are updated. Screenshot_2024-08-07_17-10

V12

Position is incorrect, after content is loaded edges remain where they were mounted initially. Screenshot_2024-08-07_17-09

I’m wondering what has changed specifically that has caused this issue? Is this is design choice on your behalf and expected behavior?

Steps to reproduce the bug or issue

This can be tried using the custom node example with a large image

Expected behavior

I expect the edges to automatically shift to correct position.

Screenshots or Videos

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 39 (15 by maintainers)

Most upvoted comments

We will investigate.

@alexanderhorner I actually have no idea! I just noticed it as I was testing the performance of my onNodeChanges method. But if I were to guess, I’d say by using current you’re essentially bypassing all the proxy logic. Without currentc the function below was hitting 0.8ms while using current brought it down to 0.3ms.

To be clear, the performance gain can also be specific to my implementation. My nodes and edges are stored as key/value objects instead of the default array[] format that is recommended by RF. So I have to do some logic here and there for updates/sets, but the key/val setup makes it much easier to access and modify individual nodes outside of react components where I don’t have access to hooks and without incurring a O(n) search penalty.

this is my actual function

onNodesChange: (changes: NodeChange[]) => {
  set((state) => {
    const { nodes } = current(state);

    const changedNodes = applyNodeChanges(
      changes,
      Object.values(nodes) as Node[],
    ) as AnyFuserNode[];

    for (const change of changes) {
      if (change.type === 'remove') {
        delete state.nodes[change.id];
      }

      if (
        change.type === 'select' &&
        !change.selected &&
        state.lastAddedNode === change.id
      ) {
        state.lastAddedNode = undefined;
      }
    }

    for (const node of changedNodes) {
      state.nodes[node.id] = castDraft(node);
    }
    state.lastUpdate = Date.now();
  });
},

@alexanderhorner for your immer issues, I think you can use current or the original methods from immer. Performance is much faster than structuredClone and actually even 2x faster than const { nodes } = state additionally I highly recommend disabling autofreeze via setAutoFreeze(false)

onNodesChange: (changes: NodeChange[]) => {
  set((state) => {
    const { nodes } = current(state);
    state.nodes = applyNodeChanges(changes, nodes);
  });
}

@moklick I still encounter strange issues with initialWidth/Height. This happens specifically when I throttle my connection using chrome’s dev tools to Slow 3G, and the edges fail to be positioned properly. On speedy connections everything works as expected.

Also opening this up again for @alexanderhorner.

measured is not something that is set by the user but whenever the resize observer detects a change and on first render.

I think I found the issue while creating the reproduction repo. The violating node attributes are width and height. If these are present dynamic positioning breaks. Rereading the docs I can see that Using width and height now means that the dimensions are not dynamic based on the content but fixed. smh