We are trying to build a REST api using firebase functions. We want that format because we are a web services company who need to expose an api to our clients. People are mostly familiar with REST, therefore, REST.
Following the advice given here I have layered a custom domain in front of a firebase function. I did this by first forwarding my custom domain to the firebase domain via an A record,then setting up a rewrite for the hosting:
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "api"
},
{
"source": "!/#(api)/**",
"destination": "/index.html"
}
]
},
Note that the api function is mapped to anything going to /api/. So far so good. Now I want to make a POST request with a body to mydomain.com/api/user. Unfortunately, when doing so, I get a response Cannot GET /api/user. It seems like firebase is converting the request into a GET before it reaches the function at api. Does anyone know how to make POST requests to functions behind custom domains?
In case someone stumbles over this it's because our route was not defined as containing api. We had something like /user but this configuration obviously requires /api/user.
Related
I'm using the brand new Firebase feature which allows now to deploy Next JS apps on Firebase and use SSR.
I followed the documentation and I'm able to deploy.
As a result, Firebase deploys to Hosting and creates an HTTP Cloud Function that needs to be called in order to access the website.
My problem is that I don't manage to use a custom domain or even the default one provided by firebase hosting.
My firebase.json is currently set as such:
{
"hosting": {
"source": ".",
"rewrites": [
{
"source": "**",
"function": "ssrfunction"
}],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Is there something that I miss?
I'm using two firebase services : Cloud Functions & Firebase hosting.
According to the documentation I'm able to configure urls on Firebase hosting by using firebase.json :
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "showWebProduct"
}
]
}
}
So, in my case when anything is used on path URL it executes showWebProduct, but is it possible to send parameters to this Cloud function ? I don't fond any ressource which show an example with parameter in query.
Thanks
It's not possible to specify parameters in the Hosting configuration to be included in the call to your function. Instead, you should direct Hosting to a function that already has everything it needs to operate.
If you have a specific use case that demands it, you should file a feature request with Firebase support.
I have an api set up on Google Cloud Functions (https://europe-west1-myproject-name.cloudfunctions.net/api/v1/ical.ics).
This works well, but I wish to set up a "friendly" domain name for the api. :)
According to Googles documentation this is seems easy, but it does not seem to work for cloud functions outside the USA, eg. europe-west1.
I have updated the firebase.json file with the below code according to documentation.
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/api/**",
"function": "api"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
When accessing https://myproject-name.web.app/api/v1/ical.ics
I get redirected to https://us-central1-myproject-name.cloudfunctions.net/api/api/v1/cal.ics with error 403 and the below error message.
Error: Forbidden
Your client does not have permission to get URL /api/api/ical.ics from this server.
I must be overlooking something really basic here, since this seems like a really easy operation? :)
Kind regards
/K
As stated in the documentation (see blue text block):
If you are using HTTP functions to serve dynamic content for Firebase
Hosting, you must use us-central1.
You will also find a similar warning in the doc you refer to in your question about "Serve dynamic content and host microservices with Cloud Functions" (See blue text block as well):
Firebase Hosting supports Cloud Functions in us-central1 only.
It is not completely the answer you are looking for since it is not possible with Firebase Hosting.
But it is possible to get a custom domain in front of your cloud functions hosted in EU by using Cloud Run.
I followed this guide:
https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-functions
And after that i added the custom domain under the Manage custom domains on Cloud Run.
Is there a way to use a custom domain for firebase cloud functions http hooks.
The default url for cloud functions looks something like this:
https://us-central1-my-awesome-app.cloudfunctions.net/ios-oauth/
And
I would like to make it look like this:
https://myawesomeapp.com/ios-oauth/
I looked around if there was some other people looking for the same solution and sure enough I found this:
https://stackoverflow.com/questions/43482224/firebase-cloud-functions-custom-domain
I have contacted firebase support to get some answers about this.
And I was forwarded to this part in the documentation.
https://firebase.google.com/docs/hosting/functions#create_an_http_function_to_your_hosting_site
You can use your own domain with the firebase-cloud-functions. The way to do is is using the firebase-hosting.
Connect custom domain to firebase hosting
Add custom function routing to firebase.json
{
"hosting": {
"public": "public",
// Add the following rewrites section *within* "hosting"
"rewrites": [{
"source": "/bigben", "function": "bigben"
}]
}
}
Deploy to firebase
The accepted answer is correct, and I created this repository last year to demonstrate the functionality: https://github.com/cjmyles/firebase-react-express
In order to retain the HTML pushState functionality as per https://firebase.google.com/docs/hosting/full-config#rewrites you may want to extend the rules to allow all other requests through to your index.html page, which solves the issue #Boris was facing in his answer.
"rewrites": [
{
"source": "/api/**",
"function": "app"
},
{
"source": "!/#(api)/**",
"destination": "/index.html"
}
]
This shouldn't really be needed as the rewrite rules are meant to match the first occurence of a request (so the order matters), but this worked for me by allowing all non-api related requests through.
Please note: At the time of writing this the Firebase documentation states: Firebase Hosting supports Cloud Functions in us-central1 only.
If anyone else runs into this, Thomas Bulva's answer is correct but for me, I also had to remove the below snippet from the firebase.json file. .
It was redirecting any request to the index.html page. My https://us---<>.cloudfunctions.net URL was working fine; when I did /helloWorld it would take me to "Hello from Firebase!" But if I tried the same from my custom domain, it would fail. Removing this fixed it.
{
"source": "**",
"destination": "/index.html"
},
For the code examples in the firebase docs, it says to initiate a url rewrite like so:
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
}
What do I do when I want to pass a parameter to the index page? I tried:
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/**",
"destination": "/item.html?i=$1"
} ]
}
But that doesn't do anything..
I have also tried the answer below:
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/:item",
"destination": "/item.html?i=:item"
} ]
}
but that just returns a 404 page.
I know this is an old question but I had a similar issue and didn't find an answer so thought I'd share how I solved it.
{
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/**",
"destination": "/item.html"
} ]
}
I solved my issue by rewriting the dynamic URL to the static html page. Since it's a rewrite the URL will stay as the source URL pattern and not change to the destination URL like a redirect would.
We can then read the source URL through window.location.pathname with Javascript on the item.html page. You can then use .split('/') to return an Array to choose the part that you need.
Hope this helps to anybody looking for a similar solution.
I have just received an email from Firebase support with the follwing:
Update From Firebase support:
Your use case is not possible with Firebase Hosting alone. You will need to make use of Cloud Functions as well to do so. Firebase has just recently released a native Firebase Hosting + Functions integration which makes it possible to perform server-side processing so you can redirect URLs to a function where you can write code to dissect the path and generate whatever response you want. You may check our documentations out to know more about it in detail.
https://firebase.google.com/docs/hosting/functions
I have emailed them back to see if this is something that will be added in the future as it seems a little overkill to run to processes for one page.
Update 28/07/2017
Since this is not supported, I have filed a feature request for you. However, I am not able to share any details or timelines for now. We'll keep your feedback in consideration moving forward though.
In the meantime, you may keep an eye out on our release notes.
Have a great day.
https://firebase.google.com/support/releases
I ran into this same issue as well. Although this isn't possible with rewrites, I found it is possible with redirects:
"redirects": [
{"source": "/user/friendly/url.html",
"destination": "/userunfriendly.html?myid=42",
"type": 301
},
"hosting": {
// Add the "rewrites" section within "hosting"
"rewrites": [ {
"source": "/item/:item",
"destination": "/item.html?i=:item"
} ]
}
Give that a try.
FireBase Allows for Static web hosting. It also has some Custom Behaviours
https://firebase.google.com/docs/hosting/url-redirects-rewrites
Redirects:: this is only for page forwarding
Rewrites:: when you want to show the same content for multiple URLs [ it is there for URL reversing ].
It also provides control over the MIME types of the HTTP requests.
Now, from the Question above I see you are trying to Query for a DOM object from the page which is what Dynamic web hosts provide.
https://www.quora.com/How-are-dynamic-websites-hosted-1