Jasper Huang
Jasper Huang

Reputation: 569

Firebase Functions onRequest not triggering

I have a Firebase function host set up to run whenever the user visits a Firebase Hosting URL, but nothing ever gets logged (the function is not being triggered)

index.js:

exports.host = functions.https.onRequest((req, res) => {
  console.log(">>>> FUNCTION TRIGGERED!")
  res.send(">>>> GOT IT")
})

I have it set up like so in my firebase.json:

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/**",
        "destination": "/index.html",
        "function": "host"
      }
    ]
  }
}

Here are the emulator function logs when I run it locally (the emulator is running on localhost:4001, and the web app is running on localhost:5000. When I visit localhost:5000, nothing logs, even though the function is said to be initialized.

12:53:42W
ui
Emulator UI unable to start on port 4000, starting on 4001 instead.
12:53:42I
ui
Emulator UI logging to ui-debug.log
12:53:43I
functions
Watching "**firebase-project**/functions" for Cloud Functions...
12:53:43I
functions
http function initialized (http://localhost:5001/****/us-central1/host).
12:53:43I

┌───────────────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! View status and logs at http://localhost:4001 │
└───────────────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4001/functions │
├───────────┼────────────────┼─────────────────────────────────┤
│ Hosting   │ localhost:5000 │ n/a                             │
└───────────┴────────────────┴─────────────────────────────────┘
  Other reserved ports: 4400, 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
 
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET / HTTP/1.1" 200 2603 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/css/main.0fec40ca.chunk.css HTTP/1.1" 200 53148 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/css/2.85b71462.chunk.css HTTP/1.1" 200 43004 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/js/main.3cf6a16f.chunk.js HTTP/1.1" 200 216613 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/js/2.8f67d3af.chunk.js HTTP/1.1" 200 3343005 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /faceapi/weights/ssd_mobilenetv1_model-weights_manifest.json HTTP/1.1" 200 26548 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /faceapi/weights/face_landmark_68_model-weights_manifest.json HTTP/1.1" 200 7889 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/media/Create%20visuals%20for%20remote%20teaching..9fdce83b.png HTTP/1.1" 200 30230 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/media/homepage_background.1c84776f.png HTTP/1.1" 200 72508 "http://localhost:5000/static/css/main.0fec40ca.chunk.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /static/media/homepage_laptop.a18abdeb.png HTTP/1.1" 200 6106290 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /faceapi/weights/face_landmark_68_model-shard1 HTTP/1.1" 200 356840 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /faceapi/weights/ssd_mobilenetv1_model-shard2 HTTP/1.1" 200 1422653 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"
12:54:19I
hosting
127.0.0.1 - - [03/Nov/2020:04:54:19 +0000] "GET /faceapi/weights/ssd_mobilenetv1_model-shard1 HTTP/1.1" 200 4194304 "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0"

Is this correct way of setting up functions? It's my first time working with them so I'm having a hard time debugging this. For some context, I'm trying to dynamically modify the OpenGraph meta tags depending on the URL, and I'm following this tutorial: https://medium.com/@jalalio/dynamic-og-tags-in-your-statically-firebase-hosted-polymer-app-476f18428b8b

Upvotes: 1

Views: 237

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317412

Your hosting rewrite looks incorrect. I suggest taking a look at the documentation for how to configure a rewrite to a function. It shouldn't have a "destination", just "source" and "function". Also, if you intend to serve the URI "/" through the function, you should not have an index.html present at all in your public folder, otherwise it will get served first.

Upvotes: 1

Related Questions