diff --git a/lib/staticPathTimeout.js b/lib/staticPathTimeout.js index 9e6fa0b3..df39762f 100644 --- a/lib/staticPathTimeout.js +++ b/lib/staticPathTimeout.js @@ -1,3 +1,38 @@ -export const staticPathTimeout = (ms) => new Promise((resolve, reject) => -setTimeout(() => reject(new Error('Request timed out')), ms) -); +import { TOTAL_STATIC_POSTS } from "@/lib/constants"; +import { getAllPostsWithSlug } from "@/lib/api"; + +function staticPathTimeout(ms) { + return new Promise(resolve => setTimeout(() => resolve("dummy"), ms)); +} + +export async function getPostsWithRetry({maxRetries = 3, postType="article"}) { + for (let i = 0; i < maxRetries; i++) { + try { + const allPosts = await Promise.race([ + getAllPostsWithSlug( + postType, + process.env.NODE_ENV || + process.env.NEXT_PUBLIC_HOME_URL.indexOf("localhost") > -1 + ? 20 + : TOTAL_STATIC_POSTS + ), + staticPathTimeout(26000), // Set your desired timeout in milliseconds + ]); + + // If a dummy response is received, retry the request + if (allPosts === "dummy") { + continue; + } + + return allPosts; + } catch (error) { + console.error(error); + } + } + + // If the maximum number of retries is reached, return a default value + console.log( + `Failed to get posts after ${maxRetries} retries. Continuing with default value.` + ); + return { data: [] }; // Replace with a suitable default value for your application +} diff --git a/pages/post/[slug].js b/pages/post/[slug].js index f8566197..83bc16ff 100644 --- a/pages/post/[slug].js +++ b/pages/post/[slug].js @@ -31,7 +31,7 @@ import { TOTAL_STATIC_POSTS } from "@/lib/constants"; import PostHeader from "@/components/post-header"; import SocialShare from "@/components/SocialShare"; import PostGroupRow from "@/components/v4/layout/PostGroupRow"; -import { staticPathTimeout } from "@/lib/staticPathTimeout"; +import { getPostsWithRetry, staticPathTimeout } from "@/lib/staticPathTimeout"; const StickyFooterCTA = dynamic(() => import("@/components/StickyFooterCTA"), { ssr: false, }); @@ -449,21 +449,7 @@ export async function getStaticProps({ params, preview = null, locale }) { export async function getStaticPaths({ locales }) { - let allPosts=null - try { - allPosts = await Promise.race([ - getAllPostsWithSlug( - "article", - process.env.NODE_ENV || - process.env.NEXT_PUBLIC_HOME_URL.indexOf("localhost") > -1 - ? 20 - : TOTAL_STATIC_POSTS - ), - staticPathTimeout(26000) // Set your desired timeout in milliseconds - ]); - } catch (error) { - console.error(error); - } + const allPosts=await getPostsWithRetry({maxRetries: 3, postType:"article"}) return { paths: diff --git a/pages/toolbox/[slug].js b/pages/toolbox/[slug].js index 1b01d171..c0027942 100644 --- a/pages/toolbox/[slug].js +++ b/pages/toolbox/[slug].js @@ -655,22 +655,7 @@ export async function getStaticProps({ params, preview = null, locale }) { } export async function getStaticPaths() { - let allPosts = null; - - try { - allPosts = await Promise.race([ - getAllPostsWithSlug( - "tool", - process.env.NODE_ENV || - process.env.NEXT_PUBLIC_HOME_URL.indexOf("localhost") > -1 - ? 20 - : TOTAL_STATIC_POSTS - ), - staticPathTimeout(26000), // Set your desired timeout in milliseconds - ]); - } catch (error) { - console.error(error); - } + const allPosts = await getAllPostsWithSlug("tool", TOTAL_STATIC_POSTS); return { paths: