puck icon indicating copy to clipboard operation
puck copied to clipboard

Eliminate re-rendering of components when hovered in the canvas

Open FedericoBonel opened this issue 4 weeks ago • 1 comments

Closes #1442

Description

This PR fixes a performance issue where components re-rendered when hovered over in the canvas.

Changes made

  • The renderPreview function in DropZoneChild was moved to its own component to simplify the code and avoid remounting and unnecessary re-renders.
  • The DropZoneChild component now uses MemoizeComponent to render user components. This component accepts two props: the component to render, and the props to pass to it. It memoizes the rendering by only re-rendering when the props change, using shallow comparison for general props and deep comparison for the puck prop. This approach was chosen because user props can be large and deeply nested, making deep equality checks costly. For solving the hover-related re-renders, this level of optimization was enough.
  • A new shallowEqual utility was introduced to check for shallow equality between two objects.

How to test

  1. Render the Puck editor with a config that uses slots:
const config = {
  components: {
    Slot: {
      fields: { items: { type: "slot" } },
      render: ({ items: Items }) => {
        console.log("Re-rendering Slot");
        return <Items />;
      }
    },
    Heading: {
      fields: { title: { type: "text" } },
      defaultProps: { title: "Heading" },
      render: ({ title }) => {
        console.log("Re-rendering Heading");
        return <h1>{title}</h1>;
      }
    }
  }
};
  1. Navigate to the editor.
  2. Open the devtools console.
  3. Drag and drop a Slot.
  4. Drag and drop two Heading components inside the Slot.
  5. Hover over the Slot component.
  6. Watch the console.
  7. Confirm that no re-rendering logs are printed.

https://github.com/user-attachments/assets/72099349-fc6f-4831-9a3c-fbe318dea87f

FedericoBonel avatar Nov 27 '25 06:11 FedericoBonel

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
puck-demo Ready Ready Preview Comment Nov 27, 2025 6:58am
puck-docs Ready Ready Preview Comment Nov 27, 2025 6:58am

vercel[bot] avatar Nov 27 '25 06:11 vercel[bot]