ushka.web.seo
SEO that ships ready: meta tags for your templates, robots.txt and sitemap.xml.
Everything is configured in [seo] of ushka.toml (or USHKA_SEO_*).
Put {{ seo() }} inside <head> instead of <title> and every page gets
a title, description, canonical URL, robots rule, Open Graph and Twitter tags
and JSON-LD. A page adjusts what it needs, in the template:
{{ seo(title="About us", description="Who we are", image="/static/team.png") }}
or from the handler, with a page_seo dict in the template context:
return await render(request, "post.html", {"page_seo": {"title": post.title, "type": "article"}})
Absolute URLs need the public address: set [seo] url. Without it the origin
is taken from the request only in debug or when [security] allowed_hosts is
set, because a Host header nobody checked must not end up in a canonical
link or a sitemap.
Nothing is injected behind your back: the tags appear where you call
seo(). /robots.txt and /sitemap.xml are served unless you define
those routes yourself.
In this module
def safe_url() | value when it is a path or an http(s) URL, else an empty string. |
def origin_of() | The public origin: [seo] url, else the request's when that is safe. |
def page_title() | The <title>: the page title through title_template, or the site name. |
def tags() | The <head> tags for one page. page holds the per-page overrides. |
def seo() | {{ seo() }} in <head>: every SEO tag for this page. |
def robots_txt() | |
def static_paths() | Paths of the app's plain GET pages that belong in a sitemap. |
def sitemap_xml() | |
def install() | Registers /robots.txt and /sitemap.xml as built-in routes (yours win). |
class Seo | app.use(Seo(url="https://shop.example", description="Handmade shoes")). |
def safe_url
def safe_url(value: str) -> strvalue when it is a path or an http(s) URL, else an empty string.
def origin_of
def origin_of(request: Request | None, config: Any) -> str | NoneThe public origin: [seo] url, else the request's when that is safe.
def page_title
def page_title(config: Any, title: str = '') -> strThe <title>: the page title through title_template, or the site name.
def tags
def tags(request: Request | None, config: Any, page: dict[str, Any]) -> MarkupThe <head> tags for one page. page holds the per-page overrides.
def seo
def seo(context, **overrides: Any) -> Markup{{ seo() }} in <head>: every SEO tag for this page.
def robots_txt
def robots_txt(request: Request | None, config: Any) -> strdef static_paths
def static_paths(app: Ushka) -> list[str]Paths of the app's plain GET pages that belong in a sitemap.
def sitemap_xml
async def sitemap_xml(app: Ushka, origin: str) -> strdef install
def install(app: Ushka) -> NoneRegisters /robots.txt and /sitemap.xml as built-in routes (yours win).
class Seo
app.use(Seo(url="https://shop.example", description="Handmade shoes")).
The same as the [seo] section of ushka.toml, in one line: the file and the
environment still win. **options are any other keys of that section.
SEO is already on without it; the piece only sets the defaults.
install
def install(app: Ushka) -> None