Skip to content

feat: Add lazy panel loading support#613

Open
jakduch wants to merge 1 commit intonette:masterfrom
jakduch:feat/lazy-panel-loading
Open

feat: Add lazy panel loading support#613
jakduch wants to merge 1 commit intonette:masterfrom
jakduch:feat/lazy-panel-loading

Conversation

@jakduch
Copy link

@jakduch jakduch commented Mar 5, 2026

Summary

Implements lazy loading for debug bar panels (resolves #530). Panels registered with lazy: true have their getPanel() deferred to a shutdown function — the tab is rendered immediately, but the panel content is stored in the session and fetched via AJAX when the user clicks or hovers over the tab.

This is useful for panels that perform expensive operations (database profiling, API call logging, heavy data analysis) — they no longer slow down the page response time.

Usage

// Normal panel (eager) — rendered during the request
Debugger::getBar()->addPanel(new MyPanel, 'my-panel');

// Lazy panel — getPanel() is called in shutdown, content served via AJAX
Debugger::getBar()->addPanel(new MyExpensivePanel, 'my-panel', lazy: true);

How it works

  1. Bar::addPanel() accepts a new lazy: bool = false parameter
  2. During renderPanels(), lazy panels skip getPanel() — only getTab() is called
  3. After the response is sent, renderLazyPanels() calls getPanel() and stores content in session via DeferredContent
  4. A new AJAX endpoint (_tracy_bar=lazy-panel.{requestId}.{panelId}) serves the panel content as JSON
  5. On the client side, bar.js detects data-tracy-lazy panels and fetches content on first interaction

Changes

  • Bar.phpaddPanel() with lazy param, renderLazyPanels() method, lazy-aware renderPanels()
  • DeferredContent.php — New lazy-panel AJAX endpoint
  • bar.jsfetchLazyContent() method, lazy-aware Panel.init()
  • bar.phtml — Lazy panels get clickable <a> tabs
  • panels.phtml — Lazy panels get data-tracy-lazy="1" attribute
  • DevelopmentStrategy.php — Calls renderLazyPanels() after render()
  • ProductionStrategy.php — Matching interface update

Demo

A demo page is included at examples/lazy-panels.php with three panels: one eager and two lazy (simulating expensive operations with usleep).

Backward compatibility

Fully backward compatible — the lazy parameter defaults to false, so existing addPanel() calls work unchanged. No changes to IBarPanel interface.

Panels registered with `lazy: true` have their getPanel() deferred to
a shutdown function. The tab renders immediately, but panel content is
stored in session and fetched via AJAX on first interaction.

This avoids expensive panel rendering blocking the page response.

Usage:
  Debugger::getBar()->addPanel($panel, 'id', lazy: true);
@jakduch jakduch force-pushed the feat/lazy-panel-loading branch from 63bf239 to 44bb6fc Compare March 5, 2026 15:02
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.

Tracy panel lazy loading

1 participant