'Disable / Undo Firebase Cloud Functions default request parsing

I want to deploy a remix application to Firebase Cloud Functions, using Hosting for the static assets. The function is defined as:

const functions = require("firebase-functions");
const express = require("express");
const compression = require("compression");
const morgan = require("morgan");
const { createRequestHandler } = require("@remix-run/express");

const app = express();

app.use(compression());
app.use(morgan("tiny"));

app.all("*", createRequestHandler({ build: require("./build") }));

const api = functions.https.onRequest(app);

module.exports = {
  api,
};

As documented here the request bodies are parsed by firebase before the request is passed to the api function. But the app is expecting "untouched" requests. This results in the request body being empty inside remix.

Is there a way to disable or undo the request body parsing? I've tried req.body = req.rawBody; in a middleware without luck.



Solution 1:[1]

There is no way to disable the preprocessing done on the request in Cloud Functions. This is true for both the Firebase and Google Cloud variants.

If you want full control over the code that processes the request, consider using a different product, such as Cloud Run, which gives you the ability to control the behavior of all the code in the docker image that you build and deploy.

Solution 2:[2]

There is a way to make Firebase functions work with Remix form data.

You can pass the form data by adding it to context:

in function/index.js, you need to get the post payload and add it to context

  app.all("*", createRequestHandler({build: require("./build"),
    getLoadContext(req) {
      return {body: req.body || null};
    }}));

and then action in your route you can read the context:

export const action = async ({ request, context }) => {
  console.log(context)
  return {context};
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Doug Stevenson
Solution 2 Jkarttunen