Firebase - function rewrites for client/server setup in 1 project - firebase

Fairly new to Firebase (and love it), but have scoured the web and cannot find a similar issue with a valid solution.
I am using 2 firebase.json files, 1 for my client and 1 for my server (both express apps). They both deploy a function each to a single firebase project.
Note: They are both express apps (client side is ssr react app, server side is node js api).
The problem I am having is that for the life of me, I cannot seem to redirect the /api path to my server firebase function.
This is my client-side firebase.json:
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "/api{,/**}",
"function": "server"
},
{
"source": "**",
"function": "client"
}
],
"ignore": ["firebase.json", "**/.*", "node_modules/**"]
},
"functions": {
"source": ".",
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint", "npm --prefix \"$RESOURCE_DIR\" run build"]
}
}
And this is my server (api) firebase.json:
{
"hosting": {
"public": "lib",
"rewrites": [
{
"source": "**",
"function": "server"
}
],
"ignore": ["firebase.json", "**/.*", "node_modules/**"]
},
"functions": {
"source": ".",
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint", "npm --prefix \"$RESOURCE_DIR\" run build"]
}
}
Whatever I do, when I navigate to www.mydomain.com/api it is only rendering the client side app not the API! Yet I have both the client & server functions showing in my Firebase functions dashboard.
So a couple of questions:
1) Why is this happening, is there something wrong with my firebase config?
2) Is it ok to use 2 firebase.json files for 1 project or preference to separate these into multiple projects? If so, why?
Thanks

Related

"Ref does not have a version" error when trying to install an extension locally using Firebase CLI

