fix(SW-663): Fixed leaking of live preview hash and removing preview pages

This commit is contained in:
Erik Tiekstra
2024-11-07 12:49:13 +01:00
parent 0465f8e450
commit 953f860e5d
16 changed files with 117 additions and 301 deletions

42
lib/previewContext.ts Normal file
View File

@@ -0,0 +1,42 @@
import { cache } from "react"
interface PreviewData {
hash: string
uid: string
}
const getRef = cache((): { current: PreviewData | undefined } => ({
current: undefined,
}))
/**
* Set the preview hash for the current request
*
* It works kind of like React's context,
* but on the server side, per request.
*
* @param hash
*/
export function setPreviewData(data: PreviewData | undefined) {
console.log("SETTING HASH")
getRef().current = data
}
/**
* Get the preview hash set for the current request
*/
export function getPreviewHash() {
return getRef().current?.hash
}
/**
* Check if the current request is a preview by comparing the uid
*/
export function isPreview(uid?: string) {
const data = getRef().current
if (uid && data?.hash) {
return data.uid === uid
}
return false
}