DEV Community

CodeHunterz
CodeHunterz

Posted on

Technical SEO in the Next.js App Router: Metadata, Canonicals, Sitemaps and robots.txt

Most Next.js projects for small businesses ship with a good looking homepage and a long list of SEO problems nobody notices until the client asks why their service pages are not showing up on Google. The good news is that the App Router gives you first class tools for almost every technical SEO basic. The bad news is that they are easy to half configure.

This guide walks through the setup we use on local service business sites: a shared metadata base, per page titles and descriptions, canonical URLs, a generated sitemap, a robots file, and a few guardrails that stop staging and filter pages from leaking into the index. Everything here uses built in Next.js features, no extra packages.

1. Start with a root metadata object

The root layout is where you define defaults that every page inherits. The most important field here is metadataBase. Without it, any relative URL you put in canonicals or Open Graph images stays relative, and Next.js will warn you during the build.

// app/layout.tsx
import type { Metadata } from "next";

export const metadata: Metadata = {
  metadataBase: new URL("https://www.example.com"),
  title: {
    default: "Example Plumbing | Licensed Plumbers",
    template: "%s | Example Plumbing",
  },
  description:
    "Licensed plumbers for repairs, water heaters and drain cleaning.",
  openGraph: {
    type: "website",
    siteName: "Example Plumbing",
  },
};
Enter fullscreen mode Exit fullscreen mode

The title.template saves you from typing the brand name on every page. A child page that sets title: "Water Heater Repair" will render as "Water Heater Repair | Example Plumbing".

Pick one host and stick with it. If the live site answers on both example.com and www.example.com, decide which one is canonical, set it in metadataBase, and redirect the other at the hosting or DNS level. Mixing the two is one of the most common reasons we see duplicate pages in Search Console.

2. Give every indexable page its own title and description

Static pages can export a plain metadata object. Dynamic routes, such as a page per service or per city, should use generateMetadata so the title and description come from the same data that renders the page.

// app/services/[slug]/page.tsx
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getService } from "@/lib/services";

type Props = { params: Promise<{ slug: string }> };

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;
  const service = await getService(slug);
  if (!service) return {};

  return {
    title: service.seoTitle,
    description: service.seoDescription,
    alternates: { canonical: `/services/${service.slug}` },
    openGraph: {
      title: service.seoTitle,
      description: service.seoDescription,
      url: `/services/${service.slug}`,
    },
  };
}

export default async function ServicePage({ params }: Props) {
  const { slug } = await params;
  const service = await getService(slug);
  if (!service) notFound();
  // render the page
}
Enter fullscreen mode Exit fullscreen mode

In recent versions of Next.js, params is a Promise, so await it in both functions. If you are on an older version, it is a plain object. Check the docs for the version you are running.

A few rules that keep titles useful:

  • Lead with what the page is about, then the brand. Searchers scan the first few words.
  • Keep descriptions specific to the page. A description that is identical across twenty service pages gives Google nothing to work with.
  • Store seoTitle and seoDescription next to the content, not hard coded in the component. Whoever edits the content will keep them in sync.

3. Set canonical URLs on purpose

The alternates.canonical field renders a <link rel="canonical"> tag. Because metadataBase is set, a relative path like /services/drain-cleaning becomes a full URL in the output.

You want a canonical on every indexable page, pointing to the clean version of itself. This matters more than people expect. Ad clicks add tracking parameters, social shares add their own, and some analytics setups append session values. Each variation is technically a different URL. A self referencing canonical tells search engines which one to keep.

Be careful with two patterns:

  1. Do not point every page's canonical at the homepage. It looks harmless in a layout file, but it tells Google that your service pages are duplicates of the homepage.
  2. Do not set the canonical in the root layout. It will be inherited by every page that does not override it. Set it per page, or per route segment layout when a whole section shares a pattern.

If you want to sanity check what a page actually renders, view the page source on the deployed site and search for rel="canonical". The free tools on our website checker and SEO tools page can also show you the title, description and status code for a live URL.

4. Generate the sitemap from your data

The App Router supports a sitemap.ts file that returns an array of URLs. Next.js turns it into /sitemap.xml at build time or on request, depending on whether the data is static.

// app/sitemap.ts
import type { MetadataRoute } from "next";
import { getAllServices } from "@/lib/services";
import { getAllPosts } from "@/lib/posts";