I installed stripe/firestore-stripe-payments extension using Firebase Console (https://firebase.google.com/products/extensions/stripe-firestore-stripe-payments) and it works fine.
However, I wanted to install the extension locally so I can have a full testing environment using Firebase Local Emulator Suite.
The issue is that whenever I try to run firebase ext:install stripe/firestore-stripe-payments using both firebase-tools 11.8.1 and 11.14.1, it shows the following error:
I tried looking online but it seems that this error is not common.
Here's my firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"database": {
"port": 9000
},
"ui": {
"enabled": true
},
"firestore": {
"port": 8080
},
"storage": {
"port": 9199
},
"hosting": {
"port": 5000
},
"pubsub": {
"port": 8085
},
"eventarc": {
"port": 9299
}
},
"remoteconfig": {
"template": "remoteconfig.template.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"storage": {
"rules": "storage.rules"
},
"hosting": {
"public": "./web/build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
Thank you in advance
The v11 version of firebase-tools works quite differently to previous major versions.
To run locally, you will need to make a few additions.
1). Add an extensions object to your firebase.json and use a relative path to point to the route of the stripe extension (basically where the extension.yaml lives).
"extensions": {
firestore-stripe-payments: "../path-to-extension-yaml"
}
2). Next you will require a new folder to host your extension configuration.
Add a new folder called "extensions" in the same directory as your firebase.json.
Create new files for local config (this must match the extension you are attempting to emulate).
For example:
firestore-stripe-payments.env.local for main configuration
firestore-stripe-payments.secret.local for secrets
LOCATION=us-central-1
PRODUCTS_COLLECTION =products
etc...
3). Finally, run firebase emulators:start -P ${project_id}, it is recommended to use the prefix demo-, as in demo-project` to avoid emulating any production services.
For more guidelines with Stripe, the official repository has an example or extensions tests https://github.com/stripe/stripe-firebase-extensions/tree/next/firestore-stripe-payments/_emulator
I fixed the error by specifying the exact version, according to this GitHub comment.
The latest version can be found as version in extension.yaml in the official repository, which "source code" link in Firebase's extension page can refer to it, in my case:
https://github.com/stripe/stripe-firebase-extensions/blob/master/firestore-stripe-payments/extension.yaml
So, I used this command and it worked!
firebase ext:install stripe/firestore-stripe-payments#0.3.1
It took care of the firebase.json and extensions/firestore-stripe-payments.env automatically.
If you already installed the extension via console.firebase.google.com, use the following command to import the configuration:
firebase ext:export --project=XXXX
Then, override any attribute in extensions/firestore-stripe-payments.env.local as #darren-ackers mentioned.
There's a pending PR that covers most of these steps in addition to webhook configuration, here's the relevant file (README):
https://github.com/stripe/stripe-firebase-extensions/pull/436/files

Firebase Hosting deploys CRA app to 'build' subdirectory

What is happening
When I execute firebase deploy --only hosting given the below folder structure, it deploys the entirety of the ui folder. Meaning, I will have to visit my app at http://my-app.web.app/build.
What I would like to happen
I want to deploy the contents of the build folder generated in the predeploy step of firebase.json as the root of the application. So that my-app/ui/build/index.html ultimately winds up as http://my-app.web.app.
What else have I tried
I thought it would work if I specified "public": "ui/build" in firebase.json, but what happens then is the following error about ui/build/package.json not being found.
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\me\Projects\my-app\ui\build\package.json'
I understand it's looking for ui/build/package.json because of public: ui/build but how can I tell it to build from the ui folder, but deploy the resulting build folder?
folder structure
my-app
functions/
ui/
build (after npm run build, at least)
src/
whatever.tsx
.firebaserc
firebase.json
firestore.indexes.json
firestore.rules
firebase.json
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
},
"hosting": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
],
// this is the problem line
"public": "ui",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"hosting": {
"port": 5000
},
"ui": {
"enabled": true
}
}
}
Oof! Couldn't see it through the trees.
I simply divorced the predeploy script from the public setting in firebase.json:
Set the pre-deploy to target the ui folder ( because $RESOURCE_DIR will become ui/build) and set the public value to ui/build:
"hosting": {
"predeploy": [
"npm --prefix ui run build"
],
"public": "ui/build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},

firebase deploy: error parsing triggers: Cannot find module 'path_to_project/functions'

I need to deploy my project to firebase, I need to deploy some functions too: the command firebase deploy starts working correctly but ends with the error in the title, I run npm install in the main directory and in the functions directory too, but the error is the same.
Can you try to change your firebase.json to:
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"source": "functions",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
},
"hosting": {
"public": "www",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"storage": {
"rules": "storage.rules"
}
}

Issue with multiple sites deployment in Firebase

I have an Angular web application called "my-app".
I created a project on Google Firebase and called it "my-app" as well.
I deployed the Angular application to Firebase project "my-app". As a result, a site "my-app-*****" appeared. My app was accessible on "my-app-*****.web.app" address.
Then I created the second site in Hosting of the same project. I called it "my-app-dev". It got an address but was empty because nothing was deployed yet.
I followed instructions from google: https://firebase.google.com/docs/hosting/multisites
And even one question on StackOverflow: Firebase hosting deploy to other site
I added targets and modified firebase.json. In the result, they looked like that:
firebase.json file content:
"hosting": [
{
"target": "prod",
"public": "www",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
{
"target": "dev",
"public": "www",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
]
}
.firebaserc file content:
{
"projects": {
"default": "my-app-*****"
},
"targets": {
"my-app-*****": {
"hosting": {
"dev": [
"my-app-dev"
],
"prod": [
"my-app-*****"
]
}
}
}
}
But I faced an error when I executed command firebase deploy --only hosting:dev
However, when I executed command firebase deploy --only hosting:prod, everything was deployed without errors.
As a result, I am able to deploy my project to "my-app-*****" site, but I could not deploy my project to "my-app-dev" site. I want both sites to use same built files, that is why "public" was pointing to the same directory "www". Both sites located in the same Hosting of "my-app" project on Google Firebase.
Can someone please help me and explain what have I done wrong?

Firebase Hosting with Function Not Working Locally

I created a simple dynamic firebase hosting with function, everything works fine when deployed but not when the page is served locally.
This is the log of firebase serve
i functions: Preparing to emulate functions.
i hosting: Serving hosting files from: public
✔ hosting: Local server: http://localhost:5004
✔ functions: serveLandingPage: http://localhost:5001/zonaskripsi18/us-central1/serveLandingPage
As we see, there is no error logs in console nor in firebase-debug.log but when I open http://localhost:5004 it shows An internal error occurred while connecting to Cloud Function "serveLandingPage"
This is the firebase.json
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/**",
"function": "serveLandingPage"
}
]
}
}
This is the screenshot:
Firebase has been having issues with its local server lately, just deploy it and then run it to test it
If your functions project is typescript then you have compile the typescript to javascript file before serving the localhost.
cd functions
tsc

Resources