Skip to content

{% cache %} compat follow-ups: docs, callback leak fix, regression tests - #1651

Merged
JuroOravec merged 1 commit into
masterfrom
docs/cache-tag-followups
May 30, 2026
Merged

JuroOravec merged 1 commit into
masterfrom
docs/cache-tag-followups

Conversation

@JuroOravec

@JuroOravec JuroOravec commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

(Juro)

As I read through #1619, I explored and found:

  • 1 bug
  • few edge cases that are still problematic with the compatibility with Django's {% cache %} tag.

I captured the "proper" solution here #1650.

But since the proper design would be possibly only in v3, right now I only documented what to be aware of when using the {% cache %} tag with components.


(Opus 4.8)

Summary

Follow-ups to #1619 (Django {% cache %} tag compatibility with components):

  • Docs. New "Using Django's {% cache %} tag with components" section in docs/concepts/advanced/component_caching.md. Covers the three real gotchas users can hit (same cached fragment embedded twice on a page → handlers attached more than once; JS/CSS variables silently lost after a deploy with a persistent fragment cache; vary_on hygiene), and documents the process-global patch of Django's built-in {% cache %} tag.

  • Fix. Bounded callback-dict accumulation in _assemble_cached_fragment: the two synthetic callbacks added to tree.on_component_intermediate_callbacks / tree.on_component_rendered_callbacks per {% cache %} invocation are now popped via try/finally, so a template-level loop with many {% cache %} calls doesn't leave one dead-lambda pair per iteration in the (still-live) tree's callback dicts.

  • Tests. Two regression tests:

    • test_cache_embedded_twice_on_same_page_freezes_inner_render_id — pins the documented "frozen data-djc-id" behavior so a future change is forced to discuss it.
    • test_error_in_cache_body_does_not_poison_cache — verifies that an error mid-cache-body doesn't write a partial value (the second render must run the component again, not return a poisoned hit).
  • Docs/comments. Rewrote the cache_tag.py module docstring (the old text described an earlier, abandoned implementation approach), and annotated the DjcCacheNode.render body to mark which lines are copied verbatim from Django's CacheNode.render (Django 5.2) and which are djc-specific. Helps future maintainers re-diff against upstream when Django bumps.

The architectural fix that would dissolve both documented limitations entirely (cache RenderObject instead of the rendered string) is tracked in #1650 for v3.

🤖 Generated with Claude Code

Follow-ups to #1619 (Django {% cache %} tag compatibility):

- Docs: new "Using Django's {% cache %} tag with components" section in
  docs/concepts/advanced/component_caching.md covering the real gotchas
  (same fragment embedded twice -> handlers attached more than once;
  JS/CSS variables silently lost after a deploy with a persistent
  fragment cache; vary_on hygiene), plus the process-global tag patch.

- Fix: bounded callback-dict accumulation in _assemble_cached_fragment.
  The two synthetic callbacks added to the tree's callback dicts per
  {% cache %} invocation are now popped via try/finally, so a template-
  level loop with many {% cache %} calls doesn't leave one dead-lambda
  pair per iteration.

- Tests: two regression tests.
    - test_cache_embedded_twice_on_same_page_freezes_inner_render_id
      pins the documented "frozen data-djc-id" behavior so a future
      change is forced to discuss it.
    - test_error_in_cache_body_does_not_poison_cache verifies that an
      error mid-cache-body doesn't write a partial value.

- Module docstring rewrite + body annotation in cache_tag.py. The old
  docstring described an earlier, abandoned implementation approach;
  the new one matches the actual implementation. The DjcCacheNode.render
  body is now marked to show which lines are copied verbatim from
  Django's CacheNode.render (Django 5.2) and which are djc-specific, so
  future maintainers can re-diff against upstream when Django bumps.

The architectural fix that would dissolve both documented limitations
(cache RenderObject instead of the rendered string) is tracked in
#1650 for v3.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [77e3029] After [5e5807c] Ratio Benchmark (Parameter)
69.8±1ms 69.8±1ms 1 Components vs Django.timeraw_render_lg_first('django')
251±3ms 247±3ms 0.99 Components vs Django.timeraw_render_lg_first('django-components')



class DjcCacheNode(CacheNode):
def render(self, context: Context) -> str:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added commnets here to it's clearer which parts were copied from Django and which are ours.

# Pop the callbacks in `finally` so a template-level loop with many
# {% cache %} invocations doesn't leave one set of dead lambdas per
# iteration in the (still-live) tree's callback dicts.
try:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One code change - component_post_render() is wrapped in try/finally, and callbacks are removed after teh code is done.

@JuroOravec
JuroOravec merged commit 08f50d2 into master May 30, 2026
16 checks passed
@JuroOravec
JuroOravec deleted the docs/cache-tag-followups branch May 30, 2026 14:29
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