@@ -36,27 +36,46 @@ interface ApiBackgroundConfig {
3636type ApiConfig = ApiStandardConfig | ApiScheduledConfig | ApiBackgroundConfig
3737
3838export async function getAPIRoutesConfigs ( ctx : PluginContext ) {
39+ const uniqueApiRoutes = new Set < string > ( )
40+
3941 const functionsConfigManifestPath = join (
4042 ctx . publishDir ,
4143 'server' ,
4244 'functions-config-manifest.json' ,
4345 )
44- if ( ! existsSync ( functionsConfigManifestPath ) ) {
46+ if ( existsSync ( functionsConfigManifestPath ) ) {
4547 // before https://github.com/vercel/next.js/pull/60163 this file might not have been produced if there were no API routes at all
46- return [ ]
48+ const functionsConfigManifest = JSON . parse (
49+ await readFile ( functionsConfigManifestPath , 'utf-8' ) ,
50+ ) as FunctionsConfigManifest
51+
52+ for ( const apiRoute of Object . keys ( functionsConfigManifest . functions ) ) {
53+ uniqueApiRoutes . add ( apiRoute )
54+ }
55+ }
56+
57+ const pagesManifestPath = join ( ctx . publishDir , 'server' , 'pages-manifest.json' )
58+ if ( existsSync ( pagesManifestPath ) ) {
59+ const pagesManifest = JSON . parse ( await readFile ( pagesManifestPath , 'utf-8' ) )
60+ for ( const route of Object . keys ( pagesManifest ) ) {
61+ if ( route . startsWith ( '/api/' ) ) {
62+ uniqueApiRoutes . add ( route )
63+ }
64+ }
4765 }
4866
49- const functionsConfigManifest = JSON . parse (
50- await readFile ( functionsConfigManifestPath , 'utf-8' ) ,
51- ) as FunctionsConfigManifest
67+ // no routes to analyze
68+ if ( uniqueApiRoutes . size === 0 ) {
69+ return [ ]
70+ }
5271
5372 const appDir = ctx . resolveFromSiteDir ( '.' )
5473 const pagesDir = join ( appDir , 'pages' )
5574 const srcPagesDir = join ( appDir , 'src' , 'pages' )
5675 const { pageExtensions } = ctx . requiredServerFiles . config
5776
5877 return Promise . all (
59- Object . keys ( functionsConfigManifest . functions ) . map ( async ( apiRoute ) => {
78+ [ ... uniqueApiRoutes ] . map ( async ( apiRoute ) => {
6079 const filePath = getSourceFileForPage ( apiRoute , [ pagesDir , srcPagesDir ] , pageExtensions )
6180
6281 const sharedFields = {
0 commit comments