Cloudflare Pages With Worker Re-Direction Origin Down

I have a pages project that I wish to display if my origin is down... I have a worker setup with the following code:

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

async function handleRequest(request) {
const originUrl = 'https://x.x.io/panel';

try {
// Try to fetch from the origin server
const response = await fetch(originUrl);

// If the origin server is not down, return the response
return response;
} catch (err) {
// If there's an error (e.g., the origin server is down),
// fetch from the backup source
return fetch('https://cloudflare-x.dev/');
}
}
Was this page helpful?