How CF Pages function get request.host referer
I have created a topic about How can I get request.host referer from CF Pages function
https://community.cloudflare.com/t/how-cf-pages-function-get-request-host-referer/601736
I aim to identify the domain and the calling page URL, as there are multiple domains linking to this Page project. Utilizing these two parameters is essential for fetching distinct data based on the origin.
I can see these two properties in the Real-time Logs of functions
14 Replies
Something like
it works. Thank you very much. I have tried
context.request.headers.host
does not work for me. I even console.log(context) object. it shows request property is empty.
Thank you so much for the quick reply. Yeah the request object doesn't log. Pretty sure you have to use
JSON.stringify(request)
.Could you also assist me in understanding why the custom domain is not caching Pages once I've added it to the page project? I use vue and call a function return it host
How can achieve cache it?
This is by design. By default Pages, doesn't cache to prevent issues with new deployments caching. It does have internal caching as well though. You can change it will cache rules or headers but keep in mind that it could break your deployments
Thanks @Cyb3r-Jok3 , if domain is not managed by cloudflare. so I cannot use cache rule right?(I am a newbie).
I can use middleware https://developers.cloudflare.com/pages/functions/middleware/ to add response header to achieve it ?
I can use middleware https://developers.cloudflare.com/pages/functions/middleware/ to add response header to achieve it ?
Middleware · Cloudflare Pages docs
Middleware is reusable logic that can be run before your onRequest function. Middlewares are typically utility functions. Error handling, user …
You should be able to yeah.
@Cyb3r-Jok3 I have added cache header via adding _headers file
/*
Cache-Control: public, max-age=604800
But CF still doesn't cache it. even the header clearing showing in the response.
I wondered if you could help me to acheive it?Cloudflare doesn't cache HTML by default. You'd have to setup a cache rule to enable caching HTML which if your zone isn't on Cloudflare isn't possible
@Cyb3r-Jok3 , I appreciate your generosity and the time you've dedicated for my question.
@Cyb3r-Jok3 I wonder if cloudflare still provide DDOS protection to the Pages project if domain zone isn't on Cloudflare. Becuase this pages project will allow multiple non-clouflare managed domains point to it.
There is DDoS protection in the sense that you would need an astronomical number of requests to take down a Pages project, but you wouldn’t be able to block Functions requests, so if you have those, you would still be billed
@HardlyHolidayin’ Many thanks. I'm currently on the free plan. I can't see any settings limiting the budget cap from the cloudflare web console. I'd like to know if CF has a setting like setting a maximum budget cap, say up to $20 per month, and once the budget cap is reached, the functions will automatically stop/disable .like Failure mode. if CF doen't provide spending cap, how can I manully stop/disable functions?
You can set a billing alert, but not a cap. As for disabling functions, afaik you would have to run a new deploy without the functions
Great thanks. Thanks for letting me know more about CF.