Using a Custom Host on Cloudflare Pages
On Cloudflare Pages, you can proxy JamComments requests through your domain using Cloudflare Functions. You have two options: create a dedicated function file, or use middleware.
Option 1: Create a Function File
Section titled “Option 1: Create a Function File”Create a file at functions/jamcomments/[[anything]].ts in your project:
├── functions│ └── jamcomments│ └── [[anything]].tsAdd this code to handle the proxy:
export function onRequest(context) { return fetch( context.request.url.replace( "https://your-site.com", "https://go.jamcomments.com", ), context.request, );}Replace https://your-site.com with your actual domain.
Option 2: Use Middleware
Section titled “Option 2: Use Middleware”Alternatively, use Cloudflare middleware by creating /functions/_middleware.ts. Since this runs on every request, check the path first:
export async function onRequest(context) { // Only rewrite JamComments paths if (context.request.url.includes("/jamcomments/")) { return fetch( context.request.url.replace( "https://your-site.com", "https://go.jamcomments.com", ), context.request, ); }
// Let all other requests pass through return context.next();}What This Does
Section titled “What This Does”Either approach causes Cloudflare to rewrite all /jamcomments/* requests to go through go.jamcomments.com. Your visitors will see your-site.com/jamcomments/* in their browsers, but the requests will actually be handled by JamComments.
Learn More
Section titled “Learn More”See Cloudflare’s Functions documentation for more details.