Do middlewares are counted as functions?
Hi everyone, I am new to cloudflare and trying to learn pages.
On the pages websites, it is said "On both free and paid plans, requests to static assets are free and unlimited. A request is considered static when it does not invoke Functions."
https://developers.cloudflare.com/pages/functions/pricing/#static-asset-requests
I was wondering if invoking a middleware is counted as invoking a function. To be more specific, I am using
functions/_middleware.js
to serve static assets and I am not sure if such a middleware incur cost for serving a static file.
If so, is there a way to serve those static files without a middleware?Cloudflare Docs
Pricing | Cloudflare Pages docs
Requests to your Functions are billed as Cloudflare Workers requests. Workers plans and pricing can be found in the Workers documentation.
6 Replies
Middleware only run if a Function is triggered. If you don't have any functions covering
/static
, then you shouldn't need the above Middleware, and all requests to /static
would be freeI can not use a static index.html as I need to generate its content dynamically. I don't know how to serve it without a middleware.
Then you can just remove the
/static
sectionWithout the middleware, I receive 404 for anything, including static assets.
I was using it as an easy way to detect static assets, I can remove it. So, without it, there should be no cost then. For example:
I just realized I left out the most important detail. I was using
functions/_middleware.js
and that is the only middleware I use, so everything counts as function invocation, right?Yes, but you should be able to scope it with a
_routes.json
Cloudflare Docs
Routing | Cloudflare Pages docs
Functions utilize file-based routing. Your /functions directory structure determines the designated routes that your Functions will run on. You can create a /functions directory with as many levels as needed for your project’s use case. Review the following directory:
Ok, thanks.