generated from nathanwoodburn/python-webserver-template
feat: Add backup api for cross site blocks
All checks were successful
Build Docker / BuildImage (push) Successful in 31s
All checks were successful
Build Docker / BuildImage (push) Successful in 31s
This commit is contained in:
parent
c3db03e67d
commit
a26a7938ad
@ -42,14 +42,41 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function fetchJoke() {
|
async function fetchJoke() {
|
||||||
const response = await fetch('https://icanhazdadjoke.com', {
|
try {
|
||||||
headers: {
|
let response;
|
||||||
'Accept': 'application/json'
|
|
||||||
|
// Attempt to fetch the external API first
|
||||||
|
try {
|
||||||
|
response = await fetch('https://icanhazdadjoke.com/', {
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// If the external API response is not ok, fallback to local API
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('External API failed');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('External API failed:', error);
|
||||||
|
// Fall back to local API after external API failure
|
||||||
|
response = await fetch('/api/joke');
|
||||||
|
|
||||||
|
// If local API also fails, throw an error
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Both external and local APIs failed');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
const data = await response.json();
|
// Parse and display the joke
|
||||||
document.getElementById('joke').innerText = data.joke;
|
const data = await response.json();
|
||||||
|
document.getElementById('joke').innerText = data.joke;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch joke:', error);
|
||||||
|
document.getElementById('joke').innerText = "Oops! Couldn't fetch a joke right now.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user