Skip to content

Commit 2ef16bd

Browse files
committed
Refactor meta.py: abstract common keywords and remove duplicate code
1 parent 7e38067 commit 2ef16bd

File tree

1 file changed

+77
-97
lines changed

1 file changed

+77
-97
lines changed

pcweb/meta/meta.py

Lines changed: 77 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,68 @@
22

33
from pcweb.constants import REFLEX_DOMAIN, REFLEX_DOMAIN_URL, TWITTER_CREATOR
44

5+
TITLE = "The unified platform to build and scale enterprise apps."
56
ONE_LINE_DESCRIPTION = "Build with AI, iterate in Python, deploy to any cloud. The unified platform to build and scale enterprise apps."
67

7-
meta_tags = [
8-
# HTML Meta Tags
9-
{"name": "application-name", "content": "Reflex"},
10-
{
11-
"name": "keywords",
12-
"content": "reflex, python, web apps, framework, open source, frontend, backend, full stack",
13-
},
14-
{
15-
"name": "description",
16-
"content": ONE_LINE_DESCRIPTION,
17-
},
18-
# Facebook Meta Tags
19-
{"property": "og:url", "content": REFLEX_DOMAIN_URL},
20-
{"property": "og:type", "content": "website"},
21-
{"property": "og:title", "content": "Reflex · Build with AI, Deploy with Python"},
22-
{
23-
"property": "og:description",
24-
"content": ONE_LINE_DESCRIPTION,
25-
},
26-
{"property": "og:image", "content": "/previews/index_preview.webp"},
27-
# Twitter Meta Tags
28-
{"name": "twitter:card", "content": "summary_large_image"},
29-
{"property": "twitter:domain", "content": REFLEX_DOMAIN},
30-
{"property": "twitter:url", "content": REFLEX_DOMAIN_URL},
31-
{"name": "twitter:title", "content": "Reflex · Build with AI, Deploy with Python"},
32-
{
33-
"name": "twitter:description",
34-
"content": ONE_LINE_DESCRIPTION,
35-
},
36-
{"name": "twitter:image", "content": "/previews/index_preview.webp"},
37-
{"name": "twitter:creator", "content": TWITTER_CREATOR},
38-
]
39-
40-
hosting_meta_tags = [
41-
# HTML Meta Tags
42-
{"name": "application-name", "content": "Reflex"},
43-
{
44-
"name": "keywords",
45-
"content": "reflex, python, web apps, framework, open source, frontend, backend, full stack",
46-
},
47-
{
48-
"name": "description",
49-
"content": ONE_LINE_DESCRIPTION,
50-
},
51-
# Facebook Meta Tags
52-
{"property": "og:url", "content": REFLEX_DOMAIN_URL},
53-
{"property": "og:type", "content": "website"},
54-
{"property": "og:title", "content": "Reflex · Build with AI, Deploy with Python"},
55-
{
56-
"property": "og:description",
57-
"content": ONE_LINE_DESCRIPTION,
58-
},
59-
{"property": "og:image", "content": "/previews/hosting_preview.webp"},
60-
# Twitter Meta Tags
61-
{"name": "twitter:card", "content": "summary_large_image"},
62-
{"property": "twitter:domain", "content": REFLEX_DOMAIN},
63-
{"property": "twitter:url", "content": REFLEX_DOMAIN_URL},
64-
{"name": "twitter:title", "content": "Reflex · Build with AI, Deploy with Python"},
65-
{
66-
"name": "twitter:description",
67-
"content": ONE_LINE_DESCRIPTION,
68-
},
69-
{"name": "twitter:image", "content": "/previews/hosting_preview.webp"},
70-
{"name": "twitter:creator", "content": TWITTER_CREATOR},
71-
]
8+
# Common constants
9+
APPLICATION_NAME = "Reflex"
10+
KEYWORDS = (
11+
"reflex, python, web apps, framework, open source, frontend, backend, full stack"
12+
)
13+
TWITTER_CARD_TYPE = "summary_large_image"
14+
OG_TYPE = "website"
15+
16+
17+
def _build_meta_tags(
18+
title: str,
19+
description: str,
20+
image: str,
21+
url: str = REFLEX_DOMAIN_URL,
22+
) -> list[dict[str, str]]:
23+
"""Build a list of meta tags with the given parameters.
24+
25+
Args:
26+
title: The page title.
27+
description: The page description.
28+
image: The image path for social media previews.
29+
url: The page URL (defaults to REFLEX_DOMAIN_URL).
30+
31+
Returns:
32+
A list of meta tag dictionaries.
33+
"""
34+
return [
35+
# HTML Meta Tags
36+
{"name": "application-name", "content": APPLICATION_NAME},
37+
{"name": "keywords", "content": KEYWORDS},
38+
{"name": "description", "content": description},
39+
# Facebook Meta Tags
40+
{"property": "og:url", "content": url},
41+
{"property": "og:type", "content": OG_TYPE},
42+
{"property": "og:title", "content": title},
43+
{"property": "og:description", "content": description},
44+
{"property": "og:image", "content": image},
45+
# Twitter Meta Tags
46+
{"name": "twitter:card", "content": TWITTER_CARD_TYPE},
47+
{"property": "twitter:domain", "content": REFLEX_DOMAIN},
48+
{"property": "twitter:url", "content": url},
49+
{"name": "twitter:title", "content": title},
50+
{"name": "twitter:description", "content": description},
51+
{"name": "twitter:image", "content": image},
52+
{"name": "twitter:creator", "content": TWITTER_CREATOR},
53+
]
54+
55+
56+
meta_tags = _build_meta_tags(
57+
title=TITLE,
58+
description=ONE_LINE_DESCRIPTION,
59+
image="/previews/index_preview.webp",
60+
)
61+
62+
hosting_meta_tags = _build_meta_tags(
63+
title=TITLE,
64+
description=ONE_LINE_DESCRIPTION,
65+
image="/previews/hosting_preview.webp",
66+
)
7267