const BASE = "https://www.example.com";

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const [services, posts] = await Promise.all([
    getAllServices(),
    getAllPosts(),
  ]);

  const staticRoutes = ["", "/about", "/contact", "/services"].map((path) => ({
    url: `${BASE}${path}`,
    lastModified: new Date(),
  }));

  const serviceRoutes = services.map((s) => ({
    url: `${BASE}/services/${s.slug}`,
    lastModified: s.updatedAt,
  }));

  const postRoutes = posts.map((p) => ({
    url: `${BASE}/blog/${p.slug}`,
    lastModified: p.updatedAt,
  }));

  return [...staticRoutes, ...serviceRoutes, ...postRoutes];
}
Enter fullscreen mode Exit fullscreen mode

Things worth getting right:

  • Only list URLs you want indexed. No drafts, no thank you pages, no filtered views, no redirects. If a URL is in the sitemap, it should return 200 and carry a canonical pointing to itself.
  • Use real dates for lastModified. Setting every entry to "now" on every build makes the field meaningless. Pull it from your content source where you can.
  • Use the same host as metadataBase. A sitemap full of non www URLs on a www site sends mixed signals.

Once it is deployed, submit /sitemap.xml in Google Search Console and check the status after a day or two. If you see "Couldn't fetch", open the URL in a browser and confirm it returns XML and not an HTML error page.

5. Add robots.ts, and make it environment aware

A robots.ts file in the app directory generates /robots.txt.

// app/robots.ts
import type { MetadataRoute } from "next";

export default function robots(): MetadataRoute.Robots {
  const isProduction = process.env.VERCEL_ENV === "production";

  if (!isProduction) {
    return { rules: { userAgent: "*", disallow: "/" } };
  }

  return {
    rules: {
      userAgent: "*",
      allow: "/",
      disallow: ["/api/", "/thank-you"],
    },
    sitemap: "https://www.example.com/sitemap.xml",
  };
}
Enter fullscreen mode Exit fullscreen mode

The environment check is the important part. Preview deployments are public URLs. If they are crawlable, you can end up with copies of the client's site competing with the real one. The example uses the VERCEL_ENV variable because that is what Vercel provides. On other hosts, use whatever variable tells you the deployment is production.

Remember that robots.txt controls crawling, not indexing. A page blocked in robots.txt can still appear in results if other sites link to it. For pages that must stay out of the index, use a noindex directive instead, which brings us to the next step.

6. Use noindex for pages that should exist but not rank

Some pages need to be reachable but have no business in search results: form confirmation pages, internal search results, paginated filter combinations, or a staging route left in production. Set the robots field in metadata:

// app/thank-you/page.tsx
import type { Metadata } from "next";

export const metadata: Metadata = {
  title: "Thanks for reaching out",
  robots: { index: false, follow: true },
};
Enter fullscreen mode Exit fullscreen mode

Do not combine noindex with a robots.txt disallow on the same URL. If crawlers are blocked from fetching the page, they never see the noindex tag.

7. Handle 404s and redirects cleanly

Two small details that show up constantly in audits:

Return a real 404. Call notFound() when a dynamic route has no matching data, as in the service page example above. Rendering a friendly "not found" message with a 200 status creates soft 404s, which Search Console will flag.

Keep redirects permanent and short. When you rename a page, add a permanent redirect in next.config:

// next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  async redirects() {
    return [
      {
        source: "/our-services/:slug",
        destination: "/services/:slug",
        permanent: true,
      },
    ];
  },
};

export default nextConfig;
Enter fullscreen mode Exit fullscreen mode

Avoid chains where A redirects to B and B redirects to C. Update old redirects to point at the final destination, and update internal links so they skip the redirect entirely.

8. A quick pre launch checklist

Before handing a site over, we run through this list:

  • metadataBase is set to the production host, and the other host redirects to it.
  • Every indexable page has a unique title, a unique description and a self referencing canonical.
  • /sitemap.xml lists only live, indexable URLs on the correct host.
  • /robots.txt allows crawling in production and blocks it on previews.
  • Thank you pages and internal utility pages carry noindex.
  • Unknown slugs return a real 404 status.
  • Renamed URLs have a single permanent redirect.
  • The sitemap is submitted in Search Console.

None of this is glamorous, but it is the foundation that content and links build on. If you want a wider list that covers speed, mobile layout and structured data as well, our technical SEO checklist for small business websites goes through the rest. And if you are still deciding on a stack for a client, we compared the tradeoffs in Next.js vs WordPress for business websites.

Wrapping up

The App Router makes technical SEO a matter of configuration rather than plugins. Set a metadata base, generate titles and canonicals from the same data that renders the page, build the sitemap from that data too, keep previews out of the index, and return honest status codes. Do those things once in a starter template and every new client site inherits them.

If you would rather have someone handle this as part of a build, that is the kind of work we do in our Next.js development service.

CodeHunterz Team

Top comments (0)