diff --git a/src/django_components/component.py b/src/django_components/component.py index 9f8cda0b..8336cecb 100644 --- a/src/django_components/component.py +++ b/src/django_components/component.py @@ -1306,6 +1306,8 @@ class Component( # The component rendering was short-circuited by an extension, skipping # the rest of the rendering process. This may be for example a cached content. if result_override is not None: + # Cleanup needs to be done even if short-circuited + context.render_context.pop() return result_override # We pass down the components the info about the component's parent. diff --git a/tests/templates/test_cached_component_inside_include_base.html b/tests/templates/test_cached_component_inside_include_base.html new file mode 100644 index 00000000..260d3df5 --- /dev/null +++ b/tests/templates/test_cached_component_inside_include_base.html @@ -0,0 +1,4 @@ +{% include "test_cached_component_inside_include_sub.html" %} +{% block content %} + THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN +{% endblock %} \ No newline at end of file diff --git a/tests/templates/test_cached_component_inside_include_sub.html b/tests/templates/test_cached_component_inside_include_sub.html new file mode 100644 index 00000000..a6f29bd3 --- /dev/null +++ b/tests/templates/test_cached_component_inside_include_sub.html @@ -0,0 +1 @@ +{% component "test_component" / %} \ No newline at end of file diff --git a/tests/test_component_cache.py b/tests/test_component_cache.py index c46c2d7c..61457afc 100644 --- a/tests/test_component_cache.py +++ b/tests/test_component_cache.py @@ -2,8 +2,10 @@ import time from typing import Any from django.core.cache import caches +from django.template import Template +from django.template.context import Context -from django_components import Component +from django_components import Component, register from django_components.testing import djc_test from .testutils import setup_test_config @@ -207,3 +209,27 @@ class TestComponentCache: # The key should use the custom hash methods expected_key = "components:cache:TestComponent_28880f:custom-args-and-kwargs" assert component.cache.get_cache_key(1, 2, key="value") == expected_key + + def test_cached_component_inside_include(self): + + @register("test_component") + class TestComponent(Component): + template = "Hello" + + class Cache: + enabled = True + + template = Template(""" + {% extends "test_cached_component_inside_include_base.html" %} + {% block content %} + THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN + {% endblock %} + """) + + result = template.render(Context({})) + assert "THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN" not in result + assert "THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN" in result + + result_cached = template.render(Context({})) + assert "THIS_IS_IN_BASE_TEMPLATE_SO_SHOULD_BE_OVERRIDDEN" not in result_cached + assert "THIS_IS_IN_ACTUAL_TEMPLATE_SO_SHOULD_NOT_BE_OVERRIDDEN" in result_cached