troublshooting gcloud function with firebase hosting custom domain

92 views
Skip to first unread message

David N Cohen

unread,
Apr 21, 2020, 9:36:59 AM4/21/20
to Google Cloud Developers
I'm working on my first Firebase app, and I've run into an issue with a cloud function triggered by http.

I'm testing the function on the project-specific cloudfunctions.net domain.  Let's say the function is triggered by https://my-project.cloudfunctions.net/MyFunction.  It's implemented on the go113 runtime, in case that matters.  I've also added a custom domain, so I can trigger the cloud function via https://my.domain.com/MyFunction.

But, I've discovered the following differences, which prevent my function from working as intended via the custom domain...

On cloudfunctions.net, any request starting "/MyFunction/..." triggers my function.  And paths have the leading "/MyFunction" trimmed.  So for example https://my-project.cloudfunctions.net/MyFunction/foo/bar reaches my handler and the request path is "/foo/bar"

On my.domain.com, only https://my.domain.com/MyFunction reaches my function.  So, my.domain.com/MyFunction/foo/bar does not trigger my function but instead returns the firebase app 404 page.  Also, when triggered via my.domain.com/MyFunction, the request path is "/MyFunction", the prefix is not trimmed.

What I really hope to accomplish is that all paths starting my.domain.com/MyFunction/... trigger my cloud function.  Is there any way to accomplish that?



David N Cohen

unread,
Apr 21, 2020, 12:16:36 PM4/21/20
to Google Cloud Developers
I was able to address my issue, using the rewrite feature of firebase.json.

I'm not yet certain the best wildcard to use for "source", but something like:

    "rewrites": [ { "source": "*", "function": "MyFunction" }]

causes my cloud function to be triggered for all paths under my.domain.com.

Christian Wong

unread,
Aug 11, 2020, 4:15:06 AM8/11/20
to Google Cloud Developers
Hello,

My function is set to handle /messages and configured with this rewrite:
"rewrites": [{ "source": "*", "function": "api" }]

When I use this rewrite, I get the following scenario:

But when I change it to this rewrite which I believe is fine:
"rewrites": [{ "source": "/api/**", "function": "api" }]

Everything in my.domain.com is messed up and i cannot access the resource. Same as your described in your first email.

That makes me think there is an issue with my instructions inside firebase.json, but I cannot understand what.
I would like to use my function as /api/messages as the example shows.

Appreciate any help.

noverlyjoseph

unread,
Aug 11, 2020, 3:08:17 PM8/11/20
to Google Cloud Developers
Could you post this as a different thread for better tracking for other users, since the original one has been solved.
Reply all
Reply to author
Forward
0 new messages