The Hidden Gotcha with Dynamic API Routes in Nuxt 3 (Nitro) That Drives Developers Crazy
If you’ve built more than a toy API in Nuxt 3, you’ve probably run into this frustrating situation:
The Hidden Gotcha with Dynamic API Routes in Nuxt 3 (Nitro) That Drives Developers Crazy

Generated by AI
If you’ve built more than a toy API in Nuxt 3, you’ve probably run into this frustrating situation:
One dynamic route works perfectly.
You add another endpoint (or a sub-folder) under the same dynamic segment — and suddenly you get a 404 Not Found.
You double-check the file name. Restart the dev server. Clear caches. Nothing changes.
Then, almost magically, when you remove a sub-folder, everything starts working again.
Sound familiar?
You’re not alone — and it’s not random, and it’s not your fault.
Why This Happens: How Nitro Resolves Routes
Nuxt 3 uses Nitro as its server engine. Nitro generates API routes from your file system and builds an internal route map that is used at runtime to match incoming requests.
At a high level, the process looks simple:
File structure → Generated route tree → Request matching
But the important detail is this:
Route matching is based on specificity and path structure, not just how files are organized.
In practice, this means:
- Dynamic segments like
[id]act as flexible placeholders - More specific routes are evaluated differently than generic patterns
- When multiple routes exist at different nesting levels, the final match depends on how the router resolves the available candidates
This is where things can start to feel confusing in real projects.
When you mix:
- dynamic segments (
[id]) - nested folders under those segments
- and additional sibling routes at different depths
…the router may resolve a different matching candidate than the one you expect. Some routes appear valid but are never actually reached during request matching, resulting in 404 responses.
It’s not that the route is missing — it’s that it is not being selected.
Real-World Impact
A very common structure starts out looking clean and intuitive:
server/api/
└── resources/
└── [id]/
├── details.get.ts
├── comments.get.ts
└── related/
└── [id].get.ts
This feels natural: everything related to a resource lives under its ID.
But as the API grows, developers often start noticing:
- Some endpoints work perfectly
- Others return 404
- Moving files changes behavior unexpectedly
- Removing a folder “fixes” the issue
At that point, it starts to feel like the framework is unstable — but the root cause is usually the complexity of nested dynamic routing, not a bug.
Better Ways to Structure Nuxt 3 API Routes
The key takeaway is simple:
Avoid overloading dynamic folders with deep or inconsistent nesting.
Instead, structure your API in a way that keeps route resolution predictable.
1. Resource-First Grouping (Recommended)
Instead of deeply nesting everything under a single [id] folder, group endpoints by responsibility:
server/api/
├── resources/
│ └── [id].get.ts
├── resources-details/
│ └── [id].get.ts
├── resources-comments/
│ └── [id].get.ts
└── resources-related/
└── [id].get.ts
Why this works better
- Reduces deep nesting inside dynamic segments
- Keeps route structure easier to reason about
- Avoids ambiguous resolution cases
- Scales more predictably in larger APIs
This approach favors clarity over hierarchical purity.
2. Safe Nesting (Classic REST Style)
If you prefer traditional REST-style URLs like:
/api/resources/123/comments
You can still use nested routing — but with constraints.
Safe structure:
server/api/
└── resources/
└── [id]/
├── details.get.ts
├── comments.get.ts
└── settings.get.ts
Rules to keep it stable:
- Keep nesting shallow and consistent
- Avoid mixing unrelated concepts inside the same
[id]folder - Don’t build deeply nested trees under dynamic segments
- Prefer explicit route files over complex folder hierarchies
This keeps route resolution predictable and reduces surprises.
When Things Start to Break Down
Most issues don’t appear in small projects.
They usually show up when:
- API grows organically over time
- multiple developers add routes independently
- folder structure becomes deeply nested
- dynamic segments are reused in different contexts
At that point, routing behavior can feel inconsistent, even though it is following deterministic rules internally.
The challenge is that those rules are not obvious from the folder structure alone.
Final Thoughts
Nuxt 3’s file-based routing is one of its best features. It makes API development fast, intuitive, and enjoyable — especially for small and medium projects.
But as your application grows, especially with dynamic and nested routes, structure matters more than convenience.
The key lesson is:
Design your API structure for clarity and predictability, not just file organization.
In many cases, flattening or segmenting routes by feature leads to fewer surprises than deeply nested dynamic structures.
Have you experienced this?
Have you ever encountered “mysterious” 404s in Nuxt 3 dynamic routes?
What structure has worked best for your projects — nested or flat?
Share your experience in the comments.
메타데이터
- post_id
- daae593c8eef
- slug
- the-hidden-gotcha-with-dynamic-api-routes-in-nuxt-3-nitro-that-drives-developers-crazy-daae593c8eef
- url
- https://medium.com/@Roy.Wong/the-hidden-gotcha-with-dynamic-api-routes-in-nuxt-3-nitro-that-drives-developers-crazy-daae593c8eef
- canonical_url
- https://medium.com/@Roy.Wong/the-hidden-gotcha-with-dynamic-api-routes-in-nuxt-3-nitro-that-drives-developers-crazy-daae593c8eef
- author_url
- https://medium.com/@Roy.Wong
- status
- ok
- fetched_at
- 2026-06-16 19:09:56