Unverified Commit 2b4b1ec1 authored by Jude Gao's avatar Jude Gao Committed by GitHub

[backport] Revert i18n localization change for dynamic Pages API routes (#94905) (#97330)

Backports #97327 ("Revert i18n localization change for dynamic Pages API
routes (#94905)") to `next-16-3`.

Clean cherry-pick of `2f84a1f2` — `next-16-3` carries the exact code
being reverted, so it applies as-is.

Restores i18n locale prefixing for dynamic Pages API routes, so the
emitted matcher and the adapter's i18n rules agree again on whether
`/api/...` carries a locale prefix. When they disagree the matcher is
unreachable and requests fall through to `/{locale}/404`.

Pairs with the adapter revert (nextjs/adapter-vercel#101, published as
`adapter-vercel@0.0.1-beta.26`). Full rationale and reproduction in
#97327.
parent 228df5fd
...@@ -55,7 +55,6 @@ import { defaultOverrides } from '../../server/require-hook' ...@@ -55,7 +55,6 @@ import { defaultOverrides } from '../../server/require-hook'
import { generateRoutesManifest } from '../generate-routes-manifest' import { generateRoutesManifest } from '../generate-routes-manifest'
import { Bundler } from '../../lib/bundler' import { Bundler } from '../../lib/bundler'
import { resolveCacheHandlerPathToFilesystem } from '../../lib/format-dynamic-import-path' import { resolveCacheHandlerPathToFilesystem } from '../../lib/format-dynamic-import-path'
import { isAPIRoute } from '../../lib/is-api-route'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
interface SharedRouteFields { interface SharedRouteFields {
...@@ -2065,7 +2064,7 @@ export async function handleBuildComplete({ ...@@ -2065,7 +2064,7 @@ export async function handleBuildComplete({
] ]
for (const route of routesManifest.dynamicRoutes) { for (const route of routesManifest.dynamicRoutes) {
const shouldLocalize = Boolean(config.i18n) && !isAPIRoute(route.page) const shouldLocalize = Boolean(config.i18n)
const routeRegex = getNamedRouteRegex(route.page, { const routeRegex = getNamedRouteRegex(route.page, {
prefixRouteKeys: true, prefixRouteKeys: true,
......
...@@ -6,7 +6,11 @@ describe('adapter config with i18n routes', () => { ...@@ -6,7 +6,11 @@ describe('adapter config with i18n routes', () => {
files: __dirname, files: __dirname,
}) })
it('does not localize dynamic Pages API routes', async () => { // Dynamic Pages API routes are localized like any other dynamic route. The
// Vercel adapter's i18n rules prefix the locale onto `/api/...` too, so the
// matcher has to expect it — if the two disagree the matcher is unreachable
// and requests fall through to `/{locale}/404`. See vercel/next.js#96935.
it('localizes dynamic Pages API routes', async () => {
const { outputs, routing }: Parameters<NextAdapter['onBuildComplete']>[0] = const { outputs, routing }: Parameters<NextAdapter['onBuildComplete']>[0] =
await next.readJSON('build-complete.json') await next.readJSON('build-complete.json')
...@@ -22,9 +26,9 @@ describe('adapter config with i18n routes', () => { ...@@ -22,9 +26,9 @@ describe('adapter config with i18n routes', () => {
expect(apiOutput).toBeDefined() expect(apiOutput).toBeDefined()
expect(apiRoute).toBeDefined() expect(apiRoute).toBeDefined()
expect(apiRoute?.sourceRegex).not.toContain('nextLocale') expect(apiRoute?.sourceRegex).toContain('nextLocale')
expect(apiRoute?.destination).toBe( expect(apiRoute?.destination).toBe(
'/api/proxy/[[...slug]]?nxtPslug=$nxtPslug' '/$nextLocale/api/proxy/[[...slug]]?nxtPslug=$nxtPslug'
) )
expect(pageRoute).toBeDefined() expect(pageRoute).toBeDefined()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment