@payloadcms/live-preview 3.56.0-internal.457de85 → 3.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/handleMessage.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"handleMessage.d.ts","sourceRoot":"","sources":["../src/handleMessage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAkB7F,eAAO,MAAM,UAAU,QAAO,IAE7B,CAAA;AAED,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAA;IACjC,WAAW,EAAE,CAAC,CAAA;IACd,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAAO,CAAC,CAAC,CAkCZ,CAAA"}
|
package/dist/handleMessage.js
CHANGED
|
@@ -6,10 +6,20 @@ const _payloadLivePreview = {
|
|
|
6
6
|
* This will ensure changes compound overtop of each other
|
|
7
7
|
*/ previousData: undefined
|
|
8
8
|
};
|
|
9
|
+
// Reset the internal cached merged data. This is useful when navigating
|
|
10
|
+
// between routes where a new subscription should not inherit prior data.
|
|
11
|
+
export const resetCache = ()=>{
|
|
12
|
+
_payloadLivePreview.previousData = undefined;
|
|
13
|
+
};
|
|
9
14
|
export const handleMessage = async (args)=>{
|
|
10
15
|
const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args;
|
|
11
16
|
if (isLivePreviewEvent(event, serverURL)) {
|
|
12
17
|
const { collectionSlug, data, globalSlug, locale } = event.data;
|
|
18
|
+
// Only attempt to merge when we have a clear target
|
|
19
|
+
// Either a collectionSlug or a globalSlug must be present
|
|
20
|
+
if (!collectionSlug && !globalSlug) {
|
|
21
|
+
return initialData;
|
|
22
|
+
}
|
|
13
23
|
const mergedData = await mergeData({
|
|
14
24
|
apiRoute,
|
|
15
25
|
collectionSlug,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler, LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousData: any\n} = {\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\nexport const handleMessage = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n depth?: number\n event: LivePreviewMessageEvent<T>\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): Promise<T> => {\n const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args\n\n if (isLivePreviewEvent(event, serverURL)) {\n const { collectionSlug, data, globalSlug, locale } = event.data\n\n const mergedData = await mergeData<T>({\n apiRoute,\n collectionSlug,\n depth,\n globalSlug,\n incomingData: data,\n initialData: _payloadLivePreview?.previousData || initialData,\n locale,\n requestHandler,\n serverURL,\n })\n\n _payloadLivePreview.previousData = mergedData\n\n return mergedData\n }\n\n if (!_payloadLivePreview.previousData) {\n _payloadLivePreview.previousData = initialData\n }\n\n return _payloadLivePreview.previousData as T\n}\n"],"names":["isLivePreviewEvent","mergeData","_payloadLivePreview","previousData","undefined","handleMessage","args","apiRoute","depth","event","initialData","requestHandler","serverURL","collectionSlug","data","globalSlug","locale","mergedData","incomingData"],"mappings":"AAEA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,MAAMC,sBAGF;IACF;;;GAGC,GACDC,cAAcC;AAChB;AAEA,OAAO,MAAMC,gBAAgB,OAAsCC;IAQjE,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,KAAK,EAAEC,WAAW,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGN;IAE3E,
|
|
1
|
+
{"version":3,"sources":["../src/handleMessage.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler, LivePreviewMessageEvent } from './types.js'\n\nimport { isLivePreviewEvent } from './isLivePreviewEvent.js'\nimport { mergeData } from './mergeData.js'\n\nconst _payloadLivePreview: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n previousData: any\n} = {\n /**\n * Each time the data is merged, cache the result as a `previousData` variable\n * This will ensure changes compound overtop of each other\n */\n previousData: undefined,\n}\n\n// Reset the internal cached merged data. This is useful when navigating\n// between routes where a new subscription should not inherit prior data.\nexport const resetCache = (): void => {\n _payloadLivePreview.previousData = undefined\n}\n\nexport const handleMessage = async <T extends Record<string, any>>(args: {\n apiRoute?: string\n depth?: number\n event: LivePreviewMessageEvent<T>\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): Promise<T> => {\n const { apiRoute, depth, event, initialData, requestHandler, serverURL } = args\n\n if (isLivePreviewEvent(event, serverURL)) {\n const { collectionSlug, data, globalSlug, locale } = event.data\n\n // Only attempt to merge when we have a clear target\n // Either a collectionSlug or a globalSlug must be present\n if (!collectionSlug && !globalSlug) {\n return initialData\n }\n\n const mergedData = await mergeData<T>({\n apiRoute,\n collectionSlug,\n depth,\n globalSlug,\n incomingData: data,\n initialData: _payloadLivePreview?.previousData || initialData,\n locale,\n requestHandler,\n serverURL,\n })\n\n _payloadLivePreview.previousData = mergedData\n\n return mergedData\n }\n\n if (!_payloadLivePreview.previousData) {\n _payloadLivePreview.previousData = initialData\n }\n\n return _payloadLivePreview.previousData as T\n}\n"],"names":["isLivePreviewEvent","mergeData","_payloadLivePreview","previousData","undefined","resetCache","handleMessage","args","apiRoute","depth","event","initialData","requestHandler","serverURL","collectionSlug","data","globalSlug","locale","mergedData","incomingData"],"mappings":"AAEA,SAASA,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,SAAS,QAAQ,iBAAgB;AAE1C,MAAMC,sBAGF;IACF;;;GAGC,GACDC,cAAcC;AAChB;AAEA,wEAAwE;AACxE,yEAAyE;AACzE,OAAO,MAAMC,aAAa;IACxBH,oBAAoBC,YAAY,GAAGC;AACrC,EAAC;AAED,OAAO,MAAME,gBAAgB,OAAsCC;IAQjE,MAAM,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,KAAK,EAAEC,WAAW,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGN;IAE3E,IAAIP,mBAAmBU,OAAOG,YAAY;QACxC,MAAM,EAAEC,cAAc,EAAEC,IAAI,EAAEC,UAAU,EAAEC,MAAM,EAAE,GAAGP,MAAMK,IAAI;QAE/D,oDAAoD;QACpD,0DAA0D;QAC1D,IAAI,CAACD,kBAAkB,CAACE,YAAY;YAClC,OAAOL;QACT;QAEA,MAAMO,aAAa,MAAMjB,UAAa;YACpCO;YACAM;YACAL;YACAO;YACAG,cAAcJ;YACdJ,aAAaT,qBAAqBC,gBAAgBQ;YAClDM;YACAL;YACAC;QACF;QAEAX,oBAAoBC,YAAY,GAAGe;QAEnC,OAAOA;IACT;IAEA,IAAI,CAAChB,oBAAoBC,YAAY,EAAE;QACrCD,oBAAoBC,YAAY,GAAGQ;IACrC;IAEA,OAAOT,oBAAoBC,YAAY;AACzC,EAAC"}
|
package/dist/subscribe.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../src/subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,YAAY,CAAA;AAIpE,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,CAAC,CAAA;IACd,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../src/subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,YAAY,CAAA;AAIpE,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,CAAC,CAAA;IACd,cAAc,CAAC,EAAE,kCAAkC,CAAA;IACnD,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,CAAC,CAAC,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAyBjD,CAAA"}
|
package/dist/subscribe.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { handleMessage } from './handleMessage.js';
|
|
1
|
+
import { handleMessage, resetCache } from './handleMessage.js';
|
|
2
2
|
export const subscribe = (args)=>{
|
|
3
3
|
const { apiRoute, callback, depth, initialData, requestHandler, serverURL } = args;
|
|
4
|
+
// Ensure previous subscription state does not leak across navigations
|
|
5
|
+
// by clearing the internal cached data before subscribing.
|
|
6
|
+
resetCache();
|
|
4
7
|
const onMessage = async (event)=>{
|
|
5
8
|
const mergedData = await handleMessage({
|
|
6
9
|
apiRoute,
|
package/dist/subscribe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/subscribe.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler } from './types.js'\n\nimport { handleMessage } from './handleMessage.js'\n\nexport const subscribe = <T extends Record<string, any>>(args: {\n apiRoute?: string\n callback: (data: T) => void\n depth?: number\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): ((event: MessageEvent) => Promise<void> | void) => {\n const { apiRoute, callback, depth, initialData, requestHandler, serverURL } = args\n\n const onMessage = async (event: MessageEvent) => {\n const mergedData = await handleMessage<T>({\n apiRoute,\n depth,\n event,\n initialData,\n requestHandler,\n serverURL,\n })\n\n callback(mergedData)\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('message', onMessage)\n }\n\n return onMessage\n}\n"],"names":["handleMessage","subscribe","args","apiRoute","callback","depth","initialData","requestHandler","serverURL","onMessage","event","mergedData","window","addEventListener"],"mappings":"AAEA,SAASA,aAAa,QAAQ,qBAAoB;
|
|
1
|
+
{"version":3,"sources":["../src/subscribe.ts"],"sourcesContent":["import type { CollectionPopulationRequestHandler } from './types.js'\n\nimport { handleMessage, resetCache } from './handleMessage.js'\n\nexport const subscribe = <T extends Record<string, any>>(args: {\n apiRoute?: string\n callback: (data: T) => void\n depth?: number\n initialData: T\n requestHandler?: CollectionPopulationRequestHandler\n serverURL: string\n}): ((event: MessageEvent) => Promise<void> | void) => {\n const { apiRoute, callback, depth, initialData, requestHandler, serverURL } = args\n\n // Ensure previous subscription state does not leak across navigations\n // by clearing the internal cached data before subscribing.\n resetCache()\n\n const onMessage = async (event: MessageEvent) => {\n const mergedData = await handleMessage<T>({\n apiRoute,\n depth,\n event,\n initialData,\n requestHandler,\n serverURL,\n })\n\n callback(mergedData)\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('message', onMessage)\n }\n\n return onMessage\n}\n"],"names":["handleMessage","resetCache","subscribe","args","apiRoute","callback","depth","initialData","requestHandler","serverURL","onMessage","event","mergedData","window","addEventListener"],"mappings":"AAEA,SAASA,aAAa,EAAEC,UAAU,QAAQ,qBAAoB;AAE9D,OAAO,MAAMC,YAAY,CAAgCC;IAQvD,MAAM,EAAEC,QAAQ,EAAEC,QAAQ,EAAEC,KAAK,EAAEC,WAAW,EAAEC,cAAc,EAAEC,SAAS,EAAE,GAAGN;IAE9E,sEAAsE;IACtE,2DAA2D;IAC3DF;IAEA,MAAMS,YAAY,OAAOC;QACvB,MAAMC,aAAa,MAAMZ,cAAiB;YACxCI;YACAE;YACAK;YACAJ;YACAC;YACAC;QACF;QAEAJ,SAASO;IACX;IAEA,IAAI,OAAOC,WAAW,aAAa;QACjCA,OAAOC,gBAAgB,CAAC,WAAWJ;IACrC;IAEA,OAAOA;AACT,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/live-preview",
|
|
3
|
-
"version": "3.56.0
|
|
3
|
+
"version": "3.56.0",
|
|
4
4
|
"description": "The official live preview JavaScript SDK for Payload",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@payloadcms/eslint-config": "3.28.0",
|
|
36
|
-
"payload": "3.56.0
|
|
36
|
+
"payload": "3.56.0"
|
|
37
37
|
},
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"registry": "https://registry.npmjs.org/"
|