MagicSpace SEO
ServicesCustomersAboutBlog
MagicSpace SEO

SaaS SEO agency for founders and scale-ups. We drive signups, demos, and revenue—not just traffic.

XX / TwitterLinkedInLinkedInYouTubeYouTube
About
  • Services
  • Customers
Services
  • AI SEO Services
  • 15-Min LIVE Audit
  • Order SEO Audit
  • Monthly SEO Plans
  • Link Building
  • Strategy Session
Resources
  • Blog
  • Programmatic SEO Course
  • AI SEO Tracker
  • Affiliate Program
  • Ilias Ism
  • Link Building Tool
  • AgenturScout
Privacy PolicyTerms of ServiceImprint

© 2025 Stardrop GmbH. MagicSpace SEO® is a registered Swiss trademark (No. 829062).

MAGICSPACE
WRITTEN BY
Ilias Ism
Ilias Ism@illyism
  • •Built and sold Officient ($500k+ MRR)
  • •20+ products generating $10M+ revenue
  • •1000+ students taught SEO

Programmatic SEO with Next.js: The Complete Tutorial

Ilias IsmIlias Ism
Last updated:Feb 19, 2025
Blog/SEO/
You'll learn how to build a scalable programmatic SEO site using Next.js. We'll cover everything from initial setup to deployment, with a focus on SEO best practices and performance optimization.
⚡ In this tutorial, you'll learn:
  • Setting up a Next.js project optimized for pSEO
  • Creating dynamic routes and page templates
  • Implementing SEO metadata and sitemaps
  • Handling data sources and content generation
  • Deploying your pSEO site to production
By the end of this tutorial, you'll have a fully functional programmatic SEO site that can scale to thousands of pages while maintaining excellent performance and search engine visibility.

Introduction - Why Next.js for pSEO?

notion image
Next.js offers several advantages for programmatic SEO:
  • Server-Side Rendering (SSR) and Static Site Generation (SSG): Next.js supports both SSR and SSG, which are crucial for SEO. SSR renders pages on the server for each request, providing search engines with fully rendered HTML. SSG generates static HTML files at build time, resulting in incredibly fast page load times – a key ranking factor. For pSEO, SSG is often the ideal choice, especially for large sites.
  • Dynamic Routing: Next.js makes it incredibly easy to create dynamic routes, which are essential for generating hundreds or thousands of programmatic pages from a single template.
  • Built-in SEO Features: Next.js provides built-in components and APIs for managing SEO metadata (title tags, meta descriptions, canonical URLs), image optimization, and more.
  • API Routes: You can easily create API routes within your Next.js application to handle data fetching, form submissions, or other server-side logic.
  • React Ecosystem: Leverage the vast React ecosystem of components, libraries, and tools.
  • Developer Experience: Next.js is known for its excellent developer experience, with features like fast refresh, built-in TypeScript support, and a large, active community.
  • Vercel Deployment: Next.js is developed by Vercel, which offers seamless deployment and hosting for Next.js applications.

Project Setup - Setting up a Next.js Project

Let's start by creating a new Next.js project. You'll need Node.js (and npm or yarn) installed on your system.
  1. Create the project:
    1. This command creates a new Next.js project named my-pseo-site with TypeScript support.
      You can omit --typescript if you prefer JavaScript.
  1. Run the development server:
    1. This starts the Next.js development server, typically on http://localhost:3000. You should see the default Next.js welcome page.

Data Source - How to Get Data

notion image
For this tutorial, we'll use a simple example: creating city pages with population data.
We'll use a static JSON file as our data source.
In a real-world pSEO project, you might fetch data from a database, an API, or a CSV file (as covered in Data Sources).
Create a file named data.json in the root of your project:
We've added a slug field to each city object. This will be used for creating clean, SEO-friendly URLs.

Template Creation - Building a Page Template in Next.js

Next.js uses a file-system-based router.
Pages are created by adding React components to the app directory.
Let's create a basic page template for our city pages.
Create a file named app/city/[slug]/page.tsx:
Explanation:
  • app/city/[slug]/page.tsx: The [slug] part of the filename indicates a dynamic segment. This means that this page will handle routes like /city/new-york, /city/los-angeles, etc.
  • getCityData function: This asynchronous function fetches the city data based on the provided slug. In a real application, this function might fetch data from a database or an API.
  • generateMetadata Function: This function generates Metadata.
  • CityPage component: This is the main component for the city page. It receives the params object as a prop, which contains the dynamic segment value (the slug).
  • generateStaticParams: This crucial function is used for static site generation (SSG). It tells Next.js which paths to pre-render at build time. We fetch all cities from our data.json and return an array of objects, each with a slug property. This tells Next.js to generate pages for /city/new-york, /city/los-angeles, and /city/chicago.
  • Error Handling: The code includes basic error handling to display a "City not found" message if the requested city doesn't exist.
  • toLocaleString(): This method is used to format the population number with commas, making it more readable.

