Skip to content

Serve props JSON for /api data requests via middleware rewrite - #902

Merged
SunsetDrifter merged 3 commits into
mainfrom
docs/fix-api-data-requests-middleware
Aug 3, 2026
Merged

Serve props JSON for /api data requests via middleware rewrite#902
SunsetDrifter merged 3 commits into
mainfrom
docs/fix-api-data-requests-middleware

Conversation

@SunsetDrifter

@SunsetDrifter SunsetDrifter commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

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 from next.config.mjs to 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 receives pageProps, so the sidebar method list stays collapsed and the title is undefined until a full reload. The dev server and next start resolve rewrites for data requests correctly, which is why this half only reproduces on Vercel infrastructure.

Fix

Add src/middleware.js that rewrites /api/* to /ipa/* for data requests only (identified by the x-nextjs-data header, matcher scoped to /api paths). 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/api canonical redirect is unchanged.

Verification

  • /_next/data/<buildId>/api/resources/networks.json and /_next/data/<buildId>/api.json return props JSON; /ipa-prefixed data URLs unchanged
  • Client-side navigation between API pages expands the sidebar methods and sets the correct tab title
  • Docs-section navigation and docs → API cross-navigation unaffected
  • Plain page requests still return HTML; /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

  • New Features
    • Added seamless routing for client-side data requests.
    • API requests are now transparently mapped to the appropriate content paths, including a default introduction page.
    • Non-data requests and unchanged paths continue to work as expected.

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.
@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview Aug 3, 2026 11:42am

Request Review

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@SunsetDrifter, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e5f82f85-ad80-403e-b5d9-182677998851

📥 Commits

Reviewing files that changed from the base of the PR and between 36af558 and 037534b.

📒 Files selected for processing (1)
  • src/proxy.js
📝 Walkthrough

Walkthrough

Added Next.js middleware that rewrites client-side /api data requests to /ipa paths. It supports normalized and raw /_next/data/ requests, maps bare /api to /ipa/introduction, and passes through unchanged requests.

Changes

API data routing

Layer / File(s) Summary
Middleware request rewriting
src/middleware.js
Adds exported matchers and middleware. The middleware rewrites eligible /api and /_next/data/ paths, maps bare /api to /ipa/introduction, and preserves unchanged requests.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • netbirdio/docs#900: Both changes modify client-side data request routing between /api and /ipa.

Poem

A rabbit checks the paths at night,
/api hops to /ipa right.
Data requests keep their trail,
Bare introductions never fail.
Middleware twitches whiskers bright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the middleware rewrite that serves props JSON for /api data requests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/fix-api-data-requests-middleware

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between e5da16c and 36af558.

📒 Files selected for processing (1)
  • src/middleware.js

Comment thread src/middleware.js Outdated
Comment thread src/middleware.js Outdated
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.
@SunsetDrifter
SunsetDrifter merged commit c5e61c6 into main Aug 3, 2026
5 checks passed
@SunsetDrifter
SunsetDrifter deleted the docs/fix-api-data-requests-middleware branch August 3, 2026 12:40
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