Skip to content

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.

Create a file at functions/jamcomments/[[anything]].ts in your project:

├── functions
│ └── jamcomments
│ └── [[anything]].ts

Add this code to handle the proxy:

functions/jamcomments/[[anything]].ts
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.

Alternatively, use Cloudflare middleware by creating /functions/_middleware.ts. Since this runs on every request, check the path first:

functions/_middleware.ts
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();
}

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.

See Cloudflare’s Functions documentation for more details.