简体   繁体   中英

Is there a way to normalise differences in request.url when http firebase-functions running under emulator and deploy

For firebase tools 6.9.2:

When using firebase emulators:start --only functions, hosting where the functions are invoked via a hosting rewrite rule, the functions are invoked with different request url path prefixes than if the functions were deployed to the cloud. For example given a firebase.json hosting snippet such as:

    {
      "target": "myapi",
      "public": "./dist/hosting/myapi",
      "rewrites": [{
          "source": "/api/myapi/*",
          "function": "myapi"
        }
      ]
    },

When deployed to firebase, the myapi function will be called with a request url that starts with: /api/myapi/

but if run in the emulator it ends up looking like: /[firebase-project-id]/[firebase-region]/myapi/api/myapi/

This doesn't make a difference if the function doesn't look at the request path but in my case, the function is a handler for an express (koa) handler with routes so knowing the base path is important.

I've looked to see if anything is available in process.env but only the GCLOUD_PROJECT name and a few FIREBASE_CONFIG params are there.

I could not find any documentation on more advanced control of the rewrite to a function in firebase.json.

I can work around it in testing by setting an environment variable to match the prefix the functions are running at locally and taking it into account during request handling. Unfortunately the request url prefix is not available until after the emulators are started and the function urls are logged.

I am hoping to find a better solution for testing.

This turned out to be a bug and is was resolved for my case in firebase-tools 6.10.0.

https://github.com/firebase/firebase-tools/issues/1279

This related issue was also opened:

https://github.com/firebase/firebase-tools/issues/1312

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM