Host product blog as /blog subdirectory, and proxy it from the edge

For a SAAS product, there will be many support content. Like Articles, Blogs, Tutorials, etc.
These pages are typically hosted on a different hosting platform than the main product (and the root domain).

The first question that is asked is, should these be in subdomain (https://blog.example.com) or a subdirectory (https://example.com/blog).

SEO experts recommend subdirectories. Google gives vague guideline. Many developers prefer subdomain.

Subdomain is fast. Add an A record or CNAME record to the DNS configuration. Thats it. The requests directly go to the hosting provider.

Subdirectories are slow. Each request will go through your web server, then gets proxied to the hosting platform. If you are just starting, you probably won’t be globally distributed. Making it unnecessarily slow for many users.

With Cloudflare worker, we can proxy the requests from the edge.

addEventListener('fetch', event => {
  var url = new URL(event.request.url);
  if (url.pathname.startsWith('/blog/') || url.pathname === '/blog') {
    handleBlog(event, url);
  } else {
    event.respondWith(fetch(event.request));
  }
})

async function handleBlog(event, url) {
  var originUrl = url.toString().replace(
    'https://www.codiva.io/blog',
   'https://example.github.io/codiva-blog');
  event.respondWith(fetch(originUrl)); 
}

Please let me know if I missed something. Or if it can be improved.

2 Likes

It worked only for the first page, but. It does not work for the links inside.
the links showed as https://mydomain.com/link
while this link of course does not exist on mydomain.
how to resolve this?

Did you find a solution?