日本語

Alternate pages

LocalePage::alternates writes the page's address in every language into <head>, so a search engine shows each searcher the page in their language.

LocalePage::alternates

pub trait LocalePage: Sized {
    fn alternates<L: Locale>(self, origin: Origin, url: impl Fn(L) -> Url) -> Self;
}

impl<S: Signals> LocalePage for Page<S> { /* ... */ }

LocalePage::alternates takes the site's Origin and a function from a language to the page's Url in that language, which is the route's url with the language changed. It calls the function once per variant of locales and writes one <link rel="alternate"> into <head> for each, with the variant's tag as hreflang. It writes one more with hreflang="x-default" at the first variant, the default language:

// `Lang` is a `Locale` enum with the variants `En` and `Ja`, and `ByLang` its text struct.
use hypersig::{Origin, Page, Route, Signals};
use hypersig_i18n::LocalePage;

#[derive(Signals, Serialize, Deserialize)]
struct Post {}

hypersig::routes! {
    fn pages(Router<()>);
    const POST: Route<(Lang, u32)> = "/{lang}/posts/{id}" => post;
}

const SITE: Origin = Origin::new("https://example.com");

async fn post((): (), (lang, id): (Lang, u32)) -> Page<Post> {
    let title = lang.pick(ByLang { en: "Release notes", ja: "リリースノート" });
    Page::new(title, Post {})
        .lang(lang.tag())
        .alternates(SITE, |other: Lang| POST.url((other, id)))
}

let html = post((), (Lang::Ja, 3)).await.render();
assert!(html.contains(concat!(
    r#"<link rel="alternate" hreflang="en" href="https://example.com/en/posts/3">"#,
    r#"<link rel="alternate" hreflang="ja" href="https://example.com/ja/posts/3">"#,
    r#"<link rel="alternate" hreflang="x-default" href="https://example.com/en/posts/3">"#,
)));

The page in either language writes the same three tags into <head>:

<link rel="alternate" hreflang="en" href="https://example.com/en/posts/3">
<link rel="alternate" hreflang="ja" href="https://example.com/ja/posts/3">
<link rel="alternate" hreflang="x-default" href="https://example.com/en/posts/3">

A search engine reads these tags to show each searcher the page in their language, and the page at x-default to a searcher whose language none of them names. Browsers do nothing visible with them.

The function returns a Url, which only a route builds, so a path written by hand is E0308:

use hypersig::Origin;
use hypersig_i18n::LocalePage;

const SITE: Origin = Origin::new("https://example.com");

// error[E0308]: mismatched types: expected `Url`, found `String`
let page = Page::new("Post", Post {})
    .alternates(SITE, |lang: Lang| format!("/{}/posts/3", lang.tag()));

The origin

impl Origin {
    pub const fn new(origin: &'static str) -> Self;
    pub const fn under(self, prefix: &'static str) -> Self;
}

Search engines read href here only as an absolute URL, and a Url is a path, so alternates writes each one after an Origin. Origin::new takes http:// or https://, a host, and an optional port, and nothing after them. Origin::under adds the path the site is served under, such as /blog. Both panic on anything else, which in a const is E0080:

use hypersig::Origin;

// error[E0080]: evaluation panicked: an origin ends at the host and port
const SITE: Origin = Origin::new("https://example.com/blog/");

An origin read from configuration at startup goes through Origin::parse, which returns an error in place of the panic.

One alternate at a time

impl<S: Signals> Page<S> {
    pub fn alternate(self, origin: Origin, url: &Url, hreflang: impl Into<LanguageTag>) -> Self;
    pub fn alternate_default(self, origin: Origin, url: &Url) -> Self;
}

alternates covers pages whose route holds the language. For a page whose versions sit at routes of their own, such as /about and /ja/kaisha, Page::alternate writes one <link rel="alternate"> per call, and Page::alternate_default writes the x-default. hreflang takes a LanguageTag or an IntlLocale, as Page::lang does:

use hypersig::{IntlLocale, Origin, Page, Route, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct About {}

hypersig::routes! {
    fn pages(Router<()>);
    const ABOUT: Route = "/about" => about;
    const KAISHA: Route = "/ja/kaisha" => kaisha;
}

const SITE: Origin = Origin::new("https://example.com");

let html = Page::new("About us", About {})
    .lang(IntlLocale::En)
    .alternate(SITE, &ABOUT.url(()), IntlLocale::En)
    .alternate(SITE, &KAISHA.url(()), IntlLocale::Ja)
    .alternate_default(SITE, &ABOUT.url(()))
    .render();
assert!(html.contains(concat!(
    r#"<link rel="alternate" hreflang="en" href="https://example.com/about">"#,
    r#"<link rel="alternate" hreflang="ja" href="https://example.com/ja/kaisha">"#,
    r#"<link rel="alternate" hreflang="x-default" href="https://example.com/about">"#,
)));

What stays unchecked

  • Nothing checks that the function returns the same page. A function that returns HOME.url(lang) for every page tells search engines that every page is the home page in another language, and they may drop the pages from their results.
  • Search engines use the tags only when each page in the set lists all of them, its own included. Call alternates on the page in every language.
  • Nothing checks that the origin is where the site is served. A wrong host sends search engines to pages that are not the site's.

Next