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,4 +1,4 @@
import { isPreview, setPreviewData } from "@/lib/previewContext"
import { setPreviewData } from "@/lib/previewContext"
import InitLivePreview from "@/components/LivePreview"
@@ -9,11 +9,10 @@ export default function PreviewPage({
params,
}: PageArgs<UIDParams, URLSearchParams>) {
const shouldInitializePreview = searchParams.isPreview === "true"
const isInitialized = isPreview()
if (searchParams.live_preview) {
setPreviewData({ hash: searchParams.live_preview, uid: params.uid })
}
return shouldInitializePreview && !isInitialized ? <InitLivePreview /> : null
return shouldInitializePreview ? <InitLivePreview /> : null
}

View File

@@ -1,5 +1,3 @@
import React from "react"
import styles from "./layout.module.css"
import {

View File

@@ -1,6 +1,6 @@
"use client"
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
import ContentstackLivePreview from "@contentstack/live-preview-utils"
import { useEffect } from "react"
export default function InitLivePreview() {

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
}

143
package-lock.json generated
View File

@@ -10,7 +10,7 @@
"hasInstallScript": true,
"dependencies": {
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.24",
"@contentstack/live-preview-utils": "^2.0.4",
"@contentstack/live-preview-utils": "^3.0.0",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^3.3.4",
"@netlify/plugin-nextjs": "^5.1.1",
@@ -2460,17 +2460,56 @@
}
},
"node_modules/@contentstack/live-preview-utils": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@contentstack/live-preview-utils/-/live-preview-utils-2.0.4.tgz",
"integrity": "sha512-+56ExpzsOau5MEEg6sy1czaQMzPmEZAyGYx+LIyqeKKCbxFmTHcvg4Ag4vJ7KUf4LCtoGMUUT/Mkf6hqSkCvDA==",
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@contentstack/live-preview-utils/-/live-preview-utils-3.0.0.tgz",
"integrity": "sha512-zu2Xxg02MaTYMp+H+Q7YO5NfhhGe0wRuanpmF6Z3NUWS/oebr0yvJtqTctbE7NVw/dYpRRkviMe7p9CpIg5oaQ==",
"dependencies": {
"@preact/compat": "^17.1.2",
"@preact/signals": "^1.2.2",
"classnames": "^2.5.1",
"deepsignal": "^1.5.0",
"goober": "^2.1.14",
"just-camel-case": "^4.0.2",
"lodash-es": "^4.17.21",
"morphdom": "^2.6.1",
"mustache": "^4.2.0",
"post-robot": "8.0.31",
"preact": "^10.19.5",
"uuid": "^8.3.2"
},
"optionalDependencies": {
"@rollup/rollup-linux-x64-gnu": "4.9.5"
}
},
"node_modules/@contentstack/live-preview-utils/node_modules/deepsignal": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/deepsignal/-/deepsignal-1.5.0.tgz",
"integrity": "sha512-bFywDpBUUWMs576H2dgLFLLFuQ/UWXbzHfKD98MZTfGsl7+twIzvz4ihCNrRrZ/Emz3kqJaNIAp5eBWUEWhnAw==",
"peerDependencies": {
"@preact/signals": "^1.1.4",
"@preact/signals-core": "^1.5.1",
"@preact/signals-react": "^1.3.8 || ^2.0.0",
"preact": "^10.16.0"
},
"peerDependenciesMeta": {
"@preact/signals": {
"optional": true
},
"@preact/signals-core": {
"optional": true
},
"@preact/signals-react": {
"optional": true
},
"preact": {
"optional": true
}
}
},
"node_modules/@contentstack/live-preview-utils/node_modules/preact": {
"version": "10.24.3",
"resolved": "https://registry.npmjs.org/preact/-/preact-10.24.3.tgz",
"integrity": "sha512-Z2dPnBnMUfyQfSQ+GBdsGa16hz35YmLmtTLhM169uW944hYL6xzTYkJjC07j+Wosz733pMWx0fgON3JNw1jJQA==",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/preact"
}
},
"node_modules/@cspotcode/source-map-support": {
@@ -3762,6 +3801,38 @@
"node": ">=12"
}
},
"node_modules/@preact/compat": {
"version": "17.1.2",
"resolved": "https://registry.npmjs.org/@preact/compat/-/compat-17.1.2.tgz",
"integrity": "sha512-7pOZN9lMDDRQ+6aWvjwTp483KR8/zOpfS83wmOo3zfuLKdngS8/5RLbsFWzFZMGdYlotAhX980hJ75bjOHTwWg==",
"peerDependencies": {
"preact": "*"
}
},
"node_modules/@preact/signals": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/@preact/signals/-/signals-1.3.0.tgz",
"integrity": "sha512-EOMeg42SlLS72dhoq6Vjq08havnLseWmPQ8A0YsgIAqMgWgx7V1a39+Pxo6i7SY5NwJtH4849JogFq3M67AzWg==",
"dependencies": {
"@preact/signals-core": "^1.7.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/preact"
},
"peerDependencies": {
"preact": "10.x"
}
},
"node_modules/@preact/signals-core": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/@preact/signals-core/-/signals-core-1.8.0.tgz",
"integrity": "sha512-OBvUsRZqNmjzCZXWLxkZfhcgT+Fk8DDcT/8vD6a1xhDemodyy87UJRJfASMuSD8FaAIeGgGm85ydXhm7lr4fyA==",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/preact"
}
},
"node_modules/@radix-ui/primitive": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@radix-ui/primitive/-/primitive-1.1.0.tgz",
@@ -5592,6 +5663,18 @@
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
}
},
"node_modules/@rollup/rollup-linux-x64-gnu": {
"version": "4.9.5",
"resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.9.5.tgz",
"integrity": "sha512-Dq1bqBdLaZ1Gb/l2e5/+o3B18+8TI9ANlA1SkejZqDgdU/jK/ThYaMPMJpVMMXy2uRHvGKbkz9vheVGdq3cJfA==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
]
},
"node_modules/@rushstack/eslint-patch": {
"version": "1.10.2",
"resolved": "https://registry.npmjs.org/@rushstack/eslint-patch/-/eslint-patch-1.10.2.tgz",
@@ -8180,6 +8263,11 @@
"url": "https://joebell.co.uk"
}
},
"node_modules/classnames": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/classnames/-/classnames-2.5.1.tgz",
"integrity": "sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow=="
},
"node_modules/clean-deep": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/clean-deep/-/clean-deep-3.4.0.tgz",
@@ -8767,22 +8855,6 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
"node_modules/cross-domain-safe-weakmap": {
"version": "1.0.29",
"resolved": "https://registry.npmjs.org/cross-domain-safe-weakmap/-/cross-domain-safe-weakmap-1.0.29.tgz",
"integrity": "sha512-VLoUgf2SXnf3+na8NfeUFV59TRZkIJqCIATaMdbhccgtnTlSnHXkyTRwokngEGYdQXx8JbHT9GDYitgR2sdjuA==",
"dependencies": {
"cross-domain-utils": "^2.0.0"
}
},
"node_modules/cross-domain-utils": {
"version": "2.0.38",
"resolved": "https://registry.npmjs.org/cross-domain-utils/-/cross-domain-utils-2.0.38.tgz",
"integrity": "sha512-zZfi3+2EIR9l4chrEiXI2xFleyacsJf8YMLR1eJ0Veb5FTMXeJ3DpxDjZkto2FhL/g717WSELqbptNSo85UJDw==",
"dependencies": {
"zalgo-promise": "^1.0.11"
}
},
"node_modules/cross-fetch": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
@@ -13679,11 +13751,6 @@
"node": ">=4.0"
}
},
"node_modules/just-camel-case": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/just-camel-case/-/just-camel-case-4.0.2.tgz",
"integrity": "sha512-df6QI/EIq+6uHe/wtaa9Qq7/pp4wr4pJC/r1+7XhVL6m5j03G6h9u9/rIZr8rDASX7CxwDPQnZjffCo2e6PRLw=="
},
"node_modules/keygrip": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.1.0.tgz",
@@ -15270,11 +15337,6 @@
"node": ">= 0.8"
}
},
"node_modules/morphdom": {
"version": "2.7.2",
"resolved": "https://registry.npmjs.org/morphdom/-/morphdom-2.7.2.tgz",
"integrity": "sha512-Dqb/lHFyTi7SZpY0a5R4I/0Edo+iPMbaUexsHHsLAByyixCDiLHPHyVoKVmrpL0THcT7V9Cgev9y21TQYq6wQg=="
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@@ -16256,16 +16318,6 @@
"node": ">= 0.4"
}
},
"node_modules/post-robot": {
"version": "8.0.31",
"resolved": "https://registry.npmjs.org/post-robot/-/post-robot-8.0.31.tgz",
"integrity": "sha512-nUhtKgtmcgyuPm4RnIhUB3gsDYJBHOgFry3TvOxhIHpgfwYY/T69d4oB90tw4YUllFZUUwqLEv1Wgyg6eOoJ7A==",
"dependencies": {
"cross-domain-safe-weakmap": "^1.0.1",
"cross-domain-utils": "^2.0.0",
"zalgo-promise": "^1.0.3"
}
},
"node_modules/postcss": {
"version": "8.4.31",
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.4.31.tgz",
@@ -20000,11 +20052,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/zalgo-promise": {
"version": "1.0.48",
"resolved": "https://registry.npmjs.org/zalgo-promise/-/zalgo-promise-1.0.48.tgz",
"integrity": "sha512-LLHANmdm53+MucY9aOFIggzYtUdkSBFxUsy4glTTQYNyK6B3uCPWTbfiGvSrEvLojw0mSzyFJ1/RRLv+QMNdzQ=="
},
"node_modules/zod": {
"version": "3.22.4",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",

View File

@@ -25,7 +25,7 @@
},
"dependencies": {
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.24",
"@contentstack/live-preview-utils": "^2.0.4",
"@contentstack/live-preview-utils": "^3.0.0",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^3.3.4",
"@netlify/plugin-nextjs": "^5.1.1",