Dynamic routes use square brackets like [id] to capture URL parts.
Examples
The file page.tsx inside app folder creates the home page route.
NextJS
app/page.tsx
// This file renders the home page at '/'
Adding a folder about with page.tsx creates the '/about' page automatically.
NextJS
app/about/page.tsx
// This file renders the about page at '/about'
The folder [id] captures any value in the URL after '/blog/' as a parameter.
NextJS
app/blog/[id]/page.tsx
// This file renders dynamic blog posts at '/blog/[id]'
Sample Program
This example shows three pages created by files. The home page at '/', the about page at '/about', and dynamic blog posts at '/blog/[id]'. The dynamic page reads the URL part as id and shows it.
File names and folders directly control the website URLs, so naming is important.
Dynamic routes let you handle many similar pages without writing extra code.
File-based routing reduces mistakes because you don't write route code manually.
Summary
File-based routing matches files to website pages automatically.
It makes adding or changing pages as easy as creating or deleting files.
Dynamic routes let you handle many pages with one file using URL parameters.
Practice
(1/5)
1. What is the main benefit of file-based routing in Next.js?
easy
A. It only works with static HTML files, not React components.
B. It automatically creates routes based on files in the pages folder.
C. It requires manual configuration of each route in a separate file.
D. It disables dynamic URL parameters for pages.
Solution
Step 1: Understand file-based routing concept
File-based routing means Next.js uses the file names in the pages folder to create website routes automatically.
Step 2: Compare options with this concept
It automatically creates routes based on files in the pages folder. matches this idea exactly. Options A, B, and D contradict the automatic and dynamic nature of file-based routing.
Final Answer:
It automatically creates routes based on files in the pages folder. -> Option B
Quick Check:
File-based routing = automatic routes [OK]
Hint: File names equal routes in Next.js [OK]
Common Mistakes:
Thinking routes need manual setup
Confusing static files with React components
Believing dynamic URLs are not supported
2. Which file name correctly creates a dynamic route for a user profile page in Next.js?
easy
A. userId.js
B. profile.js
C. user-profile.jsx
D. [userId].js
Solution
Step 1: Identify dynamic route syntax in Next.js
Dynamic routes use square brackets around the parameter name, like [param].js.
Step 2: Match options to this syntax
Only [userId].js uses square brackets correctly to create a dynamic route for userId.
Final Answer:
[userId].js -> Option D
Quick Check:
Dynamic route syntax = [param].js [OK]
Hint: Dynamic routes use [param].js file names [OK]
Common Mistakes:
Using plain file names without brackets
Confusing file extensions
Missing brackets around parameter
3. Given these files in the pages folder: index.js, about.js, [postId].js, what URL paths will Next.js automatically create?
medium
A. /, /about, /:postId (dynamic route)
B. /index, /about, /[postId]
C. /, /about, /postId
D. /home, /about-us, /post
Solution
Step 1: Understand file to route mapping
index.js maps to root '/', about.js maps to '/about', and [postId].js creates a dynamic route '/:postId'.
Step 2: Evaluate options for correct paths
/, /about, /:postId (dynamic route) correctly shows '/' for index, '/about' for about.js, and dynamic '/:postId' for [postId].js. Others are incorrect or literal.
Final Answer:
/, /about, /:postId (dynamic route) -> Option A
Quick Check:
File names map to routes including dynamic [param] [OK]
Assuming file names map exactly without dynamic syntax
4. You created a file named user.js inside the pages folder to handle user profiles, but navigating to /123 shows a 404 error. What is the likely problem?
medium
A. Next.js does not support nested routes.
B. The file extension must be .jsx, not .js.
C. The file should be named [userId].js to handle dynamic user IDs.
D. You need to add a route manually in a config file.
Solution
Step 1: Understand dynamic route handling
To handle URLs like /123, the file must use dynamic routing syntax with brackets, e.g., [userId].js.
Step 2: Check options for correct fix
The file should be named [userId].js to handle dynamic user IDs. correctly identifies the need for a dynamic route file name. Options A, B, and C are incorrect because Next.js supports .js files, automatic routing, and nested routes.
Final Answer:
The file should be named [userId].js to handle dynamic user IDs. -> Option C
Quick Check:
Dynamic routes need [param].js file names [OK]
Hint: Use [param].js for dynamic URLs [OK]
Common Mistakes:
Using static file names for dynamic URLs
Thinking manual route config is needed
Confusing file extensions
5. You want to create a blog with categories and posts using file-based routing. Which folder and file structure correctly supports URLs like /blog/technology/123 where technology is a category and 123 is a post ID?
hard
A. /pages/blog/[category]/[postId].js
B. /pages/blog/category/postId.js
C. /pages/blog/[category-postId].js
D. /pages/blog/category/[postId].js
Solution
Step 1: Understand nested dynamic routes
To handle URLs with multiple dynamic parts, Next.js uses nested folders with bracketed file names for each parameter.
Step 2: Match structure to URL pattern
/pages/blog/[category]/[postId].js uses nested folders with [category] and [postId].js, matching /blog/technology/123 correctly. Other options do not match dynamic nesting properly.
Final Answer:
/pages/blog/[category]/[postId].js -> Option A
Quick Check:
Nested dynamic folders = nested URL params [OK]
Hint: Use nested [param] folders for multi-level dynamic routes [OK]
Common Mistakes:
Putting multiple params in one file name
Using static folder names for dynamic parts
Not nesting folders for URL segments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.