nextjs - 💡(How to fix) Fix next 16.2.1 trpc error [2 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#92114Fetched 2026-04-08 01:51:56
View on GitHub
Comments
2
Participants
2
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
commented ×2issue_type_added ×1labeled ×1subscribed ×1

Error Message

Fix Action

Fix / Workaround

Additional notes:

  • Works correctly with next build && next start locally
  • Works correctly via Vercel Instant Rollback to a build from the previous day
  • Fails on every new Vercel build regardless of code changes
  • Removing the /(api|trpc)(.*) matcher from proxy.ts is a workaround but breaks other middleware behavior

Code Example

Operating System:
    Platform: darwin
    Arch: arm64
    Version: Darwin Kernel Version 25.2.0
    Available memory (MB): 16384
    Available CPU cores: 8
  Binaries:
    Node: 24.10.0
    npm: 11.6.1
    Yarn: 1.22.22
    pnpm: 10.19.0
  Relevant Packages:
    next: 16.2.1
    eslint-config-next: N/A
    react: 19.2.4
    react-dom: 19.2.4
    typescript: 5.9.3
  Next.js Config:
    output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/chagas42/next-16.2.1-trpc-error

To Reproduce

  1. Clone the minimal reproduction: https://github.com/chagas42/next-16.2.1-trpc-error

  2. Create a free Clerk app at https://clerk.com and copy the API keys

  3. In the Vercel dashboard, import the cloned repo and add the environment variables:

    • NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=<your key>
    • CLERK_SECRET_KEY=<your key>
  4. Deploy to Vercel and open the preview URL

  5. Open DevTools → Network tab → filter by Fetch/XHR

  6. Observe that all requests to /api/trpc/* return 404 Not Found

The API route at pages/api/trpc/[trpc].ts is never reached. Vercel logs show:

Middleware:  200        ← proxy.ts ran, returned NextResponse.next()
Cache:       404 Not Found
Status:      HIT
Key:         /pt-BR/404

───────────────────────────────

Current vs. Expected behavior

Expected: GET /api/trpc/hello reaches the API route handler and returns a valid response.

Observed: Every request to /api/trpc/* returns 404 Not Found on all new Vercel deployments.

Vercel logs for the failing request show:

Middleware:  200          ← middleware ran successfully, returned NextResponse.next()
Cache:       404 Not Found
Status:      HIT
Key:         /pt-BR/404   ← static i18n 404 page served instead of the API handler

The static pre-rendered 404 page for the default locale (pt-BR) is being served from Vercel's Full Route Cache for all API route paths. The route handler is never invoked.

Vercel also logs the internal path as /pt-BR/api/trpc/[route] — the i18n locale prefix is being applied to the API route, even though pages/api/ routes should be exempt from i18n locale prefixing.

Additional notes:

  • Works correctly with next build && next start locally
  • Works correctly via Vercel Instant Rollback to a build from the previous day
  • Fails on every new Vercel build regardless of code changes
  • Removing the /(api|trpc)(.*) matcher from proxy.ts is a workaround but breaks other middleware behavior

Provide environment information

Operating System:
    Platform: darwin
    Arch: arm64
    Version: Darwin Kernel Version 25.2.0
    Available memory (MB): 16384
    Available CPU cores: 8
  Binaries:
    Node: 24.10.0
    npm: 11.6.1
    Yarn: 1.22.22
    pnpm: 10.19.0
  Relevant Packages:
    next: 16.2.1
    eslint-config-next: N/A
    react: 19.2.4
    react-dom: 19.2.4
    typescript: 5.9.3
  Next.js Config:
    output: N/A

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

Connection

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

Vercel (Deployed)

Additional context

Only reproducible on Vercel (preview and production deployments). Works correctly with next build && next start locally.

The issue appears to be related to the combination of:

  • i18n with a non-English defaultLocale (pt-BR)
  • Middleware using proxy.ts filename (Next.js 16 feature) with a matcher that includes /(api|trpc)(.*)
  • Clerk middleware + @vercel/functions geolocation

Vercel logs show the middleware runs successfully (200) but the request is served from the Full Route Cache with key /pt-BR/404 — the static i18n 404 page — instead of reaching the API route handler. The internal path logged by Vercel shows /pt-BR/api/trpc/[route], suggesting the i18n locale prefix is incorrectly being applied to API routes.

An Instant Rollback to a deployment built the previous day restores correct behavior. Every new build fails.

extent analysis

Fix Plan

To resolve the issue, we need to prevent the i18n locale prefix from being applied to API routes. We can achieve this by modifying the next.config.js file to exclude API routes from i18n routing.

Step-by-Step Solution

  1. Update next.config.js: Add the following code to your next.config.js file:
module.exports = {
  //... other configurations ...
  i18n: {
    locales: ['pt-BR', 'en'],
    defaultLocale: 'pt-BR',
    localeDetection: false,
  },
  experimental: {
    // Exclude API routes from i18n routing
    i18nSubdomains: false,
  },
}

However, since the issue seems related to the combination of i18n, middleware, and Vercel deployment, we need a more specific solution.

  1. Modify proxy.ts: Update the proxy.ts file to handle API routes differently:
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
  const url = new URL(request.url);
  if (url.pathname.startsWith('/api/')) {
    // Handle API routes without i18n prefix
    return NextResponse.next();
  }
  //... other middleware logic ...
}
  1. Verify Vercel Configuration: Ensure that your Vercel configuration is set up correctly, especially the environment variables for Clerk and the next.config.js settings.

Verification

To verify that the fix worked:

  1. Redeploy your application to Vercel.
  2. Open the DevTools → Network tab → filter by Fetch/XHR.
  3. Observe that requests to /api/trpc/* now reach the API route handler and return a valid response.

Extra Tips

  • Make sure to test your application thoroughly after applying these changes.
  • If issues persist, try rolling back to a previous deployment and re-applying the changes.
  • Consider reaching out to Vercel support for further assistance with their platform-specific configurations.

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