nextjs - 💡(How to fix) Fix [bug] Prefixing dynamic path segments on redirects breaks typedRoutes, generating static routes instead of dynamic routes [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86877Fetched 2026-04-08 02:08:38
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Author
Timeline (top)
labeled ×2commented ×1issue_type_added ×1

Code Example

typedRoutes: true, // ensure typed routes enabled
redirects() {
  return [
    {
      source: "/c:id",
      destination: "/dashboard/customers/:id",
      permanent: true,
    },
    {
      source: "/p:id",
      destination: "/dashboard/products/:id",
      permanent: true,
    },
  ];
},

---

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.12.1
  npm: 10.5.0
  Yarn: N/A
  pnpm: 10.24.0
Relevant Packages:
  next: 16.0.7 // Latest available version is detected (16.0.7).
  eslint-config-next: N/A
  react: 19.2.1
  react-dom: 19.2.1
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/next-issue-dynamic-path-redirect-types-4kxlpc

To Reproduce

  1. Add the following redirects in your next.config.js:
typedRoutes: true, // ensure typed routes enabled
redirects() {
  return [
    {
      source: "/c:id",
      destination: "/dashboard/customers/:id",
      permanent: true,
    },
    {
      source: "/p:id",
      destination: "/dashboard/products/:id",
      permanent: true,
    },
  ];
},
  1. Run next typegen and notice the produced route lists:
<img width="537" height="163" alt="Image" src="https://github.com/user-attachments/assets/bf1c727b-9420-4532-9a71-5b77948b7a76" />
  1. Observe that routes prefixed with a dynamic path segment (e.g., /a/m:id, /a/p:id) are generated as static routes instead of dynamic routes in the type generation result.

Current vs. Expected behavior

Current behavior:

  • When dynamic path segments are prefixed in redirect source paths (such as "/a/m:id"), typedRoutes generates these as static routes (e.g., "/a/m[id]") rather than dynamic routes with appropriate parameter typing.

Expected behavior:

  • Dynamic segments in redirect sources should cause typedRoutes to generate those paths as dynamic routes with params.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.12.1
  npm: 10.5.0
  Yarn: N/A
  pnpm: 10.24.0
Relevant Packages:
  next: 16.0.7 // Latest available version is detected (16.0.7).
  eslint-config-next: N/A
  react: 19.2.1
  react-dom: 19.2.1
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Dynamic Routes, Redirects

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local)

Additional context

No response

extent analysis

TL;DR

The issue can be addressed by modifying the redirects function in next.config.js to correctly handle dynamic path segments.

Guidance

  • Review the redirects function to ensure that dynamic path segments are properly defined, potentially by using a regex pattern to match the dynamic segment.
  • Verify that the typedRoutes option is enabled, as it is required for correct type generation.
  • Check the Next.js documentation for any updates or changes to the redirects function or typedRoutes option.
  • Consider testing with a simplified redirects function to isolate the issue.

Example

redirects() {
  return [
    {
      source: /^\/c:(\d+)$/, // using regex to match dynamic segment
      destination: "/dashboard/customers/:id",
      permanent: true,
    },
    {
      source: /^\/p:(\d+)$/, // using regex to match dynamic segment
      destination: "/dashboard/products/:id",
      permanent: true,
    },
  ];
},

Notes

The provided example code snippet is a potential solution, but it may require further modification to fit the specific use case. Additionally, the issue may be related to a bug in the Next.js version being used, and upgrading to a newer version may resolve the issue.

Recommendation

Apply workaround: The issue can be addressed by modifying the redirects function to correctly handle dynamic path segments, as shown in the example code snippet. This workaround allows for correct type generation and routing.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING