Skip to content

Commit 78f3e30

Browse files
committed
Do not include child DOM in the payload if there is a parent DOM as well.
1 parent e55bb4e commit 78f3e30

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

django_unicorn/static/unicorn/js/messageSender.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function send(component, callback) {
136136
component.hash = responseJson.hash;
137137

138138
let parent = responseJson.parent || {};
139-
const rerenderedComponent = responseJson.dom || {};
139+
const rerenderedComponent = responseJson.dom || "";
140140
const partials = responseJson.partials || [];
141141
const { checksum } = responseJson;
142142

django_unicorn/views/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def _process_component_request(
268268
"checksum": generate_checksum(str(component_request.data)),
269269
}
270270

271+
render_not_modified = False
272+
271273
if partial_doms:
272274
result.update({"partials": partial_doms})
273275
else:
@@ -277,13 +279,14 @@ def _process_component_request(
277279
component_request.hash == hash
278280
and (not return_data or not return_data.value)
279281
and not component.calls
280-
# TODO: Have a strategy to determine when to return a 304 when a component
281-
# has a parent by looking closer at their metadata
282-
and not component.parent
283282
):
284-
raise RenderNotModified()
283+
if not component.parent:
284+
raise RenderNotModified()
285+
else:
286+
render_not_modified = True
285287

286-
# Make sure that partials with comments or blank lines before the root element only return the root element
288+
# Make sure that partials with comments or blank lines before the root element
289+
# only return the root element
287290
soup = BeautifulSoup(rendered_component, features="html.parser")
288291
rendered_component = str(get_root_element(soup))
289292

@@ -342,6 +345,16 @@ def _process_component_request(
342345
}
343346
)
344347

348+
if render_not_modified:
349+
# TODO: Determine if all parents have not changed and return a 304 if
350+
# that's the case
351+
# i.e. render_not_modified = render_not_modified and (parent hash test)
352+
pass
353+
354+
# If there is a parent dom and a child dom, remove the child dom because it is superfluous
355+
if parent.get("dom") and result.get("dom"):
356+
del result["dom"]
357+
345358
parent_result.update({"parent": parent})
346359
component = parent_component
347360
parent_component = parent_component.parent

tests/views/message/test_hash.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99

1010
class FakeComponentParent(UnicornView):
11+
template_name = "templates/test_component_parent.html"
12+
13+
value: int = 0
14+
15+
16+
class FakeComponentParentWithValue(UnicornView):
1117
template_name = "templates/test_component_parent_with_value.html"
1218

1319
value: int = 0
@@ -250,12 +256,14 @@ def test_message_hash_no_change_but_calls(client):
250256

251257
def test_message_hash_no_change_but_parent(client):
252258
component_id = shortuuid.uuid()[:8]
253-
component = FakeComponentParent(
259+
component = FakeComponentParentWithValue(
254260
component_id=component_id,
255-
component_name="tests.views.message.test_hash.FakeComponentParent",
261+
component_name="tests.views.message.test_hash.FakeComponentParentWithValue",
256262
)
257263
component.render()
258264

265+
assert component.value == 0
266+
259267
child = component.children[0]
260268
rendered_child_content = child.render()
261269
child_hash = generate_checksum(rendered_child_content)

0 commit comments

Comments
 (0)