Skip to content

Commit 8a92f28

Browse files
authored
[backport] fix: avoid importing types that will be unused (#82856) (#83026)
1 parent 19617ab commit 8a92f28

File tree

1 file changed

+24
-3
lines changed
  • packages/next/src/server/lib/router-utils

1 file changed

+24
-3
lines changed

packages/next/src/server/lib/router-utils/typegen.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,23 @@ export function generateValidatorFile(
579579
}
580580

581581
// Build import statement based on what's actually needed
582-
const routeImports = ['AppRoutes', 'LayoutRoutes', 'ParamMap']
582+
const routeImports = []
583+
584+
// Only import AppRoutes if there are app pages
585+
if (appPageValidations) {
586+
routeImports.push('AppRoutes')
587+
}
588+
589+
// Only import LayoutRoutes if there are layouts
590+
if (layoutValidations) {
591+
routeImports.push('LayoutRoutes')
592+
}
593+
594+
// Only import ParamMap if there are routes that use it
595+
if (appPageValidations || layoutValidations || appRouteHandlerValidations) {
596+
routeImports.push('ParamMap')
597+
}
598+
583599
if (hasAppRouteHandlers) {
584600
routeImports.push('AppRouteHandlerRoutes')
585601
}
@@ -593,13 +609,18 @@ export function generateValidatorFile(
593609
? "import type { NextRequest } from 'next/server.js'\n"
594610
: ''
595611

612+
// Only import metadata types if there are App Router pages or layouts that might use them
613+
const metadataImport =
614+
appPageValidations || layoutValidations
615+
? 'import type { ResolvingMetadata, ResolvingViewport } from "next/dist/lib/metadata/types/metadata-interface.js"\n'
616+
: ''
617+
596618
return `// This file is generated automatically by Next.js
597619
// Do not edit this file manually
598620
// This file validates that all pages and layouts export the correct types
599621
600622
${routeImportStatement}
601-
import type { ResolvingMetadata, ResolvingViewport } from "next/dist/lib/metadata/types/metadata-interface.js"
602-
${nextRequestImport}
623+
${metadataImport}${nextRequestImport}
603624
${typeDefinitions}
604625
${appPageValidations}
605626

0 commit comments

Comments
 (0)