`Next Build` Shows Generic "Invalid Segment Configuration Export Detected" When `Config.Matcher` Uses `String.Raw` In `Proxy.Ts`
Using `String.raw` in `config.matcher` inside `proxy.ts` triggers a generic build error because Next.js's static analyzer does not evaluate tagged templates, and the error message lacks file and expression details.
Next.js segment config static analysis requires `config.matcher` to be a literal string or array of literals. `String.raw` is a call expression that is not statically evaluated, so the proxy config is marked invalid, and the error reporter fails to attach the source location or offending expression.
1. Create `src/proxy.ts` with `export const config = { matcher: String.raw`/api/:path*` };`
2. Run `next build`
3. Observe the generic error: `Invalid segment configuration export detected...`
Fixing Code Block
// In packages/next/src/build/segment-config/index.ts
// Replace the generic error with a specific one using the AST node location
import { codeFrameColumns } from 'next/dist/compiled/babel/code-frame'
import { relative } from 'path'
export function reportInvalidSegmentConfig(
file: { path: string; source: string },
invalidExport: { key: string; loc: { line: number; column: number } }
) {
const codeFrame = codeFrameColumns(file.source, { start: invalidExport.loc }, { highlightCode: true })
throw new Error(
`Invalid segment configuration export detected in ${relative(process.cwd(), file.path)}.\n` +
`The export \`${invalidExport.key}\` must be a static string or array of strings.\n` +
`Tagged templates such as \`String.raw\` are not statically analyzable.\n\n` +
`${codeFrame}\n\n` +
`Please replace \`String.raw\` with a plain string literal (e.g., '/api/:path*').`
)
}
// Usage: replace the generic throw in collectSegmentConfig with:
// const matcherNode = findInvalidMatcher(file, config)
// if (matcherNode) reportInvalidSegmentConfig(file, { key: 'matcher', loc: matcherNode.loc })
The hotfix adds a helper that constructs a detailed error message including the file path, a code frame pointing to the invalid export, and an explicit explanation that `String.raw` tagged templates are not statically analyzable. This replaces the generic throw in the segment config collection logic.
Edge Case Audit
This change only affects error reporting; no runtime behavior is altered. Rollback is safe by reverting the diff. Ensure that the `codeFrameColumns` import is available and that the AST location mapping is correctly implemented for the existing parser; otherwise, the error may lack a code frame but still provide the file path. No concurrency or multi-threading issues are introduced.