When browser makes a request to the firebase hosting site URL it gets the html but returns a 404 error on JS and CSS browser request. On the other hand requests made by the browser to the URL from the Cloud Run instance generates the website completely.
The firebase.json Config below:
{
"hosting": {
"public": "public",
"ignore": ["firebase.json", "/.*", "/node_modules/"]
},
"rewrites": [
{
"source": "/auth{,/}",
"run": {
"serviceId": "auter",
"region": "us-central1"
}
},
{
"source": "/profile{,/}",
"run": {
"serviceId": "rara",
"region": "us-central1"
}
},
{
"source": "/{,/}",
"run": {
"serviceId": "lala",
"region": "us-central1"
}
}
],
"trailingSlash": false,
"cleanUrls": true
}
I run a Node.js Server on each of the Cloud run instance using express.js, Here is the general code structure of each of the servers:
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cluster from "cluster";
import { cpus } from "os";
import path from "node:path";
const __dirname = process.cwd();
const staticFiles = path.resolve(__dirname + "/public/dist");
const totalCPU = cpus().length;
const app = express();
const PORT = process.env.PORT || 8000;
if (cluster.isWorker == false) {
console.log(Number of CPUs is ${totalCPU});
//Fork Workers
for (let i = 0; i < totalCPU; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(
Cluster ${worker.process.pid} died, Code: ${code}, Signal: ${signal}
);
console.log("Creating anoter worker");
cluster.fork();
});
} else {
app.use(express.static(staticFiles));
app.get("*", (req, res) => {
res.sendFile(path.resolve(staticFiles, "index.html"));
});
app.listen(Number(PORT), () => {
console.log(Server started on ${PORT});
});
}