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.
export async function onRequest(ctx) {
const { pathname } = new URL(ctx.request.url);

if (pathname.startsWith('/static')) {
return await ctx.env.ASSETS.fetch(ctx.request.url);
}

return new Response(HTML, {
headers: { 'Content-Type': 'text/html' },
});
}
export async function onRequest(ctx) {
const { pathname } = new URL(ctx.request.url);

if (pathname.startsWith('/static')) {
return await ctx.env.ASSETS.fetch(ctx.request.url);
}

return new Response(HTML, {
headers: { 'Content-Type': 'text/html' },
});
}
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
Hard@Work
Hard@Work4mo ago
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 free
snnsnn
snnsnnOP4mo ago
I 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.
Hard@Work
Hard@Work4mo ago
Then you can just remove the /static section
snnsnn
snnsnnOP4mo ago
Without 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:
export async function onRequest(ctx) {
const { pathname } = new URL(ctx.request.url);

if (pathname.endsWith('.css')) {
return await ctx.env.ASSETS.fetch(ctx.request.url);
}

return new Response(HTML, {
headers: { 'Content-Type': 'text/html' },
});
}
export async function onRequest(ctx) {
const { pathname } = new URL(ctx.request.url);

if (pathname.endsWith('.css')) {
return await ctx.env.ASSETS.fetch(ctx.request.url);
}

return new Response(HTML, {
headers: { 'Content-Type': 'text/html' },
});
}
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?
Hard@Work
Hard@Work4mo ago
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:
snnsnn
snnsnnOP4mo ago
Ok, thanks.
Want results from more Discord servers?
Add your server