Skip to content

fix: get source file for api routes in src subdirectory #1778

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

Merged
merged 7 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions packages/runtime/src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,13 @@ const getServerFile = (root: string, includeBase = true) => {
/**
* Find the source file for a given page route
*/
export const getSourceFileForPage = (page: string, root: string) => {
for (const extension of SOURCE_FILE_EXTENSIONS) {
const file = join(root, `${page}.${extension}`)
if (existsSync(file)) {
return file
export const getSourceFileForPage = (page: string, roots: string[]) => {
for (const root of roots) {
for (const extension of SOURCE_FILE_EXTENSIONS) {
const file = join(root, `${page}.${extension}`)
if (existsSync(file)) {
return file
}
}
}
console.log('Could not find source file for page', page)
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime/src/helpers/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,14 @@ export const setupImageFunction = async ({
export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => {
const pages = await readJSON(join(publish, 'server', 'pages-manifest.json'))
const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/'))
// two possible places
// Ref: https://nextjs.org/docs/advanced-features/src-directory
const pagesDir = join(baseDir, 'pages')
const srcPagesDir = join(baseDir, 'src', 'pages')

return await Promise.all(
apiRoutes.map(async (apiRoute) => {
const filePath = getSourceFileForPage(apiRoute, pagesDir)
const filePath = getSourceFileForPage(apiRoute, [pagesDir, srcPagesDir])
return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] }
}),
)
Expand Down