From bd39d66ea8d0d3de77ab3851658794f799176759 Mon Sep 17 00:00:00 2001 From: Ethan Carter Edwards Date: Sat, 28 Dec 2024 20:39:11 -0500 Subject: [PATCH] fix quote fetcher to add fallback --- pages/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pages/index.js b/pages/index.js index dbc9a0f..d2dad56 100644 --- a/pages/index.js +++ b/pages/index.js @@ -52,12 +52,18 @@ export async function getServerSideProps() { // local development // const res = { quote: "Quote", author: "Author" }; // const quotes = res; - - // production - const res = await fetch(`https://api.ethancedwards.com/quotes/v1`); - const quotes = await res.json(); + + let quotes; + let final; const posts = getAllPosts(); + await fetch(`https://api.ethancedwards.com/quotes/v1`).then((res) => { + final = res; + }).catch((error) => { + quotes = { quote: "Value your freedom or you will lose it, teaches history", author: "Richard Stallman" }; + }); + quotes = await final.json(); + return { props: { quotes, posts } }; }