1515 MultipleRootComponentElementError ,
1616 NoRootComponentElementError ,
1717)
18- from django_unicorn .settings import get_minify_html_enabled , get_script_location
19- from django_unicorn .utils import generate_checksum , html_element_to_string , sanitize_html
18+ from django_unicorn .settings import get_minify_html_enabled
19+ from django_unicorn .utils import generate_checksum , html_element_to_string
2020
2121logger = logging .getLogger (__name__ )
2222
@@ -159,6 +159,7 @@ def __init__(
159159 using = None ,
160160 component = None ,
161161 init_js = False ,
162+ epoch = "" ,
162163 ** kwargs , # noqa: ARG002
163164 ):
164165 super ().__init__ (
@@ -173,6 +174,7 @@ def __init__(
173174
174175 self .component = component
175176 self .init_js = init_js
177+ self .epoch = epoch or ""
176178
177179 def resolve_template (self , template ):
178180 """Override the TemplateResponseMixin to resolve a list of Templates.
@@ -213,82 +215,25 @@ def render(self):
213215 # Prepare Data
214216 frontend_context_variables = self .component .get_frontend_context_variables ()
215217 frontend_context_variables_dict = orjson .loads (frontend_context_variables )
216- checksum = generate_checksum (frontend_context_variables_dict )
218+ data_checksum = generate_checksum (frontend_context_variables_dict )
217219
218220 # Modify Attributes
219221 root_element .set ("unicorn:id" , self .component .component_id )
220222 if hasattr (self .component , "component_name" ):
221223 root_element .set ("unicorn:name" , self .component .component_name )
222224 root_element .set ("unicorn:key" , str (self .component .component_key or "" ))
223- root_element .set ("unicorn:checksum" , checksum )
224225 root_element .set ("unicorn:data" , frontend_context_variables )
225226 root_element .set ("unicorn:calls" , orjson .dumps (self .component .calls ).decode ("utf-8" ))
226227
227- # Calculate content hash (without script)
228+ # Calculate content hash (without script and without checksum )
228229 rendered_template_no_script = html_element_to_string (root_element )
229230 content_hash = generate_checksum (rendered_template_no_script )
231+ self .component ._content_hash = content_hash
230232
231- rendered_template = rendered_template_no_script
232-
233- # Inject Scripts
234- if self .init_js :
235- init = {
236- "id" : self .component .component_id ,
237- "name" : self .component .component_name ,
238- "key" : self .component .component_key ,
239- "data" : orjson .loads (frontend_context_variables ),
240- "calls" : self .component .calls ,
241- "hash" : content_hash ,
242- }
243- init_json = orjson .dumps (init ).decode ("utf-8" )
244- init_json_safe = sanitize_html (init_json )
245- json_element_id = f"unicorn:data:{ self .component .component_id } "
246- init_script = (
247- f"Unicorn.componentInit(JSON.parse(document.getElementById('{ json_element_id } ').textContent));"
248- )
233+ # Set the data checksum on the root element
234+ root_element .set ("unicorn:meta" , data_checksum )
249235
250- # Create JSON script tag
251- json_tag = html .Element ("script" , type = "application/json" , id = json_element_id )
252- json_tag .text = init_json_safe
253-
254- if self .component .parent :
255- self .component ._init_script = init_script
256- self .component ._json_tag = json_tag
257- else :
258- json_tags = [json_tag ]
259-
260- descendants = []
261- descendants .append (self .component )
262- while descendants :
263- descendant = descendants .pop ()
264- for child in descendant .children :
265- init_script = f"{ init_script } { child ._init_script } "
266-
267- if hasattr (child , "_json_tag" ):
268- json_tags .append (child ._json_tag )
269- del child ._json_tag
270-
271- descendants .append (child )
272-
273- script_tag = html .Element ("script" , type = "module" )
274- script_content = (
275- "if (typeof Unicorn === 'undefined') { "
276- "console.error('Unicorn is missing. Do you need {% load unicorn %} or {% unicorn_scripts %}?') "
277- "} else { " + init_script + " }"
278- )
279- script_tag .text = script_content
280-
281- if get_script_location () == "append" :
282- root_element .append (script_tag )
283- for t in json_tags :
284- root_element .append (t )
285- rendered_template = html_element_to_string (root_element )
286- else :
287- root_html = html_element_to_string (root_element )
288- script_html = html_element_to_string (script_tag )
289- for t in json_tags :
290- script_html += html_element_to_string (t )
291- rendered_template = root_html + script_html
236+ rendered_template = html_element_to_string (root_element )
292237
293238 self .component .rendered (rendered_template )
294239 response .content = rendered_template
0 commit comments