Serve props JSON for /api data requests via middleware rewrite - #902
Conversation
The /api/:path* -> /ipa/:path* rewrite in next.config.mjs is applied by Vercel's routing to client-side props fetches (/_next/data/<buildId>/api/....json), but the data-request context is lost and the prerendered page HTML is returned instead of JSON (vercel/next.js#39669). The router then never receives pageProps, so the API sidebar stays collapsed and the tab title shows undefined until a full reload. This half of the bug only occurs on Vercel infrastructure; the dev server and next start resolve rewrites for data requests correctly, and #900 fixed only the /ipa redirect half. Middleware rewrites preserve data-request semantics, so rewrite /api/* to /ipa/* in middleware for data requests only (x-nextjs-data header). Regular page requests fall through to the existing config rewrites, and the /ipa -> /api canonical redirect is unchanged.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Review limit reached
Next review available in: 52 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. 📝 WalkthroughWalkthroughAdded Next.js middleware that rewrites client-side ChangesAPI data routing
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/middleware.js`:
- Around line 27-37: Update the `/_next/data/` handling in the middleware so the
bare `/_next/data/<build>/api.json` path maps to
`/_next/data/<build>/ipa/introduction.json` before the generic `/api`
replacement runs. Preserve the existing generic mapping for data requests with
additional path segments and the separate bare `/api` behavior.
- Around line 1-43: Move the data-request rewrite logic from middleware and its
matcher configuration into the existing src/proxy.js, removing
src/middleware.js. Preserve the /api and /_next/data API-to-IPA rewrite behavior
and data-request detection, and expose the handler through a named proxy export
compatible with Next.js 16.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: b1e7546d-8e06-4988-aa72-b986cb0993cb
📒 Files selected for processing (1)
src/middleware.js
Next 16 uses proxy.js and rejects builds where both middleware.js and proxy.js exist; this repo already had src/proxy.js for the /docs-static/_next asset rewrite. Fold the /api -> /ipa data-request rewrite into it and drop middleware.js. Verified the proxy intercepts: data responses now carry x-middleware-rewrite: /ipa/... and JSON bodies.
The raw data-path fallback rewrote /_next/data/<build>/api.json to /_next/data/<build>/ipa.json, but there is no /ipa index page; mirror the /api -> /ipa/introduction rewrite instead.
Problem
#900 fixed one of two causes of the API sidebar not expanding (and the tab title showing
undefined) on client-side navigation. The behavior persisted in production because of a second, Vercel-only cause.Cause
On client-side navigation the router fetches page props from
/_next/data/<buildId>/api/....json. Vercel's routing applies the/api/:path*→/ipa/:path*rewrite fromnext.config.mjsto these requests but loses the data-request context, returning the prerendered page HTML instead of the props JSON (vercel/next.js#39669, closed unresolved). The router never receivespageProps, so the sidebar method list stays collapsed and the title isundefineduntil a full reload. The dev server andnext startresolve rewrites for data requests correctly, which is why this half only reproduces on Vercel infrastructure.Fix
Add
src/middleware.jsthat rewrites/api/*to/ipa/*for data requests only (identified by thex-nextjs-dataheader, matcher scoped to/apipaths). Middleware rewrites preserve data-request semantics, so these requests now return the props JSON. Regular page requests fall through to the existing config rewrites, and the/ipa→/apicanonical redirect is unchanged.Verification
/_next/data/<buildId>/api/resources/networks.jsonand/_next/data/<buildId>/api.jsonreturn props JSON;/ipa-prefixed data URLs unchanged/ipa/*visits still redirect to/api/*Since the remaining cause is Vercel-routing-specific, please confirm on the preview deployment before merging.
Summary by CodeRabbit