2222if sys .version_info >= (3 , 8 ):
2323 from typing import TypedDict , SupportsIndex , Protocol , runtime_checkable , Literal
2424else :
25- from typing_extensions import TypedDict , SupportsIndex , Protocol , runtime_checkable , Literal
25+ from typing_extensions import (
26+ TypedDict ,
27+ SupportsIndex ,
28+ Protocol ,
29+ runtime_checkable ,
30+ Literal ,
31+ )
2632
2733from packaging .version import Version
2834
3238 _package_dir , # type: ignore
3339 _html_escape , # type: ignore
3440 _flatten , # type: ignore
41+ hash_deterministic ,
3542)
3643
3744__all__ = (
@@ -793,7 +800,19 @@ def _hoist_head_content(
793800 # Put <meta charset="utf-8"> at beginning of head, and other hoisted tags at the
794801 # end. This matters only if the <head> tag starts out with some children.
795802 head .insert (0 , Tag ("meta" , charset = "utf-8" ))
803+
804+ # Add some metadata about the dependencies so that shiny.js' renderDependency
805+ # logic knows not to re-render them.
796806 deps = x .get_dependencies ()
807+ if len (deps ) > 0 :
808+ head .append (
809+ Tag (
810+ "script" ,
811+ ";" .join ([d .name + "[" + str (d .version ) + "]" for d in deps ]),
812+ type = "application/html-dependencies" ,
813+ )
814+ )
815+
797816 head .extend (
798817 [
799818 d .as_html_tags (lib_prefix = lib_prefix , include_version = include_version )
@@ -1113,6 +1132,14 @@ def head_content(*args: TagChildArg) -> HTMLDependency:
11131132 *args
11141133 The content to place in the ``<head>``.
11151134
1135+ Note
1136+ ----
1137+ If the same content, ``x``, is included in a document multiple times via
1138+ ``head_content(x)``, ``x`` will only appear once in the final HTML document's
1139+ ``<head>``. More often than not, this is desirable behavior, but if you need the
1140+ same content included multiple times, you can add some irrelevant/empty tags (e.g.,
1141+ ``TagList(x, Tag("meta"))``) to make sure ``x`` is included multiple times.
1142+
11161143 Example
11171144 -------
11181145 >>> from htmltools import *
@@ -1132,7 +1159,7 @@ def head_content(*args: TagChildArg) -> HTMLDependency:
11321159 head = TagList (* args )
11331160 head_str = head .get_html_string ()
11341161 # Create unique ID to use as name
1135- name = "headcontent_{:x}" . format ( abs ( hash ( head_str )) )
1162+ name = "headcontent_" + hash_deterministic ( head_str )
11361163 return HTMLDependency (name = name , version = "0.0" , head = head )
11371164
11381165
0 commit comments