@@ -354,37 +354,63 @@ export default function Home() {
354354 } ;
355355 } , [ topics ] ) ; // Add topics dependency to find associated topic
356356
357- // Handle deep linking via query params
357+ // Handle deep linking via query params and path (SPA fallback)
358358 useEffect ( ( ) => {
359359 if ( ! router . isReady ) return ;
360360
361361 const { chatRoomId, postId } = router . query ;
362+ const path = router . asPath . split ( '?' ) [ 0 ] ;
362363
364+ // Handle Query Params (Legacy/Direct)
363365 if ( chatRoomId ) {
364- // Remove query param to clean URL
365366 router . replace ( '/' , undefined , { shallow : true } ) ;
366-
367367 const event = new CustomEvent ( 'openChatRoom' , { detail : { chatRoomId } } ) ;
368- // Small delay to ensure listeners are ready/state is settled
369368 setTimeout ( ( ) => window . dispatchEvent ( event ) , 100 ) ;
370- } else if ( postId ) {
371- // Remove query param
372- router . replace ( '/' , undefined , { shallow : true } ) ;
369+ return ;
370+ }
373371
372+ if ( postId ) {
373+ router . replace ( '/' , undefined , { shallow : true } ) ;
374374 const event = new CustomEvent ( 'openPost' , { detail : { postId } } ) ;
375375 setTimeout ( ( ) => window . dispatchEvent ( event ) , 100 ) ;
376- } else if ( router . query . topicId ) {
376+ return ;
377+ }
378+
379+ if ( router . query . topicId ) {
377380 const topicId = router . query . topicId as string ;
378- // Remove query param
379381 router . replace ( '/' , undefined , { shallow : true } ) ;
380-
381- // Find and select the topic
382382 const topic = topics . find ( t => t . id === topicId ) ;
383383 if ( topic ) {
384384 handleTopicSelect ( topic ) ;
385385 }
386+ return ;
387+ }
388+
389+ // Handle Path-based Routing (SPA Fallback)
390+ // Only if we are effectively on the root page logic but the path is different
391+ if ( path !== '/' ) {
392+ // Regex for /post/[id]
393+ const postMatch = path . match ( / ^ \/ p o s t \/ ( [ a - z A - Z 0 - 9 _ - ] + ) $ / ) ;
394+ if ( postMatch ) {
395+ const id = postMatch [ 1 ] ;
396+ const event = new CustomEvent ( 'openPost' , { detail : { postId : id } } ) ;
397+ setTimeout ( ( ) => window . dispatchEvent ( event ) , 100 ) ;
398+ return ;
399+ }
400+
401+ // Regex for /chat-room/[id]
402+ const chatMatch = path . match ( / ^ \/ c h a t - r o o m \/ ( [ a - z A - Z 0 - 9 _ - ] + ) $ / ) ;
403+ if ( chatMatch ) {
404+ const id = chatMatch [ 1 ] ;
405+ const event = new CustomEvent ( 'openChatRoom' , { detail : { chatRoomId : id } } ) ;
406+ setTimeout ( ( ) => window . dispatchEvent ( event ) , 100 ) ;
407+ return ;
408+ }
409+
410+ // If it's an unknown path served by index.html fallback, redirect to 404
411+ router . replace ( '/404' ) ;
386412 }
387- } , [ router . isReady , router . query , topics ] ) ;
413+ } , [ router . isReady , router . query , router . asPath , topics ] ) ;
388414
389415
390416 // Socket listeners for badge counts
0 commit comments