Basically what the title states. I'll be replacing the service name with 'xyzservice' and the region name with 'xyzregion' but everything else is exactly as is.
I have a firebase hosting app where I want to access a service running on Google Cloud Run. As per documentation, I'm using a rewrite rule:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"run": {
"serviceId": xyzservice,
"region": xyzregion
}
},
{
"source": "**",
"function": "app"
}
]
}
}
I can confirm that I see xyzservice is part of my project when I run gcloud beta run services list
Despite this I keep getting this error:
HTTP Error: 400, Cloud Run service xyzservice does not exist in region xyzregion in this project.
I can't seem to find any information about this error online. Anyone know how I might resolve this?
Thanks in advance!
Same here. Are you using a region other than us-central1? In my case, changing the region from asia-northeast1 to us-central1 works. I'm not sure if it's a spec change or a failure.
Firebase Hosting deployments with rewrites to Cloud Run services located in europe-west1 were broken between 24. - 26. August 2020. I reached out to Google Firebase support. They acknowledged the bug and told me on the morning of 26. August that the issue is now resolved. The configuration above should now perfectly work (as long as the service actually exists).
(It was Firebase Support Case 00080862)
Related
`
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
`Have an issue where the Firebase deploy link shows "Firebase Hosting Setup Complete." I've tried just about everything on stackoverflow with no success.
I have it set to "build" not "public" in my firebase.json for hosting and it's not doing anything.
I've tried messing with the index.html but then the screen just comes up white/blank.
I've also deleted firebase files completely and started the init over. Still comes back the same.
Has anyone else ran into this lately?
It's hard to tell without any code snippets or firebase config files but I believe if you specify your public location as "build" you do not need to include the rewrites property in the firebase.json file. According to The Docs,
Set up rewrites for any of these purposes:
Show the same content for multiple URLs. Learn how.
Serve a function or access a Cloud Run container from a Hosting URL. Learn how: function or container.
Create a custom domain Dynamic Link. Learn how.
I have set up my Nuxt app (Nuxt3) to use Firebase Hosting with Cloud Functions. This works fine and able to deploy. However, now I wanted to use a cloud function to handle events triggered in Firestore database. I set up a sample "hello world" cloud function by following the Firebase Cloud Functions tutorial , but now when deploying this new hello world function via the local emulator, the function is no where to be found!
Here is the Emulator log when I run firebase emulators:start:
I see the function output is the server folder, but that is for the app hosting.
Where is the helloWorld cloud function I made?
Directory structure:
functions/:
And the app's root contains a firebase.json like so:
{
"functions": {
"source": ".output/server",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": [
{
"site": "<removed for this post>",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
]
}
My helloWorld.js function is correctly transpiled into the build folder:
Where/how do I access my Hello World function I made? The emulator ONLY seems to show the hosting function.
There is nothing in your post that makes the Firebase Functions Emulator look in the functions folder. It gets completely ignored.
You’ve configured it as:
functions.source: .output/Server
The default for this field is “functions”. Thus, in many samples, it seems like there would be a special meaning for that folder name. It’s just a default.
Thus: it looks into .output/Server/index.js (I think) and executes that.
If you want multiple Functions entry points, have a look at the “codepaces” feature recently released: https://firebase.google.com/docs/functions
Note: I haven’t used codespaces myself
I'm trying to programmatically build my index.html file from a template, using a custom script/pre-deploy hook.
Is it possible to have the Firebase emulator run my predeploy script either (1) on start, or (2) on page refresh, so that I can see in the emulator what will happen on actual deploy?
My script works in actual deployment, but never fires with the emulator.
firebase.json (relevant part)
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"predeploy": "node ./preDeploy.js",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
preDeploy.js (oversimplified minimal example)
const fs = require('fs');
const testString = 'hello';
fs.writeFileSync('./preDeployTest.txt', testString);
I would expect the test file to be generated (1), ideally every time I refresh my site while running the emulator, or (2) at least when I first start the emulator -- but the script only runs on actual deploy.
To be clear, I'd be using this script to generate the file that I want Firebase hosting to use as its index.html, but I want to be able to emulate the results.
This may be possible with the default lifecycle scripts block:
"scripts": {
"prepare": "node ./prepare.js"
}
There's also preprepare and a few other hooks; you may have to toy with it to find the correct one. The point is, that one isn't limited to Firebase specific hooks, but can use those defined by NodeJS.
I currently have the following configuration:
// firebase.json
{
"hosting": [
{
"rewrites": [{
"source": "/articles{,/**}",
"destination": "/articles"
},
{
"source": "**",
"destination": "/index.html"
}]
}
]
}
If I go to mysite.com/articles/<articleID> I'm taken to mysite.com when I'd expect to be taken to mysite.com/articles. What am I doing incorrectly?
I'm following this documentation.
I was able to resolve the issue after taking the following steps:
Change my source from /articles{,/**} to /articles/**
Change my destination from articles to articles.html which is a valid local file
Realize that changes to firebase.json don't change the behavior of the Firebase Hosting emulator until it is restarted.
I believe there are two possible causes.
You have set hosting as an array instead of an object (although I am not completely sure if that is an issue)
Secondly, as you can see in the documentation you shared, just above the heading Direct requests to a function, it says that the destination file must exist and as mysite.com/articles doesn't evaluate to a local file, it is caught by the second rewrite rule and redirected to index.html.
I am setting up a redirect(rewrite) with my firebase hosting so that I can call an api that is running from google cloud run here.
I have tried changing the rewrite string from "/api/**" (should catch all things to page.com/api/** and send that to the function). deleted the index.html and swapped to "**" to capture ALL paths including index. Nothing has worked so far.
My hosting firebase.json is setup like so, is there something wrong with this?
{
"hosting": {
"public": "dist/public",
"ignore": ["firebase.json", "**.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"run": {
"serviceId": "next-js-base-api",
"region": "us-central1"
}
}
]
}
}
I also tried with normal redirects to another page, this does not work, what determines when the firebase.json settings begin to propagate and work?
Update
I tried running the hosting emulator and with a modified rewrite "source": "/api/**" which had the following results. Navigating to /api returns non crash (doesn't redirect) with output in browser of cannot GET /api navigating to api/wkapi (a sub directory that is caught by the api endpoint) returns an unexpected error in the browser and
Error: Unable to find a matching rewriter for {"source":"/api/**","run":{"serviceId":"next-js-base-api","region":"us-central1"}}
in the console.
Make sure to update to the latest version of your Firebase CLI by running:
npm install -g firebase-tools#latest
This will enable you to rewrite to cloud run instances as you are trying to do.
Actually, I ran this just now and, looking at the logs of the deployed cloud-run helloworld container, found that custom-domain/helloworld is actually mapping onto container-domain/helloworld instead of simply mapping to container-domain/. To fix this, I had to add an additional app.get rule to my original Node.js program:
app.get('/helloworld', (req, res) => {
And then calling custom-domain/helloworld worked.
If all the above answers don't help you, the fix that worked for me was:
Look in the public directory in your project that firebase hosting uses.
Delete the index.html file that firebase created when going through
the firebase init steps.
After deleting the generated index.html file, it was able to run my cloud run containers with rewrites.
In my case, the cause for this was misspelling function in the firebase.json file, i,e:
"rewrites": [
{
"source": "**",
"function": "ngssr" // I had spelled this as "functions" (extra s)
}
]