Proxing discourse to root /blog with workers

So I was following cloudflare guide: https://blog.cloudflare.com/subdomains-vs-subdirectories-best-practices-workers-part-1/

I created a worker to proxy forum.ultraluz.com.br to https://ultraluz.com.br/blog but I still get 404 error codes on chrome?

I’ve allowed CORS and content security policy script src to allow my root domain…any help would be appreaciated… the following is my workers code:

// keep track of all our blog endpoints here
const myBlog = {
  hostname: "forum.ultraluz.com.br",
  targetSubdirectory: "/blog",
  assetsPathnames: ["/stylesheets/", "/assets/", "/images/"]
}

async function handleRequest(request) {
  // returns an empty string or a path if one exists
  const formatPath = (url) => {
    const pruned = url.pathname.split("/").filter(part => part)
    return pruned && pruned.length > 1 ? `${pruned.join("/")}` : ""
  }
  
  const parsedUrl = new URL(request.url)
  const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
  
  // if its blog html, get it
  if (requestMatches(myBlog.targetSubdirectory)) {
    console.log("this is a request for a blog document", parsedUrl.pathname)
    const targetPath = formatPath(parsedUrl)
    
    return fetch(`https://${myBlog.hostname}/${targetPath}`)
  }
  
  // if its blog assets, get them
  if ([myBlog.assetsPathnames].some(requestMatches)) {
    console.log("this is a request for blog assets", parsedUrl.pathname)
    const assetUrl = request.url.replace(parsedUrl.hostname, myBlog.hostname);

    return fetch(assetUrl)
  }

  console.log("this is a request to my root domain", parsedUrl.host, parsedUrl.pathname);
  // if its not a request blog related stuff, do nothing
  return fetch(request)
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

anyone??

It’s probably worth doing a console.log of the exact url you are proxying to in the preview. I would guess it’s not the url you think it is, leading to the 404 from your origin.

1 Like