Get the requested path as a string

Hello, I’m trying to get the requested path as a string. So far I have found that window.location.pathname is supposed work, but that says that window is not defined. Here is my code:

async function passPath() {
  const url = new URL(window.location.pathname);
  path = url.pathname;
  return path;
}

If I replace window.location.pathname with a string like “http://google.com/hello” it will return /hello but I am struggling to get window.location.pathname to be the requested URL like worker.example/hello.
Any help is greatly appreciated!

window is not available with Cloudflare, you’ll need to access request.url.

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