-
Notifications
You must be signed in to change notification settings - Fork 0
feat(seo): add sitemap.xml and robots.txt generation (#48, #60) #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
69f2852
feat(seo): add sitemap.xml and robots.txt generation (#48, #60)
x3ek 6807bbc
fix(seo): add lastmod to post index entry in sitemap
x3ek 1c5d43b
feat(seo): add date field to Page model and lastmod to page sitemap e…
x3ek 856a865
refactor(seo): extract _add_url helper to reduce duplication in sitem…
x3ek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """SEO routes: sitemap.xml and robots.txt.""" | ||
|
|
||
| import datetime | ||
| from xml.etree.ElementTree import Element, SubElement, tostring | ||
|
|
||
| from fastapi import APIRouter | ||
| from fastapi.responses import Response | ||
|
|
||
| from squishmark.models.content import Config, Page, Post | ||
| from squishmark.services.cache import get_cache | ||
| from squishmark.services.content import get_all_pages, get_all_posts | ||
| from squishmark.services.github import get_github_service | ||
| from squishmark.services.markdown import get_markdown_service | ||
|
|
||
| router = APIRouter(tags=["seo"]) | ||
|
|
||
| SITEMAP_NS = "http://www.sitemaps.org/schemas/sitemap/0.9" | ||
| SITEMAP_CACHE_KEY = "seo:sitemap" | ||
| ROBOTS_CACHE_KEY = "seo:robots" | ||
|
|
||
|
|
||
| def _add_url(urlset: Element, loc: str, lastmod: datetime.date | None = None) -> None: | ||
| """Append a <url> entry to the sitemap urlset.""" | ||
| url_el = SubElement(urlset, "url") | ||
| SubElement(url_el, "loc").text = loc | ||
| if lastmod: | ||
| SubElement(url_el, "lastmod").text = lastmod.isoformat() | ||
|
|
||
|
|
||
| def _build_sitemap(config: Config, posts: list[Post], pages: list[Page]) -> bytes: | ||
| """Build a sitemap.xml from config, posts, and pages.""" | ||
| site_url = config.site.url.rstrip("/") if config.site.url else "" | ||
| newest_post_date = posts[0].date if posts else None | ||
|
|
||
| urlset = Element("urlset", xmlns=SITEMAP_NS) | ||
|
|
||
x3ek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _add_url(urlset, f"{site_url}/", newest_post_date) | ||
| _add_url(urlset, f"{site_url}/posts", newest_post_date) | ||
|
|
||
| for post in posts: | ||
| _add_url(urlset, f"{site_url}{post.url}", post.date) | ||
|
|
||
| for page in pages: | ||
| if page.visibility == "public": | ||
| _add_url(urlset, f"{site_url}{page.url}", page.date) | ||
|
|
||
| return b'<?xml version="1.0" encoding="utf-8"?>\n' + tostring(urlset, encoding="unicode").encode("utf-8") | ||
|
|
||
|
|
||
| def _build_robots_txt(config: Config) -> str: | ||
| """Build robots.txt content.""" | ||
| site_url = config.site.url.rstrip("/") if config.site.url else "" | ||
|
|
||
| lines = [ | ||
| "User-agent: *", | ||
| "Allow: /", | ||
| "", | ||
| "Disallow: /admin/*", | ||
| "Disallow: /auth/*", | ||
| "Disallow: /health", | ||
| "Disallow: /webhooks/*", | ||
| ] | ||
|
|
||
| if site_url: | ||
| lines.append("") | ||
| lines.append(f"Sitemap: {site_url}/sitemap.xml") | ||
|
|
||
| return "\n".join(lines) + "\n" | ||
x3ek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @router.get("/sitemap.xml") | ||
| async def sitemap_xml() -> Response: | ||
| """Serve the XML sitemap.""" | ||
| cache = get_cache() | ||
|
|
||
| cached = await cache.get(SITEMAP_CACHE_KEY) | ||
| if cached is not None: | ||
| return Response(content=cached, media_type="application/xml; charset=utf-8") | ||
|
|
||
| github_service = get_github_service() | ||
| config_data = await github_service.get_config() | ||
| config = Config.from_dict(config_data) | ||
| markdown_service = get_markdown_service(config) | ||
|
|
||
| posts = await get_all_posts(github_service, markdown_service) | ||
| pages = await get_all_pages(github_service, markdown_service) | ||
|
|
||
| xml_bytes = _build_sitemap(config, posts, pages) | ||
| await cache.set(SITEMAP_CACHE_KEY, xml_bytes) | ||
| return Response(content=xml_bytes, media_type="application/xml; charset=utf-8") | ||
|
|
||
|
|
||
| @router.get("/robots.txt") | ||
| async def robots_txt() -> Response: | ||
| """Serve robots.txt.""" | ||
| cache = get_cache() | ||
|
|
||
| cached = await cache.get(ROBOTS_CACHE_KEY) | ||
| if cached is not None: | ||
| return Response(content=cached, media_type="text/plain; charset=utf-8") | ||
|
|
||
| github_service = get_github_service() | ||
| config_data = await github_service.get_config() | ||
| config = Config.from_dict(config_data) | ||
|
|
||
| content = _build_robots_txt(config) | ||
| await cache.set(ROBOTS_CACHE_KEY, content) | ||
| return Response(content=content, media_type="text/plain; charset=utf-8") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.