fix(SW-663): Fixed caching issue by using new GraphQLClient for each request
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
|
||||
import fetchRetry from "fetch-retry"
|
||||
import { DocumentNode } from "graphql"
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
@@ -9,25 +10,26 @@ import { request as _request } from "./_request"
|
||||
|
||||
import { Data } from "@/types/request"
|
||||
|
||||
const client = new GraphQLClient(env.CMS_URL, {
|
||||
fetch: cache(async function (
|
||||
url: URL | RequestInfo,
|
||||
params: RequestInit | undefined
|
||||
) {
|
||||
const wrappedFetch = fetchRetry(fetch, {
|
||||
retries: 3,
|
||||
retryDelay: function (attempt, error, response) {
|
||||
return Math.pow(2, attempt) * 150 // 150, 300, 600
|
||||
},
|
||||
})
|
||||
return wrappedFetch(url, params)
|
||||
}),
|
||||
})
|
||||
|
||||
export async function request<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {},
|
||||
params?: RequestInit
|
||||
): Promise<Data<T>> {
|
||||
const previewHash = ContentstackLivePreview.hash
|
||||
|
||||
const cmsUrl = previewHash ? env.CMS_PREVIEW_URL : env.CMS_URL
|
||||
|
||||
const client = new GraphQLClient(cmsUrl, {
|
||||
fetch: cache(async function (url: URL | RequestInfo, params?: RequestInit) {
|
||||
const wrappedFetch = fetchRetry(fetch, {
|
||||
retries: 3,
|
||||
retryDelay: function (attempt, error, response) {
|
||||
return Math.pow(2, attempt) * 150 // 150, 300, 600
|
||||
},
|
||||
})
|
||||
return wrappedFetch(url, params)
|
||||
}),
|
||||
})
|
||||
|
||||
return _request(client, query, variables, params)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user