@@ -217,63 +217,40 @@ const render = async () => {
217
217
console . log ( 'Render function completed' )
218
218
}
219
219
220
- // For Next.js App Router compatibility
221
- const initApp = ( ) => {
222
- console . log ( 'Initializing app' )
223
- if ( typeof window !== 'undefined' ) {
224
- console . log ( 'Browser environment detected' )
225
-
226
- // Function to attempt rendering multiple times
227
- const attemptRender = ( attempts = 0 , maxAttempts = 5 ) => {
228
- console . log ( `Render attempt ${ attempts + 1 } of ${ maxAttempts } ` )
229
- render ( ) . then ( ( ) => {
230
- // Check if header and footer are properly rendered
231
- const header = document . getElementById ( DEVREV_CONTENT_WRAPPER_ID )
232
- const footer = document . getElementById ( 'fern-footer' )
233
- const headerRendered = header && header . children . length > 0
234
- const footerRendered = footer && footer . children . length > 0
235
-
236
- console . log ( 'Render check:' , {
237
- headerRendered : headerRendered ? 'yes' : 'no' ,
238
- footerRendered : footerRendered ? 'yes' : 'no'
239
- } )
240
-
241
- // If not rendered properly and we haven't reached max attempts, try again
242
- if ( ( ! headerRendered || ! footerRendered ) && attempts < maxAttempts - 1 ) {
243
- console . log ( 'Components not fully rendered, trying again in 500ms' )
244
- setTimeout ( ( ) => attemptRender ( attempts + 1 , maxAttempts ) , 500 )
245
- } else if ( attempts >= maxAttempts - 1 ) {
246
- console . log ( 'Max render attempts reached' )
247
- } else {
248
- console . log ( 'Components successfully rendered' )
220
+ let observations = 0
221
+
222
+ if ( document . readyState === 'loading' ) {
223
+ document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
224
+ await render ( )
225
+ setupMutationObserver ( )
226
+ } )
227
+ } else {
228
+ // DOM is already ready, run immediately
229
+ render ( ) . then ( ( ) => {
230
+ setupMutationObserver ( )
231
+ } )
232
+ }
233
+
234
+ function setupMutationObserver ( ) {
235
+ new MutationObserver ( async ( e , o ) => {
236
+ await render ( )
237
+ for ( const item of e ) {
238
+ if ( item . target instanceof HTMLElement ) {
239
+ const target = item . target
240
+ if ( target . id === 'fern-header' || target . id === 'fern-footer' ) {
241
+ if ( observations < 3 ) {
242
+ // react hydration will trigger a mutation event
243
+ observations ++
244
+ } else {
245
+ o . disconnect ( )
246
+ }
247
+ break
249
248
}
250
- } )
251
- }
252
-
253
- // Check if the DOM is already loaded
254
- if ( document . readyState === 'loading' ) {
255
- console . log ( 'DOM still loading, adding DOMContentLoaded listener' )
256
- document . addEventListener ( 'DOMContentLoaded' , ( ) => {
257
- console . log ( 'DOMContentLoaded event fired' )
258
- attemptRender ( )
259
- } )
260
- } else {
261
- // DOM already loaded, render immediately
262
- console . log ( 'DOM already loaded, rendering immediately' )
263
- attemptRender ( )
249
+ }
264
250
}
265
-
266
- // Also add a window load event to ensure everything is rendered
267
- window . addEventListener ( 'load' , ( ) => {
268
- console . log ( 'Window load event fired' )
269
- render ( )
270
- } )
271
- } else {
272
- console . log ( 'Not in browser environment, skipping initialization' )
273
- }
251
+ } ) . observe ( document . body , { childList : true , subtree : true } )
274
252
}
275
253
276
- // Initialize the app
277
- console . log ( 'Starting application initialization' )
278
- initApp ( )
254
+
255
+
279
256
0 commit comments