Next.js Discord

Discord Forum

Next.js API routes do not work when Go function is present

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
using Next.js 15.2.3, deploying on Vercel

API routes generated from Next.js generate a permanent, default 404 page when coupled with an additional, custom Go function.

The Go function is working correctly, but the API routes generated by Next.js are returning a permanent 404 page. The Go function is placed in the top-level /api/<go-function>.go , while the API route is located in src/app/api/<route-name>/route.ts.

file structure
- api
- server-lookup.go
- src
- app/api/check-upvote
- [slug]
- route.ts

expected behavior
Both Next.js dynamic route and Go function are accessible through /api routes

current behavior
When /api/server-lookup.go is present, it works fine, but the Next.js api routes all return default 404 pages

When Go function is inside of /functions/server-lookup.go and it is defined correctly in the vercel.json file, Vercel build process as well as vercel dev return The pattern "src/functions/*.go" defined in "functions" doesn't match any Serverless Functions

When Go function is not present, Next.js api routes work fine
Answered by Spectacled bear
Renamed Next API routes to app/api-next and then added a rewrite in vercel.json
View full answer

20 Replies

Spectacled bearOP
That's something I have considered, but at the same time, it would make them pure Vercel functions and not Next.js API routes
Yes
Spectacled bearOP
That's something that I don't want whatsoever
And moving the Go function to the app/api/* doesn't help either
@Spectacled bear That's something that I don't want whatsoever
Truth is api/ and app/api/ cannot coexist. I faced this problem before and that is the conclusion I arrived at after some research.

Just imagine this, say you have api/[slug].go and app/api/[slug]/route.ts. Which one takes precedence? It’s not an easy problem to solve for Vercel. Certainly not an easy problem to solve for us when the best we can do is to find some scuffed brittle vercel.json configurations on GitHub and pray it somehow works.
Spectacled bearOP
I fully agree with you, just looking for some solution that would allow this Go function to work as I don't want to be rewriting entire Go library to TS, while also sacrifising performance
api/ and app/api-1 (or some variants, as long as it is not “api”) work too if that’s an option for you
Spectacled bearOP
Hmmm
And maybe then a rewrite inside vercel.json?
Since you are writing go functions anyway, you are already locked in Vercel, so I don’t see what harm simply moving ts functions to the root api folder would have
@joulev Since you are writing go functions anyway, you are already locked in Vercel, so I don’t see what harm simply moving ts functions to the root api folder would have
Spectacled bearOP
Next.js on Vercel bundles multiple functions together, and as far as I know, that won't happen with pure TS functions
@Spectacled bear And maybe then a rewrite inside vercel.json?
As I said, Vercel will put every request starting with /api t9 the root api folder. Your nextjs app won’t even see those requests. I doubt vercel.json will allow you to change this behaviour
@Spectacled bear Next.js on Vercel bundles multiple functions together, and as far as I know, that won't happen with pure TS functions
Skimming over the Vercel documentation tells me they now charge usage based on no of invocations and GB-hr, so that shouldn’t matter. Unless you worry about cold starts…
@joulev Skimming over the Vercel documentation tells me they now charge usage based on no of invocations and GB-hr, so that shouldn’t matter. Unless you worry about cold starts…
Spectacled bearOP
Yeah, I'm somewhat worried about cold starts and also the limit of Vercel functions per deployment
Spectacled bearOP
Renamed Next API routes to app/api-next and then added a rewrite in vercel.json
Answer
wow, nice! im pleasantly surprised
Just want to confirm the issue and solution. You had src/app/apiand /api folders that were conflicting. The solution was to rename the first to src/app/api-next?
Seems like the src/app/api folder is just a convention and any folder within src/app with a route.ts can serve as an api folder