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
Cyb3r-Jak3
Cyb3r-Jak36mo ago
Something like
export function onRequestGet(context) {
const { request } = context;
const host = request.headers.get('host');
const referer = request.headers.get('referer');
export function onRequestGet(context) {
const { request } = context;
const host = request.headers.get('host');
const referer = request.headers.get('referer');
node java
node java6mo ago
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. 10000
Cyb3r-Jak3
Cyb3r-Jak36mo ago
Yeah the request object doesn't log. Pretty sure you have to use JSON.stringify(request).
node java
node java6mo ago
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?
No description
Cyb3r-Jak3
Cyb3r-Jak36mo ago
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
node java
node java6mo ago
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 ?
Middleware · Cloudflare Pages docs
Middleware is reusable logic that can be run before your onRequest function. Middlewares are typically utility functions. Error handling, user …
Cyb3r-Jak3
Cyb3r-Jak36mo ago
You should be able to yeah.
node java
node java6mo ago
@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?
No description
Cyb3r-Jak3
Cyb3r-Jak36mo ago
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
node java
node java5mo ago
@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.
Hello, I’m Allie!
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
node java
node java5mo ago
@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?
No description
Hello, I’m Allie!
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
node java
node java5mo ago
Great thanks. Thanks for letting me know more about CF.
Want results from more Discord servers?
Add your server
More Posts