NextJS API Routes vs Firebase Cloud Functions

Viewed 1244

NextJS has a solution to build your own API and have it handle things like authentication and making database requests.

If I'm using using Vercel to host my app, is there a real reason (other than cost) to use Firebase Cloud Functions separate from NextJS, or do they work the same?

Will it be easier to have all my server side things on NextJS and Vercel rather than have it on Firebase Cloud Functions?

1 Answers

There are a few things to consider when deciding between Firebase Cloud Functions and Next.js API routes.

Reasons to use Cloud Functions

Trigger on database changes

If a document is added, you can use a Firebase Cloud Function to do something with the data. Next.JS will not be automatically triggered.

Integration with Google Cloud

If you want tight integration with Google Cloud, you may wish to just stick with Firebase. Other than error logging, I can't think of any specific reasons why this would be a deciding factor, but it may be a factor for your use case.

You need more then a REST API

There are multiple types of Firebase functions. Callable, HTTPs, triggered, etc. If all you need are HTTPs functions, you can probably just use Next.js functions. But anything more isn't possible with Next.js.

Deploy functions independently

With Next.js, to update a function, you need to redeploy the entire site again. With Firebase functions, you can deploy one function at a time.

Reasons to use Next.js API functions

If on Vercel, they're basically free

Since the entire Next.js app is broken down into serverless functions, Vercel essentially doesn't place [its normal limits on Next.js API function calls][1]. I haven't had a project where the price was really a factor since Firebase functions are so cheap, but perhaps it's something for you to consider.

Simpler development

With Firebase Functions, you also need to setup and use the emulators when in development. Next.js API functions are, well, apart of Next.js and work automatically in development.


My choice? If the project can work with only Next.js API routes, I stick with that. IF not, I code everything in Firebase Cloud Functions. It just keeps things simpler that way.

Related