Netlify Dev vs Live function URLs

Netlify site name: https://migraine-timer.netlify.app/ (doesn’t do a lot)

I’m a little confused about calling calling functions locally vs on live.

Does Netlify Dev automatically intercept requests to my live functions and redirect the request to the locally hosted function?

Or do I need to write that logic into my app to make sure that I’m calling the function in the correct place?

If it’s the latter, what’s the best practise for detecting whether I’m running my app inside Netlify Dev vs on live?

Greetings!

If you write a function that targets /.netlify/functions/<YOUR_FUNCTION_NAME>, that will resolve to the function for the that environment whether it be prod, deploy-preview, or local via netlify dev. Yes, dev will intercept your requests and route them to a locally-running Functions server that it spins up for you :slight_smile:


Jon

1 Like

Thanks!

So basically, whether I’m writing client side code that calls a function or a function that calls a function, I can just use /.netlify/functions/<YOUR_FUNCTION_NAME> and it’ll just work?

Nice!

:thinking: that one may not be so easy; Functions themselves aren’t aware that they’re attached to a website per se. I wouldn’t expect something like

fetch('/.netlify/functions/a-different-function')

to work inside a Function, fetching another function. I think to accomplish that you may need to stitch together some data from the event object:

exports.handler = (event, context) => {
  const runningAt = event.headers.host + event.path

  return {
    statusCode: 200,
    body: `I'm running at ${runningAt}`
  }
}

Which comes out looking like

:+1:t2:

That’s how I’d recommend resolving current host/location it for function-to-function calls; but yes for front-end-to-function calls, they will automatically be environment-specific


Jon

Okay I can work with that.

Thank you so much for the explanation!