Skip to content

Commit

Permalink
retry when building
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeFulton committed Apr 19, 2024
1 parent 5b6e17f commit 92ac4a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
41 changes: 38 additions & 3 deletions lib/staticPathTimeout.js
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 2 additions & 16 deletions pages/post/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 1 addition & 16 deletions pages/toolbox/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 92ac4a2

Please sign in to comment.