Unverified Commit 7297fe2a authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

Exclude metadata files from route handler type validation (#99377)

## Summary

Metadata files such as `app/icon.tsx` export a default handler, rather
than HTTP methods. Validating them as ordinary route handlers rejects
the documented `size` and `contentType` exports with TS2559 during `next
typegen` + `tsc`.

Exclude metadata files from ordinary route-handler validation in both
validator modes by matching the `route.<extension>` file convention,
including custom page extensions. Add an icon fixture with the
documented exports and a regression test that checks generated
validation, retains an ordinary route-handler control, and runs `tsc`.

Correct the existing non-strict invalid-page-props assertion as well.
The same failure reproduces on the unchanged base commit: non-strict
`AppPageConfig` accepts arbitrary props, so the per-page check emits the
invalid-props diagnostic instead. The test still requires a failed build
and the specific invalid-props error; strict mode retains its
`AppPageConfig` assertion.

## Verification

- Reproduced the stale assertion on unchanged base commit `3932a6bd`:
12/13 tests passed, with only invalid-page-props failing.
- Full Webpack validator suite: **14/14 passed** in normal mode and
**14/14 passed** with `__NEXT_EXPERIMENTAL_STRICT_ROUTE_TYPES=true`.
Both runs include the icon regression and invalid ordinary-handler
checks.
- Core Next.js build (including types), formatting, and ESLint passed.
- Direct before/after checks verified that both generators exclude
metadata files while retaining ordinary and custom-extension route
handlers.
- Tests use `NEXT_SKIP_ISOLATE=1` because the isolated install cannot
resolve the configured package registry. Full bootstrap in the temporary
worktree hit environment restrictions; the core package build passed.

<!-- NEXT_JS_LLM -->
parent df0228d4
......@@ -448,14 +448,17 @@ export function generateValidatorFile(
.filter(
(filePath) => filePath.endsWith('.ts') || filePath.endsWith('.tsx')
)
.filter(
// Don't include metadata routes or pages
// (e.g. /manifest.webmanifest)
(filePath) =>
type !== 'AppPageConfig' ||
filePath.endsWith('page.ts') ||
filePath.endsWith('page.tsx')
)
.filter((filePath) => {
// Metadata files have different exports from pages and route handlers.
if (type === 'AppPageConfig') {
return filePath.endsWith('page.ts') || filePath.endsWith('page.tsx')
}
if (type === 'RouteHandlerConfig') {
// Match custom page extensions too, such as route.api.ts.
return /(?:^|\/)route\.[^/]+$/.test(filePath)
}
return true
})
.map((filePath) => {
// Keep the file extension for TypeScript imports to support node16 module resolution
const importPath = filePath
......@@ -689,14 +692,17 @@ export function generateValidatorFileStrict(
.filter(
(filePath) => filePath.endsWith('.ts') || filePath.endsWith('.tsx')
)
.filter(
// Don't include metadata routes or pages
// (e.g. /manifest.webmanifest)
(filePath) =>
type !== 'AppPageConfig' ||
filePath.endsWith('page.ts') ||
filePath.endsWith('page.tsx')
)
.filter((filePath) => {
// Metadata files have different exports from pages and route handlers.
if (type === 'AppPageConfig') {
return filePath.endsWith('page.ts') || filePath.endsWith('page.tsx')
}
if (type === 'RouteHandlerConfig') {
// Match custom page extensions too, such as route.api.ts.
return /(?:^|\/)route\.[^/]+$/.test(filePath)
}
return true
})
.map((filePath) => {
// Keep the file extension for TypeScript imports to support node16 module resolution
const importPath = filePath
......
import { ImageResponse } from 'next/og'
export const size = { width: 32, height: 32 }
export const contentType = 'image/png'
export default function Icon() {
return new ImageResponse(<div style={{ display: 'flex' }}>A</div>, size)
}
......@@ -152,8 +152,13 @@ describe('typed-routes-validator', () => {
/Type error: Type 'typeof import\(.*' does not satisfy the expected type 'AppPageConfig</
)
} else {
expect(cliOutput).toMatch(
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'AppPageConfig</
// Non-strict AppPageConfig accepts any props. The per-page type check
// rejects invalid props instead.
expect(cliOutput.replace(/\\/g, '/')).toContain(
'Type error: Page "app/invalid/page.tsx" has an invalid "default" export:'
)
expect(cliOutput).toContain(
'Type "{ invalidProp: string; }" is not valid.'
)
}
})
......
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