fix(SW-663): Fixed leaking of live preview hash and removing preview pages
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
|
||||
import fetchRetry from "fetch-retry"
|
||||
import { DocumentNode } from "graphql"
|
||||
import { GraphQLClient } from "graphql-request"
|
||||
import { cache } from "react"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
import { getPreviewHash, isPreview } from "@/lib/previewContext"
|
||||
|
||||
import { request as _request } from "./_request"
|
||||
|
||||
@@ -12,12 +12,12 @@ import { Data } from "@/types/request"
|
||||
|
||||
export async function request<T>(
|
||||
query: string | DocumentNode,
|
||||
variables?: {},
|
||||
variables?: Record<string, any>,
|
||||
params?: RequestInit
|
||||
): Promise<Data<T>> {
|
||||
const previewHash = ContentstackLivePreview.hash
|
||||
|
||||
const cmsUrl = previewHash ? env.CMS_PREVIEW_URL : env.CMS_URL
|
||||
const shouldUsePreview = isPreview(variables?.uid)
|
||||
const previewHash = getPreviewHash()
|
||||
const cmsUrl = shouldUsePreview ? env.CMS_PREVIEW_URL : env.CMS_URL
|
||||
|
||||
const client = new GraphQLClient(cmsUrl, {
|
||||
fetch: cache(async function (url: URL | RequestInfo, params?: RequestInit) {
|
||||
@@ -31,5 +31,19 @@ export async function request<T>(
|
||||
}),
|
||||
})
|
||||
|
||||
return _request(client, query, variables, params)
|
||||
const mergedParams =
|
||||
shouldUsePreview && previewHash
|
||||
? {
|
||||
...params,
|
||||
headers: {
|
||||
...params?.headers,
|
||||
live_preview: previewHash,
|
||||
preview_token: env.CMS_PREVIEW_TOKEN,
|
||||
},
|
||||
cache: undefined,
|
||||
next: undefined,
|
||||
}
|
||||
: params
|
||||
|
||||
return _request(client, query, variables, mergedParams)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user