Statically generate dynamic routes at build time in Next.js 14

February 11th, 20245 mins read

Statically generate dynamic routes at build time in Next.js 14

Over the years, Next.js has continuously evolved, introducing and refining features to enhance web development. If you've worked with earlier versions of Next.js and have recently started using Next.js 14 or the App router, you'll encounter some changes, such as in generating static paths from dynamic routes.

I recently initiated a Next.js project that included a dynamic route for fetching blog posts from an API. While everything functioned smoothly, I encountered an issue upon attempting to build my project by deploying it on the Kinsta's static site hosting:

I use the Kinsta static site hosting for my static site projects because it offers the capability to deploy up to 100 sites at no cost. Additionally, it provides a secure URL for each deployed site.

The error message, "Error: out directory does not exist," implies that the deployment process is searching for a directory named 'out', which should contain the built version of your Next.js project, but fails to locate it. This directory is typically generated upon executing the next build command.

This issue arises because the project has a dynamic route that dynamically generates pages at the time of request (SSR) and aims to pre-render them for static hosting.

Generating Static Paths for dynamic routes in Next.js 12 and earlier

In previous versions of Next.js, you could generate static paths for dynamic routes using the getStaticPaths function. This function is used to generate a list of paths to pre-render based on data. It is typically used with dynamic routes to pre-render a page for each path. Here's an example of how to use getStaticPaths:

export async function getStaticPaths() {
	const response = await fetch('https://jsonplaceholder.typicode.com/posts');
	const posts = await response.json();

	const paths = posts.map((post) => ({
		params: { postId: post.id.toString() },
	}));

	return { paths, fallback: false };
}

In this example, the getStaticPaths function fetches posts from an API and maps each post to a params object containing the postId. This object is then returned as an array of paths to pre-render.

The fallback key is set to false to ensure that the page is pre-rendered for each path. If the fallback key is set to true, the page is pre-rendered for paths specified in the paths array and other paths are generated on-demand at request time.

But with the introduction of Next.js 14, the getStaticPaths function is no longer used to generate static paths for dynamic routes. Instead, the generateStaticParams function is used to generate a list of params for dynamic routes.

Using generateStaticParams to generate static paths for dynamic routes in Next.js 13 and 14

The generateStaticParams function is used to generate a list of params for dynamic routes. It is typically used with dynamic routes to pre-render a page for each path. Here's an example of how to use generateStaticParams:

export async function generateStaticParams() {
	const response = await fetch('https://jsonplaceholder.typicode.com/posts');
	const posts = await response.json();

	return posts.map((post) => ({
		postId: post.id.toString(),
	}));
}

In this example, the generateStaticParams function fetches posts from an API and maps each post to a params object containing the postId. This object is then returned as an array of params to pre-render.

The generateStaticParams function executes prior to the generation of corresponding Layouts or Pages. It is used to generate static paths for dynamic routes at build time, rather than on-demand at request time.

This approach ensures that when you build your Next.js project, routes are pre-rendered at build time, and HTML files for each post are generated accordingly.

When using the generateStaticParams function, ensure that the function is exported from the file containing the dynamic route. The function should be named generateStaticParams and should return an array of params objects. The params object should contain the keys and values that correspond to the dynamic route parameters.

Also, ensure you enable static exports in your Next.js project. Static exports allow you to generate static HTML files for your application, which can be deployed to a static site hosting platform.

In your Next.js project, you can enable static exports by changing the output mode inside next.config.js:

const nextConfig = {
	output: 'export',
};

When you enable static exports in your Next.js project, the next build command generates an out folder containing the HTML/CSS/JS assets for your application. This folder can be deployed to a static site hosting platform.

Wrapping up

In this article, I've explained how to generate static paths for dynamic routes in Next.js 14 using the generateStaticParams function. This function is used to generate a list of params for dynamic routes and is typically used with dynamic routes to pre-render a page for each path.

There is more to rendering in Next.js, but I hope this article helps you understand how to generate static files for dynamic routes with the generateStaticParams function.


Intersted in seeing some videos? Check out my recent uploads.