Dynamic Routing - Implementing Dynamic Routes for pSEO Pages

The app/city/[slug]/page.tsx file already handles dynamic routing. Next.js automatically creates routes based on the file structure.
The [slug] part of the filename acts as a placeholder for the city's slug.
The params object passed to the component contains the value of the slug.

Metadata Generation - Dynamically Generating SEO Metadata

The generateMetadata function, as you can see in app/city/[slug]/page.tsx allows you to dynamically generate SEO metadata (title tags, meta descriptions, canonical URLs) for each page.
This is essential for programmatic SEO, as you need unique metadata for each page.
We use the city data to create a unique title and description for each page. We're also setting a self-referencing canonical URL.

Sitemap Generation - Creating a Dynamic sitemap.xml

To create a dynamic sitemap.xml file in Next.js, create a file named app/sitemap.ts (or .js if you're not using TypeScript) in the root of your app directory:
Explanation:
  • MetadataRoute: This type is imported from Next and it tells that it will return route metadata.
  • data.json: We import our data file directly. In a real application, you'd likely fetch this data from a database or API.
  • baseUrl: Replace "<https://example.com>" with your actual website's base URL.
  • cityRoutes: We map over the cities array to create an array of sitemap entries, one for each city page. We're using the slug to generate the URL.
  • lastModified: We're using the current date as a placeholder. Ideally, you should use the actual last modification date of the content. If your data source includes a last modified timestamp, use that. If you're using a CMS, it might provide this information automatically.
  • changeFrequency and priority: These values can be adjusted.
  • Root Route (/): We manually add an entry for the homepage (/).
  • Array Concatenation: We use the spread operator (...) to combine the homepage entry with the city page entries.
  • as const: This is a TypeScript feature (const assertions) to make the array read-only.
Next.js will automatically serve this file at /sitemap.xml.
You should submit this URL to Google Search Console.
Important Considerations for Larger Sites:
  • Sitemap Index Files: If you have more than 50,000 URLs or your sitemap file exceeds 50MB, you'll need to create multiple sitemaps and a sitemap index file.
  • Incremental Updates: For very large sites, regenerating the entire sitemap on every change can be inefficient.
  • Consider implementing a system for incremental sitemap updates, where you only update the entries for pages that have changed.

Deployment - Deploying the Next.js pSEO Site

The easiest way to deploy a Next.js application is to use Vercel, the company that created Next.js.
  1. Create a Vercel account: Sign up for a free Vercel account at https://vercel.com/.
  1. Connect your Git repository: Connect your Vercel account to your Git repository (GitHub, GitLab, or Bitbucket).
  1. Deploy your project: Vercel will automatically detect that it's a Next.js project and configure the build and deployment settings. You can usually just accept the defaults.
  1. Set up a custom domain (optional).
Vercel will automatically build and deploy your site whenever you push changes to your Git repository. It also provides features like preview deployments, serverless functions, and edge caching. Other deployment options include Netlify, AWS Amplify, and self-hosting on a server.

Conclusion - Recap and Next Steps

In this tutorial, you've learned how to build a basic programmatic SEO site with Next.js. We covered:
  • Setting up a Next.js project.
  • Creating a page template.
  • Using dynamic routes to generate multiple pages from a single template.
  • Fetching data from a JSON file (in a real application, you'd likely use a database or API).
  • Generating dynamic SEO metadata.
  • Creating a dynamic sitemap.xml file.
  • Deploying your site to Vercel.
This is just the beginning!
To build a truly powerful pSEO site, you'll want to explore more advanced topics, such as:
  • Fetching data from external APIs.
  • Using a database (e.g., PostgreSQL, MongoDB).
  • Implementing user authentication and authorization.
  • Adding more complex content and features to your pages.
  • Optimizing for performance and scalability.
  • Using AI to generate or enhance content.
For a deeper dive into programmatic SEO, including advanced techniques, real-world case studies, and access to our supportive community, consider purchasing the full Programmatic SEO Course:
👉 Get the Full Programmatic SEO Course
This tutorial provides a solid foundation for building your own programmatic SEO projects with Next.js. Experiment, explore, and have fun building!
StoryChief traffic growth
SEO for Startups

50+ founders have grown their SEO traffic. You next?

SEO AuditMonthly SEO PlansLink BuildingStrategy Session
15-Min LIVE Audit
Avatar 1Avatar 2Avatar 3Avatar 4Avatar 5
50+
MagicSpace Team - Ilias & Sasha
🇨🇭 Based in Switzerland

We've helped 20+ products generate $10M+ through SEO

Work with an award-winning team of SEO founders

SEO AuditMonthly SEO PlansLink BuildingStrategy Session
15-Min LIVE Audit

Or get a recorded SEO audit • Both include next steps + intro to our services

MagicSpace Team - Ilias & Sasha
Dima Rubanov avatar
MagicSpace provided an incredible SEO roadmap.
Co-founder at Oscar Stories
Harshdip avatar
MagicSpace gave us an amazing SEO roadmap!
Co-founder
Winstyn Sayles avatar
Trust me, working with Magicspace is a smart move!
Founder at SalePier
Said Aitmbarek avatar
Invaluable SEO recommendations!
Founder at Stimpack
Nico avatar
They make a real difference!
Founder at Talknotes
Dan Mindru avatar
I'd recommend MagicSpace in a heartbeat!
Founder of Apihustle
Boat Builder avatar
Their insights are invaluable!
Founder at LocalPortal
Nico avatar
I would highly recommend them!
Founder at Talknotes
Maryam avatar
E-commerce professionals, don't miss out—go with MagicSpace!
Fashion Director at Vurcci Dali
Rafal avatar
Don't miss out.
Founder
Brian Swichkow avatar
We're eager to partner with them again in the future.
CEO
Dima Rubanov avatar
MagicSpace provided an incredible SEO roadmap.
Co-founder at Oscar Stories
Harshdip avatar
MagicSpace gave us an amazing SEO roadmap!
Co-founder
Winstyn Sayles avatar
Trust me, working with Magicspace is a smart move!
Founder at SalePier
Said Aitmbarek avatar
Invaluable SEO recommendations!
Founder at Stimpack
Nico avatar
They make a real difference!
Founder at Talknotes
Dan Mindru avatar
I'd recommend MagicSpace in a heartbeat!
Founder of Apihustle
Boat Builder avatar
Their insights are invaluable!
Founder at LocalPortal
Nico avatar
I would highly recommend them!
Founder at Talknotes
Maryam avatar
E-commerce professionals, don't miss out—go with MagicSpace!
Fashion Director at Vurcci Dali
Rafal avatar
Don't miss out.
Founder
Brian Swichkow avatar
We're eager to partner with them again in the future.
CEO
Jason Leow avatar
Their advice was undoubtedly expert-level!
Founder of plugins.carrd.co
Hunter Strozier avatar
Straight to the point!
CEO
Anthony Vanwynsberghe avatar
I highly recommend MagicSpace for taking your e-commerce business to the next level.
Managing Director of classic-mercedes-parts.com
Johan Steneros avatar
Great stuff.
CEO at Bravemark
Ernest avatar
If there's anyone I have confidence in improving my SEO, it's MagicSpace.
Founder at Galactic Crew
Zeeshaan Lakdawala avatar
I honestly highly recommend their SEO strategies!
Co-Founder
Yanick S. avatar
Highly recommended for top-notch SEO assistance.
CEO @ bilateralstimulation.io
Kyle avatar
I've realised the difference a professional makes!
Founder at Xbase
Nico avatar
Grow with a killer custom growth strategy.
CEO
Zak Gottlieb avatar
MagicSpace is the perfect choice.
Director at Bank.Green
Alec Shaw avatar
Thanks to MagicSpace, we flawlessly hit our deadline!
Growth at Tenderize
Jason Leow avatar
Their advice was undoubtedly expert-level!
Founder of plugins.carrd.co
Hunter Strozier avatar
Straight to the point!
CEO
Anthony Vanwynsberghe avatar
I highly recommend MagicSpace for taking your e-commerce business to the next level.
Managing Director of classic-mercedes-parts.com
Johan Steneros avatar
Great stuff.
CEO at Bravemark
Ernest avatar
If there's anyone I have confidence in improving my SEO, it's MagicSpace.
Founder at Galactic Crew
Zeeshaan Lakdawala avatar
I honestly highly recommend their SEO strategies!
Co-Founder
Yanick S. avatar
Highly recommended for top-notch SEO assistance.
CEO @ bilateralstimulation.io
Kyle avatar
I've realised the difference a professional makes!
Founder at Xbase
Nico avatar
Grow with a killer custom growth strategy.
CEO
Zak Gottlieb avatar
MagicSpace is the perfect choice.
Director at Bank.Green
Alec Shaw avatar
Thanks to MagicSpace, we flawlessly hit our deadline!
Growth at Tenderize