Build-Time Internal Linking for Next.js and Astro: Related Links, Mentions and Pinned Links
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
- Run
npm testandnpm run previewin the folder to see the 25 tests pass and the related links and mentions for the ten sample posts, as CSV files. - Build one of the examples (
examples/nextjsorexamples/astro) and look at the related links in the built HTML. - 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. - Call
buildRelatedIndexandbuildMentionsIndexonce per build and render the lists withnext/linkor a plain<a href>. - Optional: run
make_embeddings.pyto add embeddings to the score, and pointreadLinkMenuat 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
Where this resource is used on MLforSEO
Walkthroughs on the blog
Concepts behind it
More internal linking resources
- Coding Script or NotebookLink Blocks for WordPress: Related Posts, Topic Cards, Siblings and Mentions
- Coding Script or NotebookLink Menu to Linking Rules: Python Script for Auto-linking Plugins
- ChecklistBefore You Build: A Checklist for Internal Linking Tools
- Coding Script or NotebookInternal Linking QA and Holdout Group Notebook
- Visualization TemplateInternal Linking Impact Dashboard (Data Studio Template) with Setup Guide
- Coding Script or NotebookInternal Link Menu Notebook: Priority Score and Versioned Recommendations
See all 26 internal linking resources →See all 73 coding script or notebook resources →
