firebase-tools: NextJS API Routes not working with Firebase Hosting

[REQUIRED] Environment info

firebase-tools: 12.5.2

Platform: Mac OS

[REQUIRED] Test case

I have a simple NextJS Project using pages router with one single API endpoint in the src/pages/api folder. The API endpoint works correctly locally, but after deploying to firebase hosting it gives error, with not much info but it is either status code 500 or 408 (timeout)

Code:

Component code:

      <Button
        onClick={async () => {
          try {
            fetch("api/mixpanel/track", {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: JSON.stringify({
                event: "<EVENT_NAME>",
              }),
            })
              .then((res) => {})
              .catch((err) => {
                captureException(err); // I have enabled Sentry but for some reason those longs also don't appear, but lets ignore that mostly a Sentry issue
              });
          } catch (error: any) {
            Logger.log(error.toString()); // custom logging util which is configured to only work with localhost
          }
        }}
      >
        Track event
      </Button>

API code: src/pages/api/mixpanel/track.ts

import { NextApiRequest, NextApiResponse } from "next";

import { Logger } from "@utils/logger";
import mixpanel from "@utils/mixpanel";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method === "POST") {
    const event = req.body.event;
    const properties = req.body.properties;

    try {
      if (properties) {
        mixpanel.track(event, properties);
      } else {
        mixpanel.track(event);
      }
      return res.status(200).json({
        message: "Success",
      });
    } catch (e) {
      Logger.log("NextJS Server error: unable to track mixpanel");
      return res.status(500).json({
        message: "NextJS Server error: unable to track mixpanel",
      });
    }
  }
}

[REQUIRED] Steps to reproduce

Deploy NextJS project with API routes to firebase hosting

[REQUIRED] Expected behavior

API route working correctly.

[REQUIRED] Actual behavior

Errors with status codes 408 or 500 (no specificity in these either, which will occur is random, but it does not work is the main thing)

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Hi @ritvij14, thanks for reporting this. I was able to reproduce the issue you mentioned. I tried to use the code snippet you provided, but removed the parts related to mixpanel and logger, and I noticed that on my end, POST request responds with 500 error.

From what I can tell, the issue started occurring after Next.js v13.4.12 as I’m able to get 200 responses when using v13.4.12. Let me notify our team about this issue so that we can provide more context and investigate this behavior.

This has been addressed with the release of firebase-frameworks@0.11.0 https://github.com/FirebaseExtended/firebase-framework-tools/pull/122

Same issue, any fix on this? 13.4.12 is not working for me for server get routes (requests are redirected to 404). The only combination (somewhat) working for me is next@13.4.7 and firebase-tools@12.5.2. Even with this combination I am struggling with these issues:

  1. SSR get requests: works on emulator and cloud run revision urls but NOT on firebase hosting default url. More info: https://github.com/firebase/firebase-tools/issues/6434
  2. SSR post requests: Same issue as on this thread, timeout issues