Skip to content

Stop Broken Internal Links at Build Time (Hugo, Astro, Next.js)

By Lazarina Stoy · 11 min read
Stop Broken Internal Links at Build Time (Hugo, Astro, Next.js): illustration of a code notebook connected to an API. An MLforSEO guide by Lazarina Stoy.

Rename one post on a static site and every link to the old URL breaks. Nothing warns you. The build finishes, the deploy goes green, and the first sign is a 404 in a crawl report weeks later, or a reader who clicked and left.

I tested this on purpose this week. I added a link to a page that doesn’t exist to a post, then built the same small site with Astro 7 and with Eleventy 3. Both builds completed without a single warning. Hugo, with its default settings, did the same.

The fix costs very little: check internal links when the site is built, and let a broken one fail the build or the pull request. Some generators can do this with one setting. The rest need a link checker run on the built HTML. This post covers Hugo, Astro (with and without Starlight), Eleventy, Next.js, Gatsby, Docusaurus and MkDocs, with the configs I tested (all of them except Gatsby) and a GitHub Actions workflow you can copy.

Free configs

Static site link checks: tested settings for seven generators, a lychee config, a one-command check script and GitHub Actions workflows.

On this page
  1. Why check at build time
  2. What each generator checks on its own
  3. Hugo: ref shortcodes are checked, Markdown links aren’t
  4. Astro: one plugin for Starlight, a checker for everything else
  5. Next.js: typed routes catch typos, not missing pages
  6. Docusaurus and MkDocs: switch on what’s already there
  7. One check that works for every generator
  8. lychee or linkinator?
  9. Run it on every pull request
  10. What this doesn’t catch
  11. What the course adds
  12. Frequently asked questions

Why check at build time

A broken internal link is a dead end for the reader and a wasted request for a crawler. Google’s guidance on crawlable links is about links being followable; a link to a page that no longer exists is followable straight into a 404. On a site with a CMS, plugins and redirect managers often paper over this. On a static or headless site, nothing does, unless you add it (my headless CMS internal linking guide covers where those links live). And internal links are how your site architecture holds together, so a broken one is a small crack in the structure as well as a bad click.

The build is the cheapest place to catch it, for three reasons:

  • Every page is there. The generator, or a checker run straight after it, can see every file it’s about to publish, including pages nothing links to.
  • The built HTML is the truth. It’s what crawlers and readers receive, after templates, components and Markdown have all had their say. A link that’s fine in the source can still break in the output.
  • It’s before anyone sees it. A failed pull request costs one fix. A broken link found by a crawl report has already been live.

If you add links automatically, at build time or with a script, this matters even more. In build-time internal linking for Next.js and Astro I pin approved links into related-post blocks, and the very first thing that code does is refuse pinned links to pages that aren’t in the build.

What each generator checks on its own

Table of eight static site generators showing what happened to a deliberately broken internal link with default settings, the native setting that checks links, and what to add: Hugo, Astro and Eleventy built it without a warning
What each generator catches on its own: Hugo, Astro and Eleventy built a page with a broken link without a warning, MkDocs warned and still exited 0, and Starlight with its validator plugin failed the build (tested 5 Dec 2026; Gatsby not tested).

Here’s what I found when I tested each one with a deliberately broken link on 5 December 2026. The versions are in brackets.

GeneratorNative settingWhat it catchesWhat to add
Hugo (0.171)refLinksErrorLevel = "ERROR", already the defaultBroken ref and relref shortcodes onlyA link render hook that fails on unresolved Markdown links
Astro + Starlight (7.4 / 0.43)starlight-links-validator pluginBroken links and #hashes in Markdown and MDXNothing else
Astro (7.4)NoneNothing: the broken link built finelychee on dist/
Eleventy (3.1)NoneNothing: the broken link built finelychee on _site/
Next.js (16.5)typedRoutes: trueLiteral hrefs that match no routelychee on out/, or a crawl of next start
Gatsby (5)None found in the docsNothinglychee on public/ (not tested)
Docusaurus (3.10)onBrokenLinks, onBrokenAnchors, onBrokenMarkdownLinks hookRoutes, anchors and .md links, at docusaurus buildSet all three to throw
MkDocs (1.6)strict: true with validation:Missing pages, anchors and unrecognised linksTurn both on

Two defaults caught me out. MkDocs finds a link to a missing page and prints a warning, then finishes the build with exit code 0, so CI passes. And Docusaurus fails on broken routes by default but only warns about broken anchors and broken .md links.

Hugo’s ref and relref shortcodes resolve a page at build time, and refLinksErrorLevel defaults to ERROR, so a broken one fails the build. Most people write ordinary Markdown links, though. Those go through Hugo’s embedded link render hook, and the render hook docs say it doesn’t log an error or a warning when it can’t resolve a destination. My test confirmed it: the broken link was in the HTML and the build exited cleanly.

The free configs include a replacement layouts/_markup/render-link.html. For each internal link it looks for a page, a page resource or a global resource, resolves relative links like ../other-post/ against the current page, and stops the build when nothing matches:

