Skip to content

fix(ai-gateway): set maxDuration on /api/gateway/[...path] route - #4758

Merged
chrarnoldus merged 2 commits into
mainfrom
fix/gateway-route-max-duration
Jul 25, 2026
Merged

fix(ai-gateway): set maxDuration on /api/gateway/[...path] route#4758
chrarnoldus merged 2 commits into
mainfrom
fix/gateway-route-max-duration

Conversation

@chrarnoldus

@chrarnoldus chrarnoldus commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

api/gateway/[...path]/route.ts re-exports POST from the openrouter catch-all route, but Next.js route segment config is not inherited through a handler re-export — it must be exported from the route's own file. Without its own maxDuration export, /api/gateway/* fell back to the Vercel plan default (60s Hobby / 300s Pro) instead of the 1800s that the identical /api/openrouter/* handler gets, so long-running streaming requests through /api/gateway timed out early.

The fix adds export const maxDuration = 1800; to the gateway route file, matching api/openrouter/[...path]/route.ts.

Why not re-export maxDuration too?

export { POST, maxDuration } from '...' does not work for route segment config. Next.js extracts segment config at build time via static SWC analysis (extractExportedConstValue in next/dist/build/analysis/extract-const-value.js), which only matches an ExportDeclaration containing a const variable with a literal initializer — i.e. export const maxDuration = 1800 written directly in the route file itself. A re-export parses as an ExportNamedDeclaration, which the extractor skips, so the value is silently ignored. Vercel then derives the function timeout from Next.js's build-time functions config manifest, so a re-export that is perfectly valid at runtime never reaches the deployed function configuration and the route falls back to the plan default. Verified against the installed Next.js 16.2.6.

Verification

  • Not manually tested — one-line config export; relying on CI and Vercel deployment behavior. The openrouter route's validatePath already accepts both /api/gateway and /api/openrouter prefixes, confirming these paths are intended aliases of the same handler.

Visual Changes

N/A

The gateway route re-exports POST from the openrouter catch-all route,
but Next.js route segment config is not inherited through a handler
re-export. Without its own maxDuration export, /api/gateway/* fell back
to the Vercel plan default instead of the 1800s the identical
/api/openrouter/* handler gets, so long-running streaming requests
through /api/gateway timed out early.

Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
@chrarnoldus chrarnoldus self-assigned this Jul 24, 2026
@kilo-code-bot

kilo-code-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

The new commit only removes the explanatory comment above maxDuration; the functional export const maxDuration = 1800; line is unchanged and still correctly mirrors the sibling /api/openrouter/[...path]/route.ts handler.

Files Reviewed (1 files)
  • apps/web/src/app/api/gateway/[...path]/route.ts
Previous Review Summary (commit 7df82b8)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 7df82b8)

Status: No Issues Found | Recommendation: Merge

Executive Summary

Single-line config addition correctly mirrors the maxDuration export in the sibling /api/openrouter/[...path]/route.ts handler it re-exports from, with a clear comment documenting the required sync.

Files Reviewed (1 files)
  • apps/web/src/app/api/gateway/[...path]/route.ts

Reviewed by claude-sonnet-5 · Input: 20 · Output: 2.8K · Cached: 389K

Review guidance: REVIEW.md from base branch main

Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
@chrarnoldus
chrarnoldus merged commit c2d6326 into main Jul 25, 2026
15 checks passed
@chrarnoldus
chrarnoldus deleted the fix/gateway-route-max-duration branch July 25, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants