diff --git a/lib/api/index.ts b/lib/api/index.ts index 5c14968e6..9455a85ed 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -1,4 +1,5 @@ import merge from "deepmerge" +import fetchRetry from "fetch-retry" import { env } from "@/env/server" @@ -18,6 +19,13 @@ const defaultOptions: RequestInit = { mode: "cors", } +const fetch = fetchRetry(global.fetch, { + retries: 3, + retryDelay: function (attempt, error, response) { + return Math.pow(2, attempt) * 150 // 150, 300, 600 + }, +}) + export async function get( endpoint: Endpoint, options: RequestOptionsWithOutBody, diff --git a/lib/graphql/request.ts b/lib/graphql/request.ts index 71952e7af..a4d6296b0 100644 --- a/lib/graphql/request.ts +++ b/lib/graphql/request.ts @@ -1,3 +1,4 @@ +import fetchRetry from "fetch-retry" import { DocumentNode } from "graphql" import { GraphQLClient } from "graphql-request" import { cache } from "react" @@ -13,6 +14,12 @@ const client = new GraphQLClient(env.CMS_URL, { url: URL | RequestInfo, params: RequestInit | undefined ) { + const fetch = fetchRetry(global.fetch, { + retries: 3, + retryDelay: function (attempt, error, response) { + return Math.pow(2, attempt) * 150 // 150, 300, 600 + }, + }) return fetch(url, params) }), }) diff --git a/package-lock.json b/package-lock.json index bdfaf06a5..1aed08a3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "clean-deep": "^3.4.0", "dayjs": "^1.11.10", "deepmerge": "^4.3.1", + "fetch-retry": "^6.0.0", "graphql": "^16.8.1", "graphql-request": "^6.1.0", "graphql-tag": "^2.12.6", @@ -9721,6 +9722,11 @@ "pend": "~1.2.0" } }, + "node_modules/fetch-retry": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz", + "integrity": "sha512-BUFj1aMubgib37I3v4q78fYo63Po7t4HUPTpQ6/QE6yK6cIQrP+W43FYToeTEyg5m2Y7eFUtijUuAv/PDlWuag==" + }, "node_modules/figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", diff --git a/package.json b/package.json index 20d9d62e2..31b768769 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "clean-deep": "^3.4.0", "dayjs": "^1.11.10", "deepmerge": "^4.3.1", + "fetch-retry": "^6.0.0", "graphql": "^16.8.1", "graphql-request": "^6.1.0", "graphql-tag": "^2.12.6",