Skip to content

Commit 3966442

Browse files
committed
fix: get source file for page in api routes
Fixes: opennextjs#1774
1 parent 915e9dd commit 3966442

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

packages/runtime/src/helpers/files.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,13 @@ const getServerFile = (root: string, includeBase = true) => {
337337
/**
338338
* Find the source file for a given page route
339339
*/
340-
export const getSourceFileForPage = (page: string, root: string) => {
341-
for (const extension of SOURCE_FILE_EXTENSIONS) {
342-
const file = join(root, `${page}.${extension}`)
343-
if (existsSync(file)) {
344-
return file
340+
export const getSourceFileForPage = (page: string, roots: string[]) => {
341+
for (const root of roots) {
342+
for (const extension of SOURCE_FILE_EXTENSIONS) {
343+
const file = join(root, `${page}.${extension}`)
344+
if (existsSync(file)) {
345+
return file
346+
}
345347
}
346348
}
347349
console.log('Could not find source file for page', page)

packages/runtime/src/helpers/functions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,14 @@ export const setupImageFunction = async ({
174174
export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => {
175175
const pages = await readJSON(join(publish, 'server', 'pages-manifest.json'))
176176
const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/'))
177+
// two possible places
178+
// Ref: https://nextjs.org/docs/advanced-features/src-directory
177179
const pagesDir = join(baseDir, 'pages')
180+
const srcPagesDir = join(baseDir, 'src', 'pages')
178181

179182
return await Promise.all(
180183
apiRoutes.map(async (apiRoute) => {
181-
const filePath = getSourceFileForPage(apiRoute, pagesDir)
184+
const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir])
182185
return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] }
183186
}),
184187
)

0 commit comments

Comments
 (0)