I found cloud flare functions routing + routes.json, etc documentation to be kinda confusing.
Quotes like:
"However, once you add Functions on a Pages project, all requests by default will invoke your Function."
"Create a routes.json file to control when your Function is invoked"
"include: Defines routes that will be invoked by Functions"
Were confusing to me -- IDK I had questions like:
" is cloudflare using a fetch or an http request on every single request to my site to every single one of my functions?
Why would it do that when all my fx's are specifically made to be invoked by a fetch request from my client?
I have more than one functions in my directory, what does it mean for my Function to be invoked? Are all of them invoked? '"
Anyways, I think I've clarified most of it
(except for understanding how to fail open or fail close a cf function attached to pages project, plz help ).
Anyways, can anyone let me know if the understanding I ended up deriving below is correct?
When any request hits a cf pages project with functions (e.g., GET /, GET /about), Cloudflare doesn't inherently know if the asset requested is in /functions or /static (build dir deployed).
This means that for every request, even if its a GET yoursite.com/picture.jpg, GET yoursite.com/mycss.css, GET yoursite.com/favicon.ico, cloudflare has to go inside the /functions directory and check every single function to see if theres a match.
Regardless of if there is or isn't, the very act of having to go into the /functions directory to check for a match means this will count as a function invocation (**even if no function was actually executed**) towards your monthly total.
_routes.json fixes this problem of unnecessary checks inside the /functions directory by basically giving cloudflare a cheatsheet.
IE: By using a minimal ‘include’ in _routes.json with just your exact api, you can basically tell cloudflare:
"hey, if a user sends any request targeting mysite.com/xyz-api or mysite.com/wel-api, then you should look inside the /functions directory and use one of my monthly functions calls to satisfy this requests.
For all other requests sent to my site, please ignore the /functions directory because those are static requests and you can go directly to the /static directory to find the asset requested (or 404 if isn't found)’’
Per cloudflare user Skye , you don’t actually have to make a _routes.json if you have a simple well structured use-case, cloudflare will automatically detect and do this for you.
A question here -- can you actually see what the generated _routes.json looks like on the cloudflare dashboard?
Please do not ping community members for non-moderation reasons. Doing so will not solve your issue faster and will make people less likely to want to help you.