Skip to content

Commit

Permalink
fix: use alternative way of gathering api functions to anaylze (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Oct 11, 2024
1 parent f44f575 commit e4916da
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/build/advanced-api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,46 @@ interface ApiBackgroundConfig {
type ApiConfig = ApiStandardConfig | ApiScheduledConfig | ApiBackgroundConfig

export async function getAPIRoutesConfigs(ctx: PluginContext) {
const uniqueApiRoutes = new Set<string>()

const functionsConfigManifestPath = join(
ctx.publishDir,
'server',
'functions-config-manifest.json',
)
if (!existsSync(functionsConfigManifestPath)) {
if (existsSync(functionsConfigManifestPath)) {
// before https://github.com/vercel/next.js/pull/60163 this file might not have been produced if there were no API routes at all
return []
const functionsConfigManifest = JSON.parse(
await readFile(functionsConfigManifestPath, 'utf-8'),
) as FunctionsConfigManifest

for (const apiRoute of Object.keys(functionsConfigManifest.functions)) {
uniqueApiRoutes.add(apiRoute)
}
}

const pagesManifestPath = join(ctx.publishDir, 'server', 'pages-manifest.json')
if (existsSync(pagesManifestPath)) {
const pagesManifest = JSON.parse(await readFile(pagesManifestPath, 'utf-8'))
for (const route of Object.keys(pagesManifest)) {
if (route.startsWith('/api/')) {
uniqueApiRoutes.add(route)
}
}
}

const functionsConfigManifest = JSON.parse(
await readFile(functionsConfigManifestPath, 'utf-8'),
) as FunctionsConfigManifest
// no routes to analyze
if (uniqueApiRoutes.size === 0) {
return []
}

const appDir = ctx.resolveFromSiteDir('.')
const pagesDir = join(appDir, 'pages')
const srcPagesDir = join(appDir, 'src', 'pages')
const { pageExtensions } = ctx.requiredServerFiles.config

return Promise.all(
Object.keys(functionsConfigManifest.functions).map(async (apiRoute) => {
[...uniqueApiRoutes].map(async (apiRoute) => {
const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir], pageExtensions)

const sharedFields = {
Expand Down

0 comments on commit e4916da

Please sign in to comment.