SEO
Canonical and hreflang
Add canonical and hreflang alternate links to translated VitNode pages, so search engines show each reader the page in their language.
When a page exists in several languages, hreflang links tell search engines which URL serves which language, and the canonical link names the original of each. VitNode writes both from one alternates object, spelled with your localized URL rules.
Declare alternates on a plugin page
A plugin page declares its alternates from head(), as internal paths: the English route with each language's own values filled in. Only include translations that are actually published.
This example assumes an articles plugin whose fetchArticle helper returns one article:
export const route = defineRoute({
load: async ({ params }) => await fetchArticle(params.slug),
head: ({ loaderData }) => ({
title: loaderData?.title,
alternates: {
en: '/articles/hello-world',
pl: '/articles/witaj-swiecie',
},
}),
})VitNode turns that into a canonical link, one alternate per language and an x-default for the default locale, all absolute and spelled for each language: https://vitnode.pl/artykuly/witaj-swiecie with a domain per language, or your VITNODE_WEB_URL plus /pl/artykuly/witaj-swiecie without one.
Pass /articles/witaj-swiecie, not /pl/artykuly/witaj-swiecie. VitNode
always reads alternates as internal paths, so a public one would be
translated a second time. The delivery API gives you both. Each alternate has
a public path and an internalPath, and metadata has
canonicalInternalPath.
Content Engine pages
A page backed by the Content Engine doesn't write any of this by hand. @vitnode/core/tanstack/content turns a delivery response into page data and a head. That covers redirects, 404s, the SEO title and description, and the internal alternates of every published translation. Blog's own post page is built this way (excerpt):
import {
contentDeliveryPage,
contentDeliveryPageHead,
} from '@vitnode/core/tanstack/content'
import { defineRoute } from '@vitnode/core/tanstack/plugin-routes'
import { blogPostQueryOptions } from './post-query'
export const route = defineRoute({
load: async ({ context, params }) =>
contentDeliveryPage(
await context.queryClient.query(
blogPostQueryOptions({ locale: context.locale, slug: params.slug }),
),
),
head: ({ loaderData }) =>
contentDeliveryPageHead(loaderData?.metadata, {
title: loaderData?.item.title,
}),
})blogPostQueryOptions calls the content type's public routes, /delivery/resolve/{slug} and /{slug} under module: 'content/blog', and returns the resolution and the localized item that contentDeliveryPage expects.
A retired slug answers with a 308 to the current one, and an unknown slug is a 404. Content SEO shows the links a localized record gets.
App routes
App routes do the same with pageHead or localeAlternateLinks from @vitnode/core/tanstack/metadata:
import { localeAlternateLinks } from '@vitnode/core/tanstack/metadata'
export const Route = createFileRoute('/pricing')({
head: ({ match }) => ({
links: localeAlternateLinks({
internalPathname: '/pricing',
locale: match.context.locale,
}),
}),
})internalPathname means "the same page in every locale". Pass alternates: { en: '...', pl: '...' } instead when the path differs per language.
A page in one language only needs no alternates. Give it a canonical in its page metadata.
Check the result
Print the links of a translated page:
curl -s http://localhost:3000/blog/article/english-title | grep -oE '<link [^>]*rel="(canonical|alternate)"[^>]*>'<link href="http://localhost:3000/blog/article/english-title" rel="canonical"/>
<link href="http://localhost:3000/blog/article/english-title" hrefLang="en" rel="alternate"/>
<link href="http://localhost:3000/pl/wpisy/polish-title" hrefLang="pl" rel="alternate"/>
<link href="http://localhost:3000/blog/article/english-title" hrefLang="x-default" rel="alternate"/>The canonical link is the page's own language, and every alternate opens a published translation.
Related
- Advanced localized URLs: domains per language and translated segments.
- Sitemap: the same alternates in
/sitemap.xml.