Firebase Function Error: could not handle the request + Next App

Viewed 290

This might seem like a repeated question but i promise I've made sure to check every related ones but non solve my problem

I am deploying a nextjs app on Firebase hosting using cloud functions, the app does deploy and gives me back the hosting URL of app link. visiting the URL i got this 403 forbidden error;

Your client does not have permission to get URL /nextServer/ from this server.

Which I fixed by adding allUsers and allAuthenticatedUsers as function invoker at function permission.

But then, after fixing that I got another error saying:

Error: could not handle the request

this terminated the function with a log of

Function execution took 804 ms, finished with status: 'crash'

This doesn't give any reason why the function crashed which has made fixing it all the more harder. Below is my nextServer function code:

const { https } = require("firebase-functions");
const { default: next } = require("next");

const isDev = process.env.NODE_ENV !== "production";

const server = next({
    dev: isDev,
    conf: { distDir: ".next" },
});

const nextjsHandle = server.getRequestHandler();
exports.nextServer = https.onRequest((req, res) => {
    return server.prepare().then(() => {
        return nextjsHandle(req, res);
    });
});

and this is the firebase.json:

{
  "hosting": {
    "public": "public",
    "site": "webavocat",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "nextServer"
      }
    ]
  },
  "functions": {
    "source": ".",
    "runtime": "nodejs16",
    "ignore": [
      "**/.vscode/**",
      ".firebase/**",
      ".firebaserc",
      "firebase.json",
      "**/node_modules/**",
      "**/public/**",
      "**/.next/cache/**"
    ]
  }
}

The project folder structure is as shown below

Project folder scaffold

Edit 1: Don't really know if this is correct,

const nextjsHandle = server.getRequestHandler();
exports.nextServer = https.onRequest((req, res) => {
    try {
        return server.prepare().then(() => {
            return nextjsHandle(req, res);
        });
    } catch (error) {
        console.error(error);
    }
});

but i used a try catch on the on the server function as @mdobrucki suggested. still no log on the why the function crashed though

0 Answers
Related