GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.3k stars 160 forks source link

Cloud function return 404 for /favicon.ico #456

Closed bananaappletw closed 2 years ago

bananaappletw commented 2 years ago

I use cloud function as a reverse proxy between client(browser) and GCS bucket.

Fetch the GCS bucket object and create readable stream to pipe to client.

When I request /favicon.ico it returns 404.

There's an existing logic to return 404 for /favicon.ico|/robots.txt.

Ref: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/07668f696b29f077d4ba9d0463ea994076cdab50/src/server.ts#L122+L125

I haven't thought about the solution which also fulfill the original purpose, just to provide my use case.

Ref: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/pull/193

Thank you.

anniefu commented 2 years ago

Hey @grant, could you add some context to the history of this here? This may be related #191

grant commented 2 years ago

The URL path favicon.ico returns a 404 to prevent double invocation of a user function when invoking via a HTTP browser. Serverless functions should really just handle 1 endpoint for 1 function.

It's typically recommended that you don't use the literal URL path as an input to your function, at least as an input to another URL.


Could your function use a HTTP query param instead, such as localhost:8080/?path=myURL/foo.txt? I believe this should solve the use-case.

grant commented 2 years ago

I believe the alternative implementation should satisfy the use-case. LMK if that doesn't work. Closing as WAI but feel free to comment with requested changes and I can re-open if needed.

bananaappletw commented 2 years ago

@grant Thank you for explanation.

Could your function use a HTTP query param instead

No, I used cloud function as a reverse proxy without any manipulation on the URL.

My intent is that a cloud function should be able dealing with the original request without any extra logic attached on it.

grant commented 2 years ago

To follow up here, Cloud Functions is meant to serve and invoke a single function when receiving a HTTP request. The URL path should not be part of the logic of the function. For example, Cloud Functions Gen 1 and Gen 2 have different URL paths.

For this use-case of using URL paths within the function, I would consider using express directly with Cloud Run and not using the Functions Framework.

eastlondoner commented 2 years ago

Hi @grant I am running into this problem.

Would it be possible to have some configuration option to disable this very specific behaviour where Google Cloud http triggered functions object to serving /favicon.ico

The Google Cloud Firebase documentation has examples of using a Google Cloud Function to host an express web app which literally relies on the URL path - which is at odds with your statement:

the URL path should not be part of the logic of the function

https://firebase.google.com/docs/hosting/functions#use_a_web_framework