How to SSH into any remote server directly from VS Code
07:07

November 5th, 2025

How to SSH into any remote server directly from VS Code

What Are MCPs? I Used Cursor to Create a Repo and Push Code to GitHub
25:55

October 8th, 2025

What Are MCPs? I Used Cursor to Create a Repo and Push Code to GitHub

Understanding Docker Multi-Stage Builds (with Example) - Fast and Efficient Dockerfiles
21:26

September 1st, 2025

Understanding Docker Multi-Stage Builds (with Example) - Fast and Efficient Dockerfiles

3 Tips to Optimize Docker Images | Reduce Size, Improve Performance & Security
19:04

August 22nd, 2025

3 Tips to Optimize Docker Images | Reduce Size, Improve Performance & Security

Passwordless Auth in Node.js with Magic Links (Step-by-Step)
26:47

July 7th, 2025

Passwordless Auth in Node.js with Magic Links (Step-by-Step)

One Security Check Most Devs Forget in Their Signup Flow
10:11

May 20th, 2025

One Security Check Most Devs Forget in Their Signup Flow

How to Implement OAuth in Your Node.js Backend (GitHub & Google Login)
30:03

May 15th, 2025

How to Implement OAuth in Your Node.js Backend (GitHub & Google Login)

Next.js JWT Auth Explained: Cookies, Refresh Tokens & 2FA (No Auth.js)
01:02:51

May 6th, 2025

Next.js JWT Auth Explained: Cookies, Refresh Tokens & 2FA (No Auth.js)

Add 2FA to Your Node.js App with otplib
44:19

April 6th, 2025

Add 2FA to Your Node.js App with otplib

How to Run PostgreSQL with Docker Locally
25:48

March 31st, 2025

How to Run PostgreSQL with Docker Locally

Node.js Auth API with JWT, PostgreSQL & Prisma | Vibe Coding
01:23:24

March 30th, 2025

Node.js Auth API with JWT, PostgreSQL & Prisma | Vibe Coding

Create Stunning Emails in React — React-Email & Resend
33:53

March 6th, 2025

Create Stunning Emails in React — React-Email & Resend

Complete Guide — React Internationalization (i18n) with i18next
56:15

February 17th, 2025

Complete Guide — React Internationalization (i18n) with i18next

Build an Animated FAQ Accordion Component in React and Tailwind CSS
30:25

February 8th, 2025

Build an Animated FAQ Accordion Component in React and Tailwind CSS

Learn Shell Scripting With Me Using ChatGPT
47:12

January 30th, 2025

Learn Shell Scripting With Me Using ChatGPT

How to Build a Mega Menu in Next.js – Step-by-Step Tutorial
36:33

January 20th, 2025

How to Build a Mega Menu in Next.js – Step-by-Step Tutorial

The Easiest Way to Make Your Footer Stick to the Bottom || CSS Grid or Flexbox
11:00

January 13th, 2025

The Easiest Way to Make Your Footer Stick to the Bottom || CSS Grid or Flexbox

Form Validation in Next.js with React Hook Form & Zod
27:47

October 31st, 2024

Form Validation in Next.js with React Hook Form & Zod

Styling Active Links in Next.js
08:31

January 20th, 2024

Styling Active Links in Next.js

What is Bun — A JavaScript All-in-one Toolkit
29:01

January 17th, 2024

What is Bun — A JavaScript All-in-one Toolkit

Copy and Paste with JavaScript — 7 lines of code 🔥
01:00

December 27th, 2023

Copy and Paste with JavaScript — 7 lines of code 🔥

Migrate a Node.js Project to use Bun — 60 seconds ⚡️
01:00

December 25th, 2023

Migrate a Node.js Project to use Bun — 60 seconds ⚡️

Build a Slackbot With Slack API and Node.js
38:16

December 19th, 2023

Build a Slackbot With Slack API and Node.js

Technical Writing & Technical Blogging [w/ Asaolu Elijah]
17:58

December 30th, 2020

Technical Writing & Technical Blogging [w/ Asaolu Elijah]