ERROR Broken internal link "/blog/orphan-page/" in blog/anchor-text-guide.md (no page or file matches it)
ERROR error building site: logged 1 error(s)

External links, mailto: and #section links pass through untouched. If you’d rather start with warnings, swap errorf for warnf and run hugo build --panicOnWarning in CI. One caveat: the hook looks pages up by content path, which matches the URL with Hugo’s default permalinks. With custom permalinks, write internal links as content paths (/blog/post.md) and let Hugo turn them into URLs.

Astro: one plugin for Starlight, a checker for everything else

If your docs run on Starlight, install starlight-links-validator and add it to the plugins list, as in its getting started guide. A broken link or a missing heading then fails astro build and points at the file:

[ERROR] [starlight-links-validator] Links validation failed.
  ╭─ index.md
5 | /blog/orphan-page/
  ·                 ╰── invalid link
5 | /blog/internal-link-audit/#start-with-crawl
  ·                                       ╰── invalid hash

Plain Astro has no equivalent. There’s an open roadmap discussion about internal link validation, and until that lands the answer is to check the built output in dist/, which is what the next section is about. The same goes for Eleventy (_site/) and Gatsby (public/).

Next.js: typed routes catch typos, not missing pages

Next.js has a setting that looks like a link checker: typedRoutes: true. It makes next build type-check every literal href passed to next/link against the routes in your app. A typo in the route fails the build:

app/page.tsx(9,115): error TS2322: Type '"/blgo/orphan-pages"' is not assignable to type 'UrlObject | RouteImpl<"/blgo/orphan-pages">'.

It’s worth switching on, and it’s worth knowing what it can’t see. It checks route patterns, so /blog/no-such-post passed, because it matches /blog/[slug]. hrefs that come from data, such as links from a CMS or a related posts list, have to be cast and aren’t checked at all. And in my test a literal written with a trailing slash failed the type check even with trailingSlash: true, so write literal hrefs without it.

For the rest, check the pages Next.js actually produced. With output: 'export', that’s the HTML in out/. Without it, start the production server and crawl it with linkinator, which is how I checked a server build of the example site.

Docusaurus and MkDocs: switch on what’s already there

Docusaurus has the most complete native check of the lot. onBrokenLinks already defaults to throw; set onBrokenAnchors and markdown.hooks.onBrokenMarkdownLinks to throw as well. The hook replaced the older top-level onBrokenMarkdownLinks in version 3.9, as the config reference notes. All three failed the build in my test. The check runs on docusaurus build, so CI catches it even if local previews don’t.

MkDocs needs two things together: strict: true, which turns warnings into a failed build, and a validation: block in the configuration that makes missing pages and anchors count as warnings. With both:

WARNING -  Doc file 'index.md' contains a link '/blog/internal-link-audit.md#start-with-crawl', but the doc 'blog/internal-link-audit.md' does not contain an anchor '#start-with-crawl'.
Aborted with 1 warnings in strict mode!

One check that works for every generator

For Astro, Eleventy, Gatsby and Next.js, and as a safety net for the others, run lychee on the built HTML. It’s a fast, open source link checker, and in offline mode it checks internal links only: no network requests, so it takes milliseconds and never fails because someone else’s site was slow.

The free configs include a lychee.toml and a script, so the check is one line after your build:

npm run build
scripts/check-built-site.sh dist        # or out, public, _site, build

Run against the small fixture in the folder, which has a renamed post and a renamed heading, it prints:

[ERROR] file:///.../fixtures/broken-site/blog/missing-post (at 9:18) | File not found. Check if file exists and path is correct
[ERROR] file:///.../fixtures/broken-site/blog/real-post#old-heading (at 10:18) | Cannot find fragment

🔍 6 Total (in 0s) 🔗 6 Unique ✅ 3 OK 🚫 2 Errors 👻 1 Excluded

It exits with code 2, which stops a CI job. On the two example sites from the build-time linking post it checked 92 links in the Next.js output and 75 in the Astro output, all fine, and it caught the broken link I added to the Astro and Eleventy builds.

Three settings in lychee.toml do most of the work:

  • offline = true keeps the check to internal links.
  • include_fragments = "anchor-only" catches links to a #heading that was renamed.
  • index_files = ["index.html"] makes a folder link such as /blog/post/ require an index.html. I added this after a false alarm: without it, lychee reported a heading that did exist as missing, because it couldn’t look inside folder links.

Static exports that use clean URLs without a trailing slash (/blog/post served from blog/post.html) need one more line, fallback_extensions = ["html"], which is in the file and commented out.

lychee or linkinator?

linkinator is the other checker you’ll see recommended, and it’s good. The difference that matters for SEO: lychee checks every HTML file in the folder, while linkinator starts at one page and follows links. A page nothing links to, an orphan page, is never visited by linkinator, so a broken link on it goes unnoticed. If you want to find those orphans themselves, my blog architecture audit does that from a crawl.

