Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functions called from browser execute twice #33

Closed
stocksp opened this issue May 29, 2019 · 14 comments · Fixed by #81 or #121
Closed

functions called from browser execute twice #33

stocksp opened this issue May 29, 2019 · 14 comments · Fixed by #81 or #121
Labels
bug Something isn't working

Comments

@stocksp
Copy link

stocksp commented May 29, 2019

Although curl will only call the function once.
Chrome load or refresh calls the function twice.
A bit confusing when debugging something more complex than HelloWorld.

Very easy to see:

exports.helloWorld = (req, res) => {
    console.log('running HelloWorld'); // <-- oh no not again
    res.send('Hello, World');
  };

Is there a way to stop this?

@stew-r
Copy link
Contributor

stew-r commented May 29, 2019

Very interesting. I can confirm that I'm also encountering this issue.

@swalkowski any idea what might be causing this?

@stew-r
Copy link
Contributor

stew-r commented May 29, 2019

OK so we think this is happening because browser requests also send a request for a favicon. This means that hitting the function in the browser results in two invocations of the function.

I think we can fix this by changing our logic to only invoke the function in response to requests on /${function_name} instead of /*.

Thanks for the report.

@grant grant added the bug Something isn't working label Jul 8, 2019
@grant
Copy link
Contributor

grant commented Aug 7, 2019

@grant
Copy link
Contributor

grant commented Sep 2, 2019

I've created a PR to fix this.

@grant grant closed this as completed in #81 Sep 6, 2019
grant added a commit that referenced this issue Sep 6, 2019
* fixes #33: Only listen to functionTarget.

* fix: Specify Express routing URLs based on functionTarget

* test: Adds test target

* Fix: Fix linting

* test: Use more interesting test path
@grant
Copy link
Contributor

grant commented Sep 6, 2019

Fixed in master. Please re-open if you still see this issue when testing on master.
Should be fixed on npm in next release.

hdp617 added a commit that referenced this issue Sep 14, 2019
@dobromyslov
Copy link

Why have you reverted this without any explanation? 1.3.2 still executes twice.

@hdp617
Copy link
Contributor

hdp617 commented Nov 11, 2019

I reverted this because it was not compatible with GCF. Requests to a deployed function to GCF would return an error. We are working on solving this issue.

@hdp617 hdp617 reopened this Nov 11, 2019
@grant
Copy link
Contributor

grant commented Nov 25, 2019

Looks like internal bug is b/133880441

@dobromyslov
Copy link

dobromyslov commented Nov 25, 2019

Thank you for clarification, Grant. For development as a workaround I use Express and bind my functions to URIs there. In index.js I check if production Cloud Functions environment is not available and then configure Express itself with all project functions as a main function. This approach makes possible to run several functions simultaneously and the problem with double execution disappears.

@chiefjester
Copy link

chiefjester commented Feb 16, 2020

I'm glad I stumbled on this thread. @stew-r is correct, when accessed by a browser, it requests an additional payload for favicon.ico. I'm just putting this up for future me and people get annoyed by this extra invocation.

You can wrap your statements via:

  if (req.originalUrl !== "/favicon.ico") {
    console.log("running HelloWorld");
  }

Or just use express as @dobromyslov suggest and use a middleware that ignores the favicon.

function noFavicon(req, res, next) {
  req.originalUrl === "/favicon.ico" ? res.status(204).json({}) : next();
}
app.use(noFavicon)

or just add a new route

app.get("/favicon.ico", (req, res) => res.status(204).json({}));

@kirbdee
Copy link

kirbdee commented Feb 28, 2020

👍

When running

functions-framework --target=sampleFunction

the logs show

Serving function...
Function: sampleFunction
URL: http://localhost:8080/

Didn't realize that if you just run that, it will also request the fav icon. but explicitly setting the path as the function in your call it will avoid the favicon call.

@hdp617 hdp617 linked a pull request Mar 3, 2020 that will close this issue
@Didier68
Copy link

Hello, I have the same problem, but I do not understand the answers speaking the link with favicon: in the second call, I find in "req.url" the same path as in the first call.
This bug becomes annoying as soon as I dedug the Node server, because I'm juggling between 2 threads which do the same things in parallel.

Does anyone know how to block this in Chrome?

@leviross
Copy link

Im running into the same behavior that @Didier68 is. Chome and apparently Edge are requesting the same route 2 times in a row. The 2nd time, the request has a 'Referer' header that is the value of the route itself. So it looks like its just sending 2 requests one right after the other.

@leviross
Copy link

Try looking for an '' tag in your Html that is like malformed or something. That was the culprit in my case!! I had this: <img src="#" id="productImage" width="100" height="100" alt="adding cart image">. I commented this out and now Chrome makes only the 1 request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
9 participants