Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use alternative way of gathering api functions to anaylze #2654

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading