Skip to content
Coding Script or NotebookGitHubFree

Build-Time Internal Linking for Next.js and Astro: Related Links, Mentions and Pinned Links

ForInternal linking

By Lazarina Stoy· Updated 5 Dec 2026

When a site moves to Next.js or Astro, the related posts block and the other automatic links a CMS used to add disappear. This free code puts them back at build time: a small scoring function picks the most related pages for every page, a “where this is mentioned” index lists the pages that name each one, and an optional list of approved links pins editor-chosen links to the top. Everything is written into the HTML as plain <a href> links, so crawlers see them on the first fetch and nothing loads in the browser.

Who it’s for

Developers and technical SEOs running a Next.js, Astro or other static or headless site who want reliable internal linking without a plugin, a widget or a paid tool, and who are comfortable copying a folder into a JavaScript project.

How to use it

  1. Run npm test and npm run preview in the folder to see the 25 tests pass and the related links and mentions for the ten sample posts, as CSV files.
  2. Build one of the examples (examples/nextjs or examples/astro) and look at the related links in the built HTML.
  3. Copy lib/ into your project and map your content to pages: slug, URL, title, and whatever you have of tags, category, date, other names and body text.
  4. Call buildRelatedIndex and buildMentionsIndex once per build and render the lists with next/link or a plain <a href>.
  5. Optional: run make_embeddings.py to add embeddings to the score, and point readLinkMenu at your approved link menu to pin links.

What you get

  • A dependency-free JavaScript library with TypeScript types: scoring from shared tags (Jaccard similarity), the same category and optional cosine similarity, with a written reason for every link
  • A “where this is mentioned” index that flags the mentions that aren’t linked yet
  • Pinned links from a CSV with the link menu columns, using only rows marked approved, with a build warning for any row that points at a page missing from the build
  • Working Next.js 16 (App Router) and Astro 7 (content collections) examples that build identical links from the same sample content
  • A preview script, an embeddings script and 25 unit tests

Limits

The score is deliberately simple: good at “more on this topic”, weak at reading order, which is what pinned links are for. It relies on tidy tags. Related blocks are template links, useful against orphan pages and no substitute for links written in a sentence. Running it is free; the optional embeddings use a small open model on your own machine.

Read the walkthrough in build-time internal linking for Next.js and Astro. The course AI-powered Internal Linking Audit and Automation covers headless and static implementation end to end in Lesson 5.7.

What’s included

  • Related links scored from shared tags, category and optional embeddings, with a reason for every link
  • "Where this is mentioned" index that flags mentions not linked yet
  • Pinned links from your approved link menu, with a build warning for any that point at missing pages
  • Working Next.js 16 and Astro 7 examples that build identical links from the same content
  • No dependencies, TypeScript types, a preview script and 25 unit tests