Firebase hosting rewrites to functions not working as expected - firebase

I am building a static website with HTML, CSS & JS and hosted on firebase hosting and connected a custom domain. The only serverside function I need is to send mail from the contact form. For this, I am trying to use Firebase cloud function. I have initialized functions on the same project and trying to use firebase hosting rewrites to rewrite the request to mydomain.com/contact to the contact function. But when I try to access the mydomain.com/contact in the browser it shows the below 403 Forbidden error message.
Error: Forbidden
Your client does not have permission to get URL /contact/contact from this server.
firebase.json
{
"hosting": {
"public": "build",
"rewrites": [{
"source": "/contact",
"function": "contact"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
The cloud function (not implemented the actual logic)
import * as functions from 'firebase-functions';
exports.contact = functions.https.onRequest((request, response) => {
response.send("<h1>Contact<h1>");
});
I am using Firebase spark plan.

This works for me:
"rewrites": [
{
"source": "**",
"function": "myApp"
}
]
And in the express function,
import * as functions from 'firebase-functions';
import express from 'express';
const app = express();
app.get('/contact', (req, res) => {
res.send("<h1>Contact<h1>");
};
export const myApp = functions.https.onRequest(app);

I had the same issue this week. I had a rewrite from /user to a function called user and I was getting the response Your client does not have permission to get URL /user/user from this server.
I just deleted the functions from the firebase console then deployed them again and now they work. There must be some occasional bug with deploying functions like this and/or using hosting rewrites where the rewrite path gets appended to the function url path on the server.

Related

Quasar vite doesn't return the ssrapp handler for my firebase functions

I started a Quasar V2 app using the quasar CLI with vite config and ssr build. I can't seem to return the firebase functions handler from dist/ssr/index.js as suggested in the docs.
I've tried this with the webpack Quasar V2 config and it works fine.
I ran firebase init then installed firebase-functions and firebase-admin in the root dir.
I changed the source of functions to dist/ssr where I built my ssr app. I also edited hosting to send requests to the function "handler".
firebase.json
"functions": {
"source": "dist/ssr",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
},
"hosting": {
"public": "dist/ssr",
"rewrites": [
{
"source": "**",
"function": "handler"
}
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
},
I then returned the handler function, following the instructions from the quasar docs
server.js
import * as functions from "firebase-functions";
export const listen = ssrListen(async ({ app, port, ssrHandler }) => {
if (process.env.DEV) {
await isReady();
return app.listen(port, () => {
if (process.env.PROD) {
console.log("Server listening at port " + port);
}
});
} else {
return {
handler: functions.https.onRequest(ssrHandler),
};
}
});
I then ran "npm install" on both the root directory and the dist/ssr directory to make sure everything is fine.
then when I ran "firebase serve" I get back this result in localhost:5000 "Function us-central1-handler does not exist, valid functions are:" Whats going on :(

Firebase x NextJS deploymnet SSG only

I want to deploy my NextJS SSG build into Firebase but after enabling webframeworks experiment Firebase CLI runs its own build commands and try to deploy to my free plan Firebase project, and fails at the end saying that I need to enable Functions. How can I deploy my SSG website to firebase in Firebase CLI?
BTW, I dont have any functions folder, nor is it specified in firebase config. I user Mantine for UI and it supports SSG. Looks like the CLI does some assuming to check for funcitons too (after reading the docs I disabled NextJS Image optimization and I dont have any redirects, rewrites, or headers in next.config.js). If possible can I disable this automatic creation of a functions?
Here is my firebase.json;
{
"hosting": {
"site": "xxxxxxxxxxx",
"source": ".",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Here is my next.config.js;
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
images: {
unoptimized: true
}
}
module.exports = nextConfig

Deploying Quasar with Firebase Hosting

I'm having trouble deploying my quasar project to Firebase. It says the deployment was successful, but after two hours, this is what shows up on the webpage.
firebase.json
"hosting": {
"public": "dist/spa",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
routes.js
const routes = [
{
path: '/',
component: () => import('layouts/MainLayout.vue'),
children: [
{ path: '', component: () => import('pages/PageUsers.vue') },
{ path: '/chat/:otherUserId', component: () => import('pages/PageChat.vue') },
{ path: '/auth', component: () => import('pages/AuthPage.vue') }
]
},
// Always leave this as last one,
// but you can also remove it
{
path: '*',
component: () => import('pages/Error404.vue')
}
]
export default routes
I first ran quasar build, then firebase init, then firebase deploy yet nothing is showing up on the site yet. Any ideas to why this is happening?
Update: Now the same page is showing on http://localhost:8080/ instead of my project as well
If you build first, "firebase init" will overwrite your index.html.
Firebase init
Run quasar build
Firebase deploy
Did you try in this order?
I also had the same problem but, after I changed the order, then it worked as usual.
It is because Firebase is overwriting your index.html with its own index.html, to fix that:
run quasar build -m spa to generate the correct index.html
run firebase init and when it asks you to overwrite index.html select 'No' like this:
run firebase deploy
I ran into the same issue while setting up Quasar project with Firebase hosting.
One thing I noticed was there was a new index.html generated during firebase init inside the public folder.
After deleting public/index.html, the original index page displays properly.
Solution:
Check if there is an index.html file generated in public folder
Delete the file

Firebase Hosting proxy to Cloud Functions only triggers in us-central1

I have my firebase cloud functions created with region: asia-northeast1, I am trying to trigger one of the functions using the firebase.json file.
"hosting": [
{
"target": "ksite",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{ "source": "**", "function": "ssr" }
]
}
]
Unfortunately this triggers the function with region of us-central1 https://us-central1-site.cloudfunctions.net/ssr/ instead of asia-northeast1 something like this https://asia-northeast1-site.cloudfunctions.net/ssr/
How and why is this happening?
Here is my function:
// For Universal Rendering
export const ssr = functions.region('asia-northeast1').https.onRequest((request, response) => {
require(`${process.cwd()}/dist/ksite-webpack/server`).app(request, response);
});
Firebase Hosting currently only supports the proxy of URLs to functions deployed to us-central1, as mentioned in the documentation:
Important: Firebase Hosting supports Cloud Functions in us-central1 only.
And also here in the docs:
Important: If you are using HTTP functions to serve dynamic content for Firebase Hosting, you must use us-central1.

I can't access on express route on Firebase Hosting

Now I created a file by node server like
const functions = require("firebase-functions")
const express = require("express")
/* Express */
const app = express()
app.get("/test", (request, response) => {
response.send("Hello from Express on Firebase!")
})
const api1 = functions.https.onRequest(app)
module.exports = {
api1
}
firebase.json
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
and I have deployed it to firebase hosting and try tried access it on firebase hosting the index.html show up to me but when I require the /test it's return as 404.html page ! what's I'm missing ?
after updated firebase.json and added rewrites to
{
"hosting":
{
"public": "public",
"rewrites": [
{
"source": "/test",
"function": "app"
}
]
}
}
the message is different now
The answer
I must to structured my project files to
-project (folder)
---functions (folder)(contains on all nodejs files)
---public (filder)
You need to include a rewrites section to your firebase.json file. That tells the Firebase servers how to route any requests that come in... and right now, you aren't telling it anything.
"hosting": {
"rewrites": [
{
"source": "**",
"function": "api1"
}
]
}
This answer isn't actually an answer to your question, but it demonstrates the proper way to set up rewrites with cloud function express apps.

Resources