Firebase deployed to wrong hosting when multiple projects used - firebase

I have two firebase projects: myapp for prod, and myapp-dev for dev environment.
I first used the firebase cli to init my project with "myapp" and so all the files were generated with this, including the hosting resource myapp (so I can deploy my app to myapp.web.app).
Then I have added a second firebase project ("myapp-dev"). I run those
firebase use --add myapp-dev # I have selected the right myapp-dev firebase project and set `dev` as short name
firebase target:apply hosting myapp-dev myapp # note here that I also use name "myapp" as resource
I have manually changed my .firebasesrc because I want the dev project to be default...
So my .firebasesrc looks like this
{
"projects": {
"default": "myapp-dev",
"prod": "myapp"
},
"targets": {
"myapp": {
"hosting": {
"myapp": [
"myapp"
]
}
},
"myapp-dev": {
"hosting": {
"myapp": [
"myapp"
]
}
}
}
}
and firebase.json
{
"hosting": [
{
"target": "myapp",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
],
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
}
Now when I ran those lines, the webapp got deployed to the prod env, the functions to the dev env...
firebase use myapp-dev
firebase deploy
EDIT
Running firebase target:apply hosting myapp myapp-dev helped !

In my case I had the same app deploying to 2 different environments (development and production). The documentation is not very clear on this scenario, took some playing around to get the right config.
.firebaserc
{
"projects": {
"production": "myapp-prod",
"development": "myapp-dev"
},
"targets": {
"myapp-prod": {
"hosting": {
"myapp": [
"site-name-prod"
]
}
},
"myapp-dev": {
"hosting": {
"myapp": [
"site-name-dev"
]
}
}
}
}
firebase.json
{
"hosting": [{
"target": "myapp",
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
}]
}
To deploy to production run
firebase use production
firebase deploy
To deploy to development
firebase use development
firebase deploy
When you run firebase deploy in the project, it uses the target specified in the hosting section on the firebase.json to deploy to the site name specified in the hosting section of the target in the .firebaserc

I think your config should be something like this.
file .firebasesrc
"targets": {
"myapp-dev": {
"hosting": {
"myapp-dev": [
"myapp-dev"
],
"myapp": [
"myapp"
]
}
}
}
file firebase.json:
"hosting": [
{
"target": "myapp-dev",
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
{
"target": "myapp",
"public": "dist/myapp", /* folder */
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
]
and then deploy using target documentation

Related

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"
}
]
},

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?

Nuxt app hosting on firebase function URL end with "/" before queries

I am building a Nuxt app hosting on firebase function. When I see the URLs on local, they look fine, however, on firebase they end with "/" before queries. Am I missing any settings?
example:
https://xxxxx.firebaseapp.com/select-prefecture/?type=blocks
http://localhost:4000/select-prefecture?type=blocks
production
local
firebase.json
{
"hosting": {
"site": "xxxxxx",
"public": "public",
"rewrites": [
{
"source": "**",
"function": "app"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}

How to do url rewriting in Firebase hosting Firebase.Json

In my website i.e in my server i have googlee.technology/Verify/191711443.pdf is there, I want to know if user enters googlee.technology/Verify/191711443 without pdf extension, I want to display same results in my web page . How to do that.
This is my Firebase.json file code:
{
"hosting": {
"site": "chinnapareddy",
"public": "y",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
`"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"`
}
}
To rewrite /Verify/191711443 to /Verify/191711443.pdf, you'd use this in your firebase.json:
"hosting": {
// ...
// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
"source": "/Verify/191711443",
"destination": "/Verify/191711443.pdf"
}]
}
Update: this is what the JSON from your last comment should look like:
{
"hosting": {
"site": "chinnapareddy",
"public": "y",
"ignore": ["firebase.json", "/.*", "**/node_modules/"]
"rewrites": [{
"source": "/Verify/191711443",
"destination": "/Verify/191711443.pdf"
}]
},
"functions": {
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"],
"source": "functions"
}
}

How to set multiple environments for Firebase

For my project I use staging and production environment.
fileReplacements property inside angular.json is used to change files:
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
},
{
"replace": "src/app.html",
"with": "src/app.prod.html"
}
]
to change between environments .firebaserc files is used.
Now I need to use different firebase config files per environment ( firebase.json and firebase.prod.json )
Is it possible to do it? I thought to use predeploy scrips, but don't know how
I have done it using single .firebaserc file. You can add different targets to deploy.
firebase target:apply type target-name resource-name
ref: [ https://firebase.google.com/docs/cli/targets && https://firebase.google.com/docs/cli/ ]
// firebase.json
"hosting": [
{
"target": "prod",
"public": "dist/<folder_name>,
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
]
// .firebaserc file
// Sample: here the "projects" are your targets
{
"projects": {
"default": "staging-project",
"dev": "staging-project",
"prod": "prod-project"
},
"targets": {
"staging-project": {
"hosting": {
"stage": [
"staging-project"
],
"admin-stage": [
"your-admin-stage"
],
"admin-prod": [
"your-admin-prod"
],
"prod": [
"prod-project"
]
}
}
}
}
// in the build script
firebase use dev
firebase deploy --only firestore:rules,hosting:stage,functions...

Resources