How CF Pages function get request.host referer
# pages-help
n
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
c
Something like
Copy code
javascript
export function onRequestGet(context) {
    const { request } = context;
    const host = request.headers.get('host');
    const referer = request.headers.get('referer');
n
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.
c
Yeah the request object doesn't log. Pretty sure you have to use
JSON.stringify(request)
.
n
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?
c
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
n
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 ?
c
You should be able to yeah.
n
@Cyb3r-Jok3 I have added cache header via adding _headers file
Copy code
/*
    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?
c
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
n
@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.