Skip to content

Commit beaa713

Browse files
authored
Allow opt-out of sitemap inclusion (#6077)
Pass `context={"sitemap": None}` to app.add_page to exclude the page from the generated sitemap.
1 parent 89f3a27 commit beaa713

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

reflex/plugins/sitemap.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ def generate_links_for_sitemap(
138138
links: list[SitemapLink] = []
139139

140140
for page in unevaluated_pages:
141-
sitemap_config: SitemapLinkConfiguration = page.context.get("sitemap", {})
141+
sitemap_config: SitemapLinkConfiguration | None = page.context.get(
142+
"sitemap", {}
143+
)
144+
if sitemap_config is None:
145+
continue
142146

143147
if is_route_dynamic(page.route) or page.route == "404":
144148
if not sitemap_config:

tests/units/plugins/test_sitemap.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,45 @@ def mock_component():
240240
)
241241

242242

243+
@patch("reflex.config.get_config")
244+
def test_generate_links_for_sitemap_opt_out(mock_get_config: MagicMock):
245+
"""Test generate_links_for_sitemap with sitemap set to None.
246+
247+
Args:
248+
mock_get_config: Mock for the get_config function.
249+
"""
250+
mock_get_config.return_value.deploy_url = None # No deploy URL
251+
252+
def mock_component():
253+
return rx.text("Unlisted")
254+
255+
pages = [
256+
UnevaluatedPage(
257+
component=mock_component,
258+
route="unlisted",
259+
title=None,
260+
description=None,
261+
image="favicon.ico",
262+
on_load=None,
263+
meta=[],
264+
context={"sitemap": None},
265+
),
266+
UnevaluatedPage(
267+
component=mock_component,
268+
route="listed",
269+
title=None,
270+
description=None,
271+
image="favicon.ico",
272+
on_load=None,
273+
meta=[],
274+
context={},
275+
),
276+
]
277+
links = generate_links_for_sitemap(pages)
278+
assert len(links) == 1
279+
assert {"loc": "/listed"} in links
280+
281+
243282
@patch("reflex.config.get_config")
244283
def test_generate_links_for_sitemap_loc_override(mock_get_config: MagicMock):
245284
"""Test generate_links_for_sitemap with loc override in sitemap config.

0 commit comments

Comments
 (0)