How to get the Page URL in the Cloudflare Worker

I want to create a worker which will exclude some pages. Not able to get the Page URL or even windows.location is not working.

It sounds like you might be looking for the “url” property of the “request” object. How you access it is going to vary slightly based on what your worker code looks like, but here’s an example:

addEventListener("fetch", event => {

I’m not looking for the request url, I want to apply custom rule to pages. E.g. if then apply rule1, if then apply rule 2. request url gives the request component url but not the page url.

For the path you need to create a URL object, like so:
const { pathname } = new URL(request.url);

This allows you to do logic based on that, like so:

if (pathname === '/page1') {
  return new Response('Walshy is cool');
} else if (pathname === '/page2') {
  return new Response('Walshy is the best');

When user navigate to different page then url do not change. Suppose user land to home page i.e. /index, then using menu if user click on page1 menu then /page1 page is loaded. Now worker is not able to get /page1 url.

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