Skip to content

docs: warn against per-render state keyed by component ID in extensions - #1649

Merged
JuroOravec merged 1 commit into
masterfrom
docs/extension-per-render-state
May 30, 2026
Merged

JuroOravec merged 1 commit into
masterfrom
docs/extension-per-render-state

Conversation

@JuroOravec

@JuroOravec JuroOravec commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

(Opus 4.8)

Summary

Documentation follow-up to #1648 (the CacheExtension short-circuit leak).

That leak was a specific instance of a general trap for extension authors: if an extension stores per-render state in on_component_input (in a dict on the extension, keyed by component_id) and removes it in on_component_rendered, it leaks whenever a later extension short-circuits the render — because on_component_rendered never fires for that component, so the entry is never removed. One orphaned entry accumulates per skipped render.

This PR documents the trap and the fix (bind per-render state to the component's lifetime, e.g. its per-component extension config), so users don't reintroduce the same bug in their own extensions.

Changes

  • on_component_input docstring (extension.py) — a !!! warning explaining that short-circuiting skips on_component_data / on_component_rendered, and that per-render state must be tied to the component lifetime.
  • Extensions guide (docs/concepts/advanced/extensions.md) — a new "Storing data for a single render" section with ❌/✅ examples, written for readers new to extensions. It sits next to the existing "Pass slot metadata" anti-pattern, which is the same shape for slots.

No code behavior change.

Checks

  • uv run ruff check . / ruff format --check — clean
  • uv run mypy src/django_components/extension.py — clean
  • uv run mkdocs build --strict — clean (link anchors verified against docs/reference/extension_hooks.md)

🤖 Generated with Claude Code

Follow-up to the CacheExtension leak fixed in #1648. That leak was an
instance of a general trap: an extension that stores per-render state in
`on_component_input` (in a dict on the extension, keyed by component ID) and
cleans it up in `on_component_rendered` will leak whenever a later extension
short-circuits the render, because `on_component_rendered` never fires for
that component.

Document the trap and the fix (bind the state to the component lifetime, e.g.
the per-component extension config) in two places:

- A `!!! warning` on the `on_component_input` hook docstring.
- A "Storing data for a single render" section in the extensions guide,
  written for readers new to extensions.

No code behavior change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@JuroOravec JuroOravec mentioned this pull request May 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [59020a2] After [298565b] Ratio Benchmark (Parameter)
61.2±2ms 62.9±2ms 1.03 Components vs Django.timeraw_render_lg_first('django')
227±6ms 226±5ms 1 Components vs Django.timeraw_render_lg_first('django-components')

@JuroOravec
JuroOravec merged commit 77e3029 into master May 30, 2026
16 checks passed
@JuroOravec
JuroOravec deleted the docs/extension-per-render-state branch May 30, 2026 09:53
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.

1 participant