7368

7469
def favicons_links() -> list[rx.Component]:
@@ -90,42 +85,27 @@ def favicons_links() -> list[rx.Component]:
9085
def create_meta_tags(
9186
title: str, description: str, image: str, url: str | None = None
9287
) -> list[rx.Component]:
88+
"""Create meta tags for a page.
89+
90+
Args:
91+
title: The page title.
92+
description: The page description.
93+
image: The image path for social media previews.
94+
url: The page URL (optional, defaults to REFLEX_DOMAIN_URL).
95+
96+
Returns:
97+
A list of meta tag dictionaries.
98+
"""
9399
page_url = url if url else REFLEX_DOMAIN_URL
94100

95101
if image and not image.startswith(("http://", "https://")):
96102
image_url = f"https://reflex.dev{'' if image.startswith('/') else '/'}{image}"
97103
else:
98104
image_url = image
99105

100-
return [
101-
# HTML Meta Tags
102-
{"name": "application-name", "content": "Reflex"},
103-
{
104-
"name": "keywords",
105-
"content": "reflex, python, web apps, framework, open source, frontend, backend, full stack",
106-
},
107-
{
108-
"name": "description",
109-
"content": description,
110-
},
111-
# Facebook Meta Tags
112-
{"property": "og:url", "content": page_url},
113-
{"property": "og:type", "content": "website"},
114-
{"property": "og:title", "content": title},
115-
{
116-
"property": "og:description",
117-
"content": description,
118-
},
119-
{"property": "og:image", "content": image_url},
120-
# Twitter Meta Tags
121-
{"name": "twitter:card", "content": "summary_large_image"},
122-
{"property": "twitter:domain", "content": REFLEX_DOMAIN},
123-
{"property": "twitter:url", "content": page_url},
124-
{"name": "twitter:title", "content": title},
125-
{
126-
"name": "twitter:description",
127-
"content": description,
128-
},
129-
{"name": "twitter:image", "content": image_url},
130-
{"name": "twitter:creator", "content": TWITTER_CREATOR},
131-
]
106+
return _build_meta_tags(
107+
title=title,
108+
description=description,
109+
image=image_url,
110+
url=page_url,
111+
)

0 commit comments

Comments
 (0)