Skip to content

Commit 611f21c

Browse files
committed
Fix circulat import
1 parent 940a96e commit 611f21c

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

reflex/app.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
from reflex.utils import codespaces, console, exceptions, format, prerequisites, types
8787
from reflex.utils.exec import is_prod_mode, is_testing_env, should_skip_compile
8888
from reflex.utils.imports import ImportVar
89-
from sitemap import serve_sitemap
89+
from .sitemap import serve_sitemap
9090

9191
# Define custom types.
9292
ComponentCallable = Callable[[], Component]
@@ -271,7 +271,6 @@ def __init__(self, **kwargs):
271271
)
272272
super().__init__(**kwargs)
273273
base_state_subclasses = BaseState.__subclasses__()
274-
self.add_sitemap()
275274

276275
# Special case to allow test cases have multiple subclasses of rx.BaseState.
277276
if not is_testing_env() and len(base_state_subclasses) > 1:
@@ -288,6 +287,9 @@ def __init__(self, **kwargs):
288287
self._add_cors()
289288
self._add_default_endpoints()
290289

290+
# Add sitemap
291+
self.add_sitemap()
292+
291293
for clz in App.__mro__:
292294
if clz == App:
293295
continue
@@ -304,7 +306,7 @@ def __init__(self, **kwargs):
304306
from reflex.utils.compat import windows_hot_reload_lifespan_hack
305307

306308
self.register_lifespan_task(windows_hot_reload_lifespan_hack)
307-
309+
308310
def add_sitemap(self):
309311
@self.api.get("/sitemap.xml")
310312
async def sitemap():
@@ -540,12 +542,13 @@ def add_page(
540542
component.sitemap_changefreq = sitemap_changefreq
541543

542544
# Auto-detect priority if not explicitly set
543-
if not hasattr(component, 'sitemap_priority'):
545+
if not hasattr(component, "sitemap_priority"):
544546
component.sitemap_priority = self._auto_detect_priority(route)
545-
547+
546548
# Set default changefreq if not explicitly set
547-
if not hasattr(component, 'sitemap_changefreq'):
549+
if not hasattr(component, "sitemap_changefreq"):
548550
component.sitemap_changefreq = "weekly"
551+
549552
# Ensure state is enabled if this page uses state.
550553
if self.state is None:
551554
if on_load or component._has_stateful_event_triggers():
@@ -591,14 +594,13 @@ def add_page(
591594
on_load = [on_load]
592595
self.load_events[route] = on_load
593596

594-
595597
def _auto_detect_priority(self, route: Optional[str]) -> float:
596598
"""Auto detect sitemap priority"""
597599
if route is None or route == "/" or route == "index":
598600
return 1.0
599601
depth = route.count("/")
600602
return max(0.1, 1.0 - (depth * 0.2))
601-
603+
602604
def get_load_events(self, route: str) -> list[EventHandler | EventSpec]:
603605
"""Get the load events for a route.
604606

reflex/sitemap.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from fastapi import Response
66
from xml.etree.ElementTree import Element, SubElement, tostring
77
from xml.dom import minidom
8+
import reflex as rx
9+
810

911
def generate_xml(links: List[Dict[str, Any]]) -> str:
1012
urlset = Element("urlset", xmlns="https://www.sitemaps.org/schemas/sitemap/0.9")
@@ -20,27 +22,29 @@ def generate_xml(links: List[Dict[str, Any]]) -> str:
2022
reparsed = minidom.parseString(rough_string)
2123
return reparsed.toprettyxml(indent=" ")
2224

23-
def generate_sitemap(app: rx.App) -> str:
25+
26+
def generate_sitemap(app: "rx.App") -> str:
27+
import reflex as rx
28+
2429
links = []
2530
for route, component in app.pages.items():
2631
# Ignore dynamic routes for now
2732
if "[" in route and "]" in route:
2833
continue
29-
34+
3035
# Handle the index route
3136
if route == "index":
3237
route = "/"
33-
34-
38+
3539
# Check for explicit sitemap settings
3640
sitemap_priority = getattr(component, "sitemap_priority", None)
3741
sitemap_changefreq = getattr(component, "sitemap_changefreq", "weekly")
38-
42+
3943
# Calculate priority if not explicitly set
4044
if sitemap_priority is None:
4145
depth = route.count("/")
4246
sitemap_priority = max(0.5, 1.0 - (depth * 0.1))
43-
47+
4448
links.append(
4549
{
4650
"loc": f"{{{{ BASE_URL}}}}{route}",
@@ -50,13 +54,17 @@ def generate_sitemap(app: rx.App) -> str:
5054
)
5155
return generate_xml(links)
5256

53-
async def serve_sitemap(app: rx.App) -> Response:
57+
58+
async def serve_sitemap(app: "rx.App") -> Response:
59+
import reflex as rx
60+
5461
sitemap_content = generate_sitemap(app)
5562
domain = get_config().deploy_url or "http://localhost:3000"
5663
modified_sitemap = sitemap_content.replace("{{ BASE_URL }}", domain)
5764
return Response(content=modified_sitemap, media_type="application/xml")
5865

59-
def add_sitemap_to_app(app: rx.App):
66+
67+
def add_sitemap_to_app(app: "rx.App"):
6068
@app.api.get("/sitemap.xml")
6169
async def sitemap():
62-
return await serve_sitemap(app)
70+
return await serve_sitemap(app)

0 commit comments

Comments
 (0)