@@ -50,7 +50,7 @@ export function useOnEntryUpdatePostMessageEvent(): void {
5050 LIVE_PREVIEW_POST_MESSAGE_EVENTS . ON_CHANGE ,
5151 ( event ) => {
5252 try {
53- const { ssr, onChange } = Config . get ( ) ;
53+ const { ssr, onChange, stackDetails } = Config . get ( ) ;
5454 const event_type = event . data . _metadata ?. event_type ;
5555 setConfigFromParams ( {
5656 live_preview : event . data . hash ,
@@ -59,41 +59,57 @@ export function useOnEntryUpdatePostMessageEvent(): void {
5959 // This section will run when there is a change in the entry and the website is CSR
6060 if ( ! ssr && ! event_type ) {
6161 onChange ( ) ;
62- }
62+ }
6363
64- if ( isOpeningInNewTab ( ) ) {
65- if ( ! window ) {
64+ if ( isOpeningInNewTab ( ) ) {
65+ if ( ! window ) {
6666 PublicLogger . error ( "window is not defined" ) ;
6767 return ;
6868 } ;
69-
70- // This section will run when there is a change in the entry and the website is SSR
71- if ( ssr && ! event_type ) {
72- if ( window . location . href . includes ( "live_preview" ) ) {
69+
70+ if ( ssr && ! event_type ) {
71+ const url = new URL ( window . location . href ) ;
72+ let live_preview = url . searchParams . get ( "live_preview" ) ;
73+ let content_type_uid = url . searchParams . get ( "content_type_uid" ) ;
74+ let entry_uid = url . searchParams . get ( "entry_uid" ) ;
75+
76+ if ( live_preview && content_type_uid && entry_uid ) {
77+ // All required params are present, just reload
7378 window . location . reload ( ) ;
7479 } else {
75- const url = new URL ( window . location . href ) ;
76- url . searchParams . set ( "live_preview" , event . data . hash ) ;
77- url . searchParams . set ( "content_type_uid" , Config . get ( ) . stackDetails . contentTypeUid || "" ) ;
78- url . searchParams . set ( "entry_uid" , Config . get ( ) . stackDetails . entryUid || "" ) ;
80+ live_preview = event . data . hash ;
81+ content_type_uid = event . data . content_type_uid || stackDetails . $contentTypeUid ?. toString ( ) || "" ;
82+ entry_uid = event . data . entry_uid || stackDetails . $entryUid ?. toString ( ) || "" ;
83+ // Set missing params and redirect
84+ url . searchParams . set ( "live_preview" , live_preview ) ;
85+ if ( content_type_uid ) {
86+ url . searchParams . set (
87+ "content_type_uid" ,
88+ content_type_uid
89+ ) ;
90+ }
91+ if ( entry_uid ) {
92+ url . searchParams . set (
93+ "entry_uid" ,
94+ entry_uid
95+ ) ;
96+ }
7997 window . location . href = url . toString ( ) ;
8098 }
8199 }
82-
100+
83101 // This section will run when the hash changes and the website is SSR or CSR
84- if ( event_type === OnChangeLivePreviewPostMessageEventTypes . HASH_CHANGE ) {
102+ if ( event_type === OnChangeLivePreviewPostMessageEventTypes . HASH_CHANGE ) {
85103 const newUrl = new URL ( window . location . href ) ;
86104 newUrl . searchParams . set ( "live_preview" , event . data . hash ) ;
87105 window . history . pushState ( { } , "" , newUrl . toString ( ) ) ;
88106 }
89-
107+
90108 // This section will run when the URL of the page changes
91- if ( event_type === OnChangeLivePreviewPostMessageEventTypes . URL_CHANGE && event . data . url ) {
109+ if ( event_type === OnChangeLivePreviewPostMessageEventTypes . URL_CHANGE && event . data . url ) {
92110 window . location . href = event . data . url ;
93111 }
94112 }
95-
96-
97113 } catch ( error ) {
98114 PublicLogger . error ( "Error handling live preview update:" , error ) ;
99115 return ;
0 commit comments