Search code examples
node.jsfirebaseexpressgoogle-cloud-functionsfirebase-hosting

Firebase rewrites not working on hosted site


I've been trying to get Firebase rewrites working on my Firebase hosted site. And it won't work no matter what I try. Right now I have my rewrites all routed to a routing function called app. As you can see below >>

firebase.json

     {
            "public": "public/dist-main",
            "target": "public-store-easy",
            "ignore": [
                "firebase.json",
                "**/.*",
                "**/node_modules/**"
            ],
            "rewrites": [
                {
                    "source": "/**",
                    "function": "app"
                }
            ]
        },

Here is my function for routing >>

const functions = require('firebase-functions');
const express = require('express');
const path = require('path');
const html = require('html')
const app = express();
app.use(express.static('/../public/'));

app.get('/', (req, res) => {
    res.sendFile('/dist-main/index.html', { root: '../public' })
})
app.get('/login', (req, res) => {
    res.sendFile('/dist-main/login.html', { root: '../public' })
})
app.get('/HOME', (req, res) => {
    res.sendFile('/dist-main/home.html', { root: '../public' })
})
app.get('/feedback', (req, res) => {
    res.sendFile('/dist-main/feedback-form.html', { root: '../public' })
})
exports.app = functions.https.onRequest(app);

I expected it to work just fine because it worked just fine in my localhost server and on the firebase serve, server.

Thank you in advance!


Solution

  • When deployed Cloud Functions only have access to files that are within the functions directory. You are trying to send files from ../public which is not deployed with functions.

    If you want this to work, you'll need to move the public directory inside the functions directory and update the {"hosting": {"public": ""}} key appropriately.