I also hit a gotcha worth sharing. A skip pattern I’ve seen suggested for checking a local folder is --skip "^(?!http://localhost)", meant to skip external links. linkinator 8.1 serves the folder on http://127.0.0.1, so that pattern skips every link, including the start page. It reports “Successfully scanned 0 links” and exits with 0, on a site with a broken link in it. The config in the free folder uses ^(?!http://127\.0\.0\.1), which found the broken link. My rule since: a link check that reports zero links has failed.

Run it on every pull request

Flow diagram of the GitHub Actions workflow: pull request, build, lychee offline on the built HTML, pass or fail; plus a comparison of lychee checking every file versus linkinator following links and missing orphan pages, and the linkinator skip-pattern gotcha
Internal links gate the pull request and external links get a weekly report; lychee checks every built file, while linkinator only reaches pages that something links to (lychee 0.24.2, linkinator 8.1.0, tested 5 Dec 2026).

The folder has a GitHub Actions workflow, link-check.yml. On every pull request it builds the site and runs lychee on the output with lychee-action, which fails the job on any broken internal link or fragment and writes the list to the job summary. You change three lines: the output folder, the toolchain and the build command. Everything is on GitHub, with the configs for each generator and notes on what I tested. If your deploy step also tells search engines about changed URLs, run it after this check passes; my IndexNow guide has a script for that.

External links get a separate, optional workflow that runs weekly with network requests on and opens an issue when something breaks. External links break on someone else’s schedule, so I don’t let them block a merge. Internal links are yours, so they should.

If you’re planning a bigger restructure, the same check doubles as a safety net for redirects: in offline mode a link to an old URL fails even if your host redirects it, which is a nudge to update links to the final URL. My site migration checklist and redirect mapping with fuzzy matching cover the rest of that job.

What this doesn’t catch

  • Links added by JavaScript. Only links in the built HTML are checked. A widget that adds links after the page loads isn’t in the file, so neither tool sees it, and crawlers that don’t render JavaScript won’t either. That’s one more reason to render related links at build time, and how to check JS-injected links covers finding the ones you already have.
  • External links, in offline mode. Use the weekly workflow for those.
  • Links that work but point at the wrong page. A checker knows a page exists, not whether it’s the right one. That still takes a person, or a review step in your linking workflow.

For the wider picture of keeping links healthy, including redirected and non-canonical targets, see the link health section of 10 ways to automate internal linking with AI.

What the course adds

Catching broken links is the last step of shipping links safely, and it’s the part that’s the same on every stack. The course AI-powered Internal Linking Audit and Automation, led by Sam Torres, covers the steps before it. In my guest module on shipping links in your CMS, Lesson 5.7 is the implementation guide for headless and static sites: where links live on those stacks, how to add a link to structured rich text safely, rules applied at build time with a preview you read before anything ships, the checks to run before writing links back into a CMS, and how to record when each link went live, so its effect can be measured later in the course.

Frequently asked questions

Does Next.js check for broken links?

Partly. With typedRoutes: true, next build fails on a literal next/link href that matches no route. It can’t tell whether a slug exists under a dynamic route, and it doesn’t check links in your content. Check the built pages with lychee or linkinator for those.

Does Astro check internal links at build time?

Not on its own: in my test with Astro 7.4, a page with a broken link built without a warning. Starlight sites can add starlight-links-validator. Other Astro sites can run lychee on dist/ after the build.

How do I find broken internal links on a static site?

Build the site, then run an internal link checker on the output folder: lychee --offline with a root directory set, or linkinator with --recurse. Add the same command to CI so every pull request is checked.

Should broken external links fail the build?

I don’t think so. They break for reasons outside your control, often temporarily. Check them on a schedule and fix them in batches; keep the build gate for internal links.

MLforSEO Academy

Pick your learning path

Start with the course that goes deepest on this article, then more courses taught by Lazarina Stoy, most relevant first.

Recommended with this article

AI-powered Internal Linking Audit and Automation

by Sam Torres with Lazarina Stoy

Audit how your site links to itself, find the pages and entities your links leave out, and plan and automate the fixes with AI at scale.

View course · €350 →

Introduction to Machine Learning for SEO

by Lazarina Stoy

Go from zero to running your first ML workflows for SEO: classification, clustering, entity extraction, fuzzy matching and content transformation.

View course · €250 →

Semantic AI-powered SEO Keyword Research

by Lazarina Stoy with Beatrice Gamba

Build a semantic keyword universe and an automated content-brief workflow, from query understanding through SERP analysis to delivery.

View course · €400 →

AI Search & LLMs: Entity SEO and Knowledge Graph Strategies for Brands

by Beatrice Gamba with Lazarina Stoy

Win in both traditional Google and LLM-driven search by mastering entities, brand recognition, and knowledge graph integration.

View course · €200 →

The AI-powered Entity Audit

by Lazarina Stoy

A complete, repeatable entity audit you can run on any site. Built on the 5Cs of entity representation and demonstrated end to end on a real, unedited site.

View course · €150 →
Bundle & save up to €200 →See all 6 courses →Already an Academy member? Use Community30 for 30% off your next course or bundle.