fix(SW-663): Fixed leaking of live preview hash and removing preview pages
This commit is contained in:
42
lib/previewContext.ts
Normal file
42
lib/previewContext.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user