feat(WEB-300): add retry for fetch calls

This commit is contained in:
Michael Zetterberg
2024-07-24 10:47:55 +02:00
parent 80c80fe1bd
commit ffa76cd8a2
4 changed files with 22 additions and 0 deletions

View File

@@ -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,

View File

@@ -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)
}),
})