Skip to content

Commit 2a3ad3c

Browse files
himself65ascorbic
andauthored
fix: get source file for page in api routes (#1778)
Fixes: #1774 Co-authored-by: Matt Kane <[email protected]>
1 parent df2aee1 commit 2a3ad3c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

packages/runtime/src/helpers/files.ts

+7-5
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,14 @@ export const setupImageFunction = async ({
169169
export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => {
170170
const pages = await readJSON(join(publish, 'server', 'pages-manifest.json'))
171171
const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/'))
172+
// two possible places
173+
// Ref: https://nextjs.org/docs/advanced-features/src-directory
172174
const pagesDir = join(baseDir, 'pages')
175+
const srcPagesDir = join(baseDir, 'src', 'pages')
173176

174177
return await Promise.all(
175178
apiRoutes.map(async (apiRoute) => {
176-
const filePath = getSourceFileForPage(apiRoute, pagesDir)
179+
const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir])
177180
return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] }
178181
}),
179182
)

0 commit comments

Comments
 (0)