Unverified Commit f696914f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Version adapter routing for Pages API routes

parent 1d5dd672
...@@ -23,6 +23,7 @@ Called after the build process completes with detailed information about routes ...@@ -23,6 +23,7 @@ Called after the build process completes with detailed information about routes
**Parameters:** **Parameters:**
- `context.routing`: Object containing Next.js routing phases and metadata - `context.routing`: Object containing Next.js routing phases and metadata
- `routing.version`: Version of the routing contract. Version `2` keeps dynamic Pages API routes in the canonical `/api` namespace. Manifests without a version use the legacy version `1` semantics.
- `routing.beforeMiddleware`: Routes executed before middleware (includes header and redirect handling) - `routing.beforeMiddleware`: Routes executed before middleware (includes header and redirect handling)
- `routing.middlewareMatchers`: Middleware matcher definitions for this build, used to decide whether middleware should be invoked for a given request - `routing.middlewareMatchers`: Middleware matcher definitions for this build, used to decide whether middleware should be invoked for a given request
- `routing.beforeFiles`: Rewrite routes checked before filesystem route matching - `routing.beforeFiles`: Rewrite routes checked before filesystem route matching
......
...@@ -9,6 +9,7 @@ import { interopDefault } from '../../lib/interop-default' ...@@ -9,6 +9,7 @@ import { interopDefault } from '../../lib/interop-default'
import type { RouteHas } from '../../lib/load-custom-routes' import type { RouteHas } from '../../lib/load-custom-routes'
import { recursiveReadDir } from '../../lib/recursive-readdir' import { recursiveReadDir } from '../../lib/recursive-readdir'
import { isDynamicRoute } from '../../shared/lib/router/utils' import { isDynamicRoute } from '../../shared/lib/router/utils'
import { isAPIRoute } from '../../lib/is-api-route'
import type { Revalidate } from '../../server/lib/cache-control' import type { Revalidate } from '../../server/lib/cache-control'
import type { NextConfigComplete } from '../../server/config-shared' import type { NextConfigComplete } from '../../server/config-shared'
import { import {
...@@ -520,6 +521,11 @@ export interface NextAdapter { ...@@ -520,6 +521,11 @@ export interface NextAdapter {
) => Promise<NextConfigComplete> | NextConfigComplete ) => Promise<NextConfigComplete> | NextConfigComplete
onBuildComplete?: (ctx: { onBuildComplete?: (ctx: {
routing: { routing: {
/**
* Version of the routing manifest contract. Unversioned manifests use
* the legacy version 1 semantics.
*/
version: 2
beforeMiddleware: Array<Route> beforeMiddleware: Array<Route>
/** /**
* middlewareMatchers are the middleware matcher definitions emitted by * middlewareMatchers are the middleware matcher definitions emitted by
...@@ -2125,7 +2131,7 @@ export async function handleBuildComplete({ ...@@ -2125,7 +2131,7 @@ export async function handleBuildComplete({
route.page route.page
) )
const shouldLocalize = Boolean(config.i18n) const shouldLocalize = Boolean(config.i18n && !isAPIRoute(route.page))
const routeRegex = getNamedRouteRegex(route.page, { const routeRegex = getNamedRouteRegex(route.page, {
prefixRouteKeys: true, prefixRouteKeys: true,
...@@ -2423,6 +2429,7 @@ export async function handleBuildComplete({ ...@@ -2423,6 +2429,7 @@ export async function handleBuildComplete({
await adapterMod.onBuildComplete({ await adapterMod.onBuildComplete({
routing: { routing: {
version: 2,
beforeMiddleware: [...headers, ...redirects], beforeMiddleware: [...headers, ...redirects],
middlewareMatchers: middlewareMatchers:
outputs.middleware?.config.matchers?.map((matcher) => ({ outputs.middleware?.config.matchers?.map((matcher) => ({
......
...@@ -6,11 +6,7 @@ describe('adapter config with i18n routes', () => { ...@@ -6,11 +6,7 @@ describe('adapter config with i18n routes', () => {
files: __dirname, files: __dirname,
}) })
// Dynamic Pages API routes are localized like any other dynamic route. The it('keeps dynamic Pages API routes in the canonical namespace', async () => {
// 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')
...@@ -24,11 +20,12 @@ describe('adapter config with i18n routes', () => { ...@@ -24,11 +20,12 @@ describe('adapter config with i18n routes', () => {
(route) => route.source === '/blog/[slug]' (route) => route.source === '/blog/[slug]'
) )
expect(routing.version).toBe(2)
expect(apiOutput).toBeDefined() expect(apiOutput).toBeDefined()
expect(apiRoute).toBeDefined() expect(apiRoute).toBeDefined()
expect(apiRoute?.sourceRegex).toContain('nextLocale') expect(apiRoute?.sourceRegex).not.toContain('nextLocale')
expect(apiRoute?.destination).toBe( expect(apiRoute?.destination).toBe(
'/$nextLocale/api/proxy/[[...slug]]?nxtPslug=$nxtPslug' '/api/proxy/[[...slug]]?nxtPslug=$nxtPslug'
) )
expect(pageRoute).toBeDefined() expect(pageRoute).toBeDefined()
......
...@@ -83,6 +83,7 @@ describe('adapter-config export', () => { ...@@ -83,6 +83,7 @@ describe('adapter-config export', () => {
} }
expect(routing).toEqual({ expect(routing).toEqual({
version: 2,
beforeMiddleware: expect.toBeArray(), beforeMiddleware: expect.toBeArray(),
middlewareMatchers: expect.toBeArray(), middlewareMatchers: expect.toBeArray(),
beforeFiles: expect.toBeArray(), beforeFiles: expect.toBeArray(),
......
...@@ -451,6 +451,7 @@ describe('adapter-config', () => { ...@@ -451,6 +451,7 @@ describe('adapter-config', () => {
} }
expect(routing).toEqual({ expect(routing).toEqual({
version: 2,
beforeMiddleware: expect.toBeArray(), beforeMiddleware: expect.toBeArray(),
middlewareMatchers: expect.toBeArray(), middlewareMatchers: expect.toBeArray(),
beforeFiles: expect.toBeArray(), beforeFiles: expect.toBeArray(),
......
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