Skip to content

Commit 8ef9c57

Browse files
committed
Fix bug where direct views were not caching components correctly.
1 parent bb405dc commit 8ef9c57

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

django_unicorn/components/unicorn_view.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,44 @@ def dispatch(self, request, *args, **kwargs):
353353
self.mount()
354354
self.hydrate()
355355

356+
self._cache_component(request, **kwargs)
357+
356358
return self.render_to_response(
357359
context=self.get_context_data(),
358360
component=self,
359361
init_js=True,
360362
)
361363

364+
def _cache_component(self, request: HttpRequest, parent=None, **kwargs):
365+
"""
366+
Cache the component in the module and the Django cache. Re-set the `request` that got
367+
removed to make the component cacheable.
368+
"""
369+
370+
# Put the location for the component name in a module cache
371+
location_cache[self.component_name] = (self.__class__.__name__, self.__module__)
372+
373+
# Put the component's class in a module cache
374+
views_cache[self.component_id] = (self.__class__, parent, kwargs)
375+
376+
cacheable_component = None
377+
378+
# Put the instantiated component into a module cache and the Django cache
379+
try:
380+
cacheable_component = get_cacheable_component(self)
381+
except UnicornCacheError as e:
382+
logger.warning(e)
383+
384+
if cacheable_component:
385+
if COMPONENTS_MODULE_CACHE_ENABLED:
386+
constructed_views_cache[self.component_id] = cacheable_component
387+
388+
cache = caches[get_cache_alias()]
389+
cache.set(cacheable_component.component_cache_key, cacheable_component)
390+
391+
# Re-set `request` on the component that got removed when making it cacheable
392+
self.request = request
393+
362394
@timed
363395
def get_frontend_context_variables(self) -> str:
364396
"""
@@ -807,30 +839,7 @@ def _get_component_class(
807839
**kwargs,
808840
)
809841

810-
# Put the location for the component name in a module cache
811-
location_cache[component_name] = (class_name, module_name)
812-
813-
# Put the component's class in a module cache
814-
views_cache[component_id] = (component_class, parent, kwargs)
815-
816-
# Put the instantiated component into a module cache and the Django cache
817-
cacheable_component = None
818-
819-
try:
820-
cacheable_component = get_cacheable_component(component)
821-
except UnicornCacheError as e:
822-
logger.warning(e)
823-
824-
if cacheable_component:
825-
if COMPONENTS_MODULE_CACHE_ENABLED:
826-
constructed_views_cache[component_id] = cacheable_component
827-
828-
cache.set(
829-
cacheable_component.component_cache_key, cacheable_component
830-
)
831-
832-
# Re-setup the `request` that got removed to make the component cacheable
833-
component.setup(request)
842+
component._cache_component(request, parent, **kwargs)
834843

835844
return component
836845
except ModuleNotFoundError as e:

0 commit comments

Comments
 (0)