Skip to content

Commit

Permalink
better default title for seo (also remove default description) (#2844)
Browse files Browse the repository at this point in the history
* better default title for seo (also remove default description)
  • Loading branch information
Lendemor authored Mar 13, 2024
1 parent eb18ce9 commit e5fc5f9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
22 changes: 16 additions & 6 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ def add_page(
self,
component: Component | ComponentCallable,
route: str | None = None,
title: str = constants.DefaultPage.TITLE,
description: str = constants.DefaultPage.DESCRIPTION,
title: str | None = None,
description: str | None = None,
image: str = constants.DefaultPage.IMAGE,
on_load: (
EventHandler | EventSpec | list[EventHandler | EventSpec] | None
Expand Down Expand Up @@ -468,13 +468,23 @@ def add_page(

component = OverlayFragment.create(component)

meta_args = {
"title": (
title
if title is not None
else format.make_default_page_title(get_config().app_name, route)
),
"image": image,
"meta": meta,
}

if description is not None:
meta_args["description"] = description

# Add meta information to the component.
compiler_utils.add_meta(
component,
title=title,
image=image,
description=description,
meta=meta,
**meta_args,
)

# Add script tags if given
Expand Down
20 changes: 15 additions & 5 deletions reflex/compiler/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common utility functions used in the compiler."""

from __future__ import annotations

import os
Expand Down Expand Up @@ -404,27 +405,36 @@ def get_asset_path(filename: str | None = None) -> str:


def add_meta(
page: Component, title: str, image: str, description: str, meta: list[dict]
page: Component,
title: str,
image: str,
meta: list[dict],
description: str | None = None,
) -> Component:
"""Add metadata to a page.
Args:
page: The component for the page.
title: The title of the page.
image: The image for the page.
description: The description of the page.
meta: The metadata list.
description: The description of the page.
Returns:
The component with the metadata added.
"""
meta_tags = [Meta.create(**item) for item in meta]

children: list[Any] = [
Title.create(title),
]
if description:
children.append(Description.create(content=description))
children.append(Image.create(content=image))

page.children.append(
Head.create(
Title.create(title),
Description.create(content=description),
Image.create(content=image),
*children,
*meta_tags,
)
)
Expand Down
4 changes: 2 additions & 2 deletions reflex/constants/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class DefaultPage(SimpleNamespace):
"""Default page constants."""

# The default title to show for Reflex apps.
TITLE = "Reflex App"
TITLE = "{} | {}"
# The default description to show for Reflex apps.
DESCRIPTION = "A Reflex app."
DESCRIPTION = ""
# The default image to show for Reflex apps.
IMAGE = "favicon.ico"
# The default meta list to show for Reflex apps.
Expand Down
30 changes: 25 additions & 5 deletions reflex/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,17 @@ def to_camel_case(text: str, allow_hyphens: bool = False) -> str:
return leading_underscores_or_hyphens + converted_word


def to_title_case(text: str) -> str:
def to_title_case(text: str, sep: str = "") -> str:
"""Convert a string from snake case to title case.
Args:
text: The string to convert.
sep: The separator to use to join the words.
Returns:
The title case string.
"""
return "".join(word.capitalize() for word in text.split("_"))
return sep.join(word.title() for word in text.split("_"))


def to_kebab_case(text: str) -> str:
Expand All @@ -187,6 +188,20 @@ def to_kebab_case(text: str) -> str:
return to_snake_case(text).replace("_", "-")


def make_default_page_title(app_name: str, route: str) -> str:
"""Make a default page title from a route.
Args:
app_name: The name of the app owning the page.
route: The route to make the title from.
Returns:
The default page title.
"""
title = constants.DefaultPage.TITLE.format(app_name, route)
return to_title_case(title, " ")


def _escape_js_string(string: str) -> str:
"""Escape the string for use as a JS string literal.
Expand Down Expand Up @@ -512,9 +527,14 @@ def format_event(event_spec: EventSpec) -> str:
":".join(
(
name._var_name,
wrap(json.dumps(val._var_name).strip('"').replace("`", "\\`"), "`")
if val._var_is_string
else val._var_full_name,
(
wrap(
json.dumps(val._var_name).strip('"').replace("`", "\\`"),
"`",
)
if val._var_is_string
else val._var_full_name
),
)
)
for name, val in event_spec.args
Expand Down

0 comments on commit e5fc5f9

Please sign in to comment.