How CF Pages function get request.host referer?

How can I get request.host request.referer from Page Functions method

My functions code is below from dir /functions/api/checkDomainAndPath.js

export function onRequestGet(context) {
    console.log(context)

    return Response.json(context)
}

I call it from html page fetch method

 const fetchData = async () => {
        const resp = await fetch('/api/checkDomainAndPath')
        const postsResp = await resp.json()
        console.log(postsResp)
        funResultRef.value = postsResp
    }

context object is

{
  "request": {},
  "functionPath": "/api/checkDomainAndPath",
  "params": {},
  "data": {},
  "env": {
    "ASSETS": {},
    "CF_PAGES": "1",
    "CF_PAGES_BRANCH": "main",
    "CF_PAGES_COMMIT_SHA": "4b994dffe61f6a10e2de79cf96b6c0cc6c27185d",
    "CF_PAGES_URL": "https://c3168e86.wf-sow.pages.dev",
    "YARN_ENABLE_IMMUTABLE_INSTALLS": "false"
  }
}

request object of context object from function is empty.

How do I retrieve request.host and request.referer from fPage functions? 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

Thank you in advance for your assistance and your time.

You can read them like any other header on the Request - Headers - Web APIs | MDN

Basic example in Functions:

export function onRequest(context) {
  const req = context.request;
  const host = req.headers.get('host');
  const referrer = req.headers.get('referer');
}
3 Likes

Thanks @WalshyMVP it works

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.