Spaces:
Running on Zero
Running on Zero
SkyeBrowse Developer
Claude Opus 5.5
Serve SEO head server-side, keep it out of embeds, skip theme redirect for crawlers
fec4e98 Download tests/test_document_head.py from broyang/3dai: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/broyang/3dai/resolve/main/tests/test_document_head.py
- Command line
-
hf download hf://spaces/broyang/3dai/tests/test_document_head.py
-
curl -L -o test_document_head.py https://huggingface.co/spaces/broyang/3dai/resolve/main/tests/test_document_head.py
2.55 kB
| import sys | |
| import unittest | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from starlette.applications import Starlette # noqa: E402 | |
| from starlette.responses import HTMLResponse, JSONResponse # noqa: E402 | |
| from starlette.routing import Route # noqa: E402 | |
| from starlette.testclient import TestClient # noqa: E402 | |
| import gradio_runtime # noqa: E402 | |
| GRADIO_INDEX = """<!doctype html><html><head> | |
| <meta property="og:title" content="Gradio" /> | |
| <meta name="twitter:creator" content="@Gradio" /> | |
| <title>Gradio</title> | |
| </head><body>app</body></html>""" | |
| TITLE = "Pocket 3D AI | SkyeBrowse" | |
| HEAD = '<link rel="canonical" href="https://3dai.skyebrowse.com/" />' | |
| GOOGLEBOT = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" | |
| CHROME = "Mozilla/5.0 (Macintosh) AppleWebKit/537.36 Chrome/141.0 Safari/537.36" | |
| def served_app(title=TITLE): | |
| kwargs = gradio_runtime.build_launch_kwargs(None, "", "", HEAD, title=title) | |
| routes = [ | |
| Route("/", lambda request: HTMLResponse(GRADIO_INDEX)), | |
| Route("/config", lambda request: JSONResponse({"head": kwargs["head"]})), | |
| ] | |
| return TestClient(Starlette(routes=routes, **kwargs["app_kwargs"]), follow_redirects=False) | |
| class DocumentHeadTest(unittest.TestCase): | |
| def test_page_html_carries_title_and_head_without_gradio_defaults(self): | |
| html = served_app().get("/?__theme=light", headers={"Accept": "text/html"}).text | |
| self.assertIn(f"<title>{TITLE}</title>", html) | |
| self.assertIn(HEAD, html) | |
| self.assertNotIn('content="Gradio"', html) | |
| self.assertNotIn("@Gradio", html) | |
| self.assertEqual(html.count("<title>"), 1) | |
| def test_config_head_is_empty_so_embeds_do_not_inherit_it(self): | |
| self.assertEqual(served_app().get("/config").json()["head"], "") | |
| def test_without_title_head_stays_in_config(self): | |
| self.assertEqual(served_app(title="").get("/config").json()["head"], HEAD) | |
| def test_browsers_are_redirected_to_light_theme(self): | |
| response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": CHROME}) | |
| self.assertEqual(response.status_code, 307) | |
| self.assertIn("__theme=light", response.headers["location"]) | |
| def test_crawlers_get_the_page_without_a_redirect(self): | |
| response = served_app().get("/", headers={"Accept": "text/html", "User-Agent": GOOGLEBOT}) | |
| self.assertEqual(response.status_code, 200) | |
| self.assertIn(HEAD, response.text) | |
| if __name__ == "__main__": | |
| unittest.main() | |