chore(SW-663): updated @contentstack/live-preview-utils to latest

This commit is contained in:
Erik Tiekstra
2024-11-07 13:29:12 +01:00
parent 953f860e5d
commit d8751b3fea
8 changed files with 109 additions and 63 deletions

View File

@@ -1,6 +1,5 @@
import "server-only"
// import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
import { ClientError, GraphQLClient } from "graphql-request"
import { Lang } from "@/constants/languages"

View File

@@ -4,7 +4,7 @@ import { GraphQLClient } from "graphql-request"
import { cache } from "react"
import { env } from "@/env/server"
import { getPreviewHash, isPreview } from "@/lib/previewContext"
import { getPreviewHash, isPreviewByUid } from "@/lib/previewContext"
import { request as _request } from "./_request"
@@ -15,10 +15,13 @@ export async function request<T>(
variables?: Record<string, any>,
params?: RequestInit
): Promise<Data<T>> {
const shouldUsePreview = isPreview(variables?.uid)
const shouldUsePreview = variables?.uid
? isPreviewByUid(variables.uid)
: false
const previewHash = getPreviewHash()
const cmsUrl = shouldUsePreview ? env.CMS_PREVIEW_URL : env.CMS_URL
// Creating a new client for each request to avoid conflicting parameters
const client = new GraphQLClient(cmsUrl, {
fetch: cache(async function (url: URL | RequestInfo, params?: RequestInit) {
const wrappedFetch = fetchRetry(fetch, {

View File

@@ -5,7 +5,7 @@ interface PreviewData {
uid: string
}
const getRef = cache((): { current: PreviewData | undefined } => ({
const getRef = cache((): { current?: PreviewData } => ({
current: undefined,
}))
@@ -17,8 +17,7 @@ const getRef = cache((): { current: PreviewData | undefined } => ({
*
* @param hash
*/
export function setPreviewData(data: PreviewData | undefined) {
console.log("SETTING HASH")
export function setPreviewData(data?: PreviewData) {
getRef().current = data
}
@@ -32,11 +31,12 @@ export function getPreviewHash() {
/**
* Check if the current request is a preview by comparing the uid
*/
export function isPreview(uid?: string) {
export function isPreviewByUid(uid: string) {
const data = getRef().current
if (uid && data?.hash) {
if (data?.hash) {
return data.uid === uid
}
return false
}