Cloudflare Page loaded from Workers as maintenance page for specific paths.
Using workers we can define the Response Status Code and the Target Endpoint to fetch the content to the maintenance page, without redirect or rewrite the URL
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url);
// Check if the request path matches certain criteria
if (url.pathname.startsWith('/maintenance-page/')) {
const targetEndpoint = 'https://maintenance-page-c4h.pages.dev'; // External endpoint to fetch from
// Fetch the content from the target endpoint
const response = await fetch(targetEndpoint);
const content = await response.text();
return new Response(content, {
status: 200,
headers: { 'Content-Type': 'text/html' }
});
} else {
// If the path does not match, simply pass the request through without any changes
return fetch(request);
}
}
We'll be back soon!