GuidesTechnical SEO
Tutorial: automatic internal linking with embeddings
1. One vector per page
// scripts/embeddings.ts: run before the build
const pages = await loadPages(); // title, summary, url
for (const p of pages) {
if (cache[p.url]?.hash === hash(p.title + p.summary)) continue;
cache[p.url] = { hash: hash(p.title + p.summary), v: await embed(`${p.title}. ${p.summary}`) };
}
await write('src/data/embeddings.json', cache);
2. The neighbours
const cos = (a: number[], b: number[]) => {
let d = 0, na = 0, nb = 0;
for (let i = 0; i < a.length; i++) { d += a[i] * b[i]; na += a[i] ** 2; nb += b[i] ** 2; }
return d / Math.sqrt(na * nb);
};
export function neighbours(url: string, n = 4, threshold = 0.5) {
const v = cache[url].v;
return Object.entries(cache)
.filter(([u]) => u !== url)
.map(([u, e]) => ({ u, s: cos(v, e.v) }))
.filter((x) => x.s >= threshold)
.sort((a, b) => b.s - a.s)
.slice(0, n);
}
3. The “Read next” block
- 1Four linksThe closest above the threshold.
- 2Anchor = titleClear for readers and search engines.
- 3Same languageFilter by language before comparing.
4. Settings
| Setting | Effect |
|---|---|
| Similarity threshold | Avoids unrelated links on isolated pages |
| Manual links first | A pillar page can impose its links |
| Exclusions | Legal notice, contact pages |
Vectors: embeddings explained. Tracking: setting up Google Search Console.
Frequently asked questions
- How many links per page?
- Three to five in a dedicated block are enough. Beyond that, each link matters less to readers and search engines alike.
- What anchor text should I use?
- The target page's title, or a short version of it. It tells readers and search engines what the page is about.
- Do I recompute on every build?
- The embeddings, only for changed pages: keep a cache. Finding neighbours is instant.