fix(shims): parse Pages data build IDs - #2806
Conversation
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4c655af23a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| const paths = pathname.slice("/_next/data/".length, -".json".length).split("/"); | ||
| this._buildId = paths[0]; | ||
| const nextDataPathname = paths[1] !== "index" ? `/${paths.slice(1).join("/")}` : "/"; |
There was a problem hiding this comment.
Preserve index-prefixed routes when parsing data URLs
When a valid Pages route starts with /index, buildPagesDataPath intentionally adds an extra index/ prefix, so /index/foo becomes /_next/data/id/index/index/foo.json. This condition treats every first post-build segment named index as the root, causing that URL's NextURL.pathname to become / and its formatted href or clone to address the root endpoint instead of /index/foo. Only an exact remaining path of index should map to /; apply the same index-prefix denormalization already implemented by parseNextDataPathname.
AGENTS.md reference: AGENTS.md:L174-L178
Useful? React with 👍 / 👎.
Performance benchmarksCompared 0 improved · 0 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
Summary
NextURL.buildIdrepresent the build ID encoded in a Pages Router/_next/data/URLskipProxyUrlNormalizeis enabledUser impact and reproduction
Before this change, an ordinary request such as
/reported the running application's__VINEXT_BUILD_ID, even though the URL carried no data-request identity. Conversely,/_next/data/request-build/about.jsondid not exposerequest-buildor the page pathname/about.That breaks middleware and other code using
request.nextUrl: ordinary page requests can be mistaken for data requests, while directNextURLdata URLs cannot be inspected, cloned, redirected, or rewritten with the identity encoded by the client request.Expected Next.js behavior
Next derives
buildIdfrom the request URL. Its middleware adapter normally converts a Pages data request to the page URL and explicitly clearsbuildIdbefore user middleware runs. With URL normalization disabled, middleware instead receives the raw data URL and its parsed build ID.For localized root requests, the real client endpoint is
/_next/data/<id>/en.json, as exercised by Next's middleware matcher E2E. Next's current standalone formatter can produceenindex.jsonafter mutation; vinext deliberately preserves the real request endpoint instead.Execution paths
src/index.tsNextURLbehavior is corrected; App Router has no Pages/_next/data/endpointTests and verification
Tests cover ordinary URLs, default middleware normalization with no build ID, raw skip-mode middleware URLs with a build ID, root and localized-root endpoints, base paths, locales, setters, cloning, and generated-entry wiring.
pnpm test tests/middleware-runtime.test.ts tests/pages-request-pipeline.test.ts tests/shims.test.ts— 1,407 passedpnpm exec vp check tests/middleware-runtime.test.ts tests/pages-request-pipeline.test.ts tests/shims.test.ts— passedvinext#build— passedSelf-review
Automated review found that the first implementation could prefix a preserved raw path twice. Commit
e2d52bd3fixed that and added regression coverage forhref,toString(), middleware use, andclone().A second full review clarified the default middleware contract, added end-to-end pipeline proof that default mode receives the page URL with no build ID, and documented/tested the localized-root endpoint divergence. No broader parser-sharing refactor was taken because request analysis and current-build validation have different responsibilities.