Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions reflex/compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,34 @@ def create_document_root(
Returns:
The document root.
"""
head_components = [
*(
head_components
or [
# Default meta tags if user does not provide.
Meta.create(char_set="utf-8"),
Meta.create(
name="viewport", content="width=device-width, initial-scale=1"
),
]
),
# Always include the framework meta and link tags.
existing_meta_types = set()

for component in head_components or []:
if isinstance(component, Meta):
if component.char_set: # pyright: ignore[reportAttributeAccessIssue]
existing_meta_types.add("char_set")
if component.name == "viewport": # pyright: ignore[reportAttributeAccessIssue]
existing_meta_types.add("viewport")
Comment thread
adhami3310 marked this conversation as resolved.
Outdated

# Always include the framework meta and link tags.
always_head_components = [
ReactMeta.create(),
Links.create(),
]
maybe_head_components = []
# Only include these if the user has not specified them.
if "char_set" not in existing_meta_types:
maybe_head_components.append(Meta.create(char_set="utf-8"))
if "viewport" not in existing_meta_types:
maybe_head_components.append(
Meta.create(name="viewport", content="width=device-width, initial-scale=1")
)

head_components = [
*(head_components or []),
*maybe_head_components,
*always_head_components,
]
return Html.create(
Head.create(*head_components),
Body.create(
Expand Down
4 changes: 2 additions & 2 deletions tests/units/compiler/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ def test_create_document_root():
html_custom_attrs={"project": "reflex"},
)
assert isinstance(root, utils.Html)
assert len(root.children[0].children) == 4
assert len(root.children[0].children) == 6
names = [c.tag for c in root.children[0].children]
assert names == ["Scripts", "Scripts", "Meta", "Links"]
assert names == ["Scripts", "Scripts", "meta", "meta", "Meta", "Links"]
lang = root.lang # pyright: ignore [reportAttributeAccessIssue]
assert isinstance(lang, LiteralStringVar)
assert lang.equals(Var.create("rx"))
Expand Down