PhoenixKitWeb.LayoutHelpers (phoenix_kit v1.7.38)

Copy Markdown View Source

Helper functions for working with PhoenixKit layouts efficiently.

Performance: Avoiding Unnecessary Layout Diffs

When using the dashboard layout, do not pass all assigns:

# ❌ BAD - triggers layout diff on ANY assign change
<PhoenixKitWeb.Layouts.dashboard {assigns}>

# ✅ GOOD - only passes assigns the layout uses
<PhoenixKitWeb.Layouts.dashboard {dashboard_assigns(assigns)}>

The dashboard_assigns/1 function extracts only the assigns the layout actually needs, preventing unnecessary network traffic when other assigns (like application-specific data) change.

Summary

Functions

Extracts only the assigns needed by the dashboard layout.

Returns the list of assign keys used by the dashboard layout.

Functions

dashboard_assigns(assigns)

@spec dashboard_assigns(map()) :: map()

Extracts only the assigns needed by the dashboard layout.

Use this to avoid unnecessary layout diffs when other assigns change:

def render(assigns) do
  ~H"""
  <PhoenixKitWeb.Layouts.dashboard {dashboard_assigns(assigns)}>
    <.my_content data={@data} />
  </PhoenixKitWeb.Layouts.dashboard>
  """
end

This prevents the layout from re-rendering when assigns like @data change, since the layout doesn't use @data.

Performance Impact

Without this optimization, a LiveView receiving 7 updates/second can send ~84KB/sec of redundant layout HTML. With this optimization, layout diffs only occur when layout-relevant assigns actually change.

dashboard_layout_keys()

@spec dashboard_layout_keys() :: [atom()]

Returns the list of assign keys used by the dashboard layout.

Useful for debugging or extending the layout with custom assigns.