@@ -166,6 +166,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
166166 const positionUpdateTimeouts = useRef < Map < string , number > > ( new Map ( ) )
167167 const isRejoiningRef = useRef < boolean > ( false )
168168 const pendingPositionUpdates = useRef < Map < string , any > > ( new Map ( ) )
169+ const deletedWorkflowIdRef = useRef < string | null > ( null )
169170
170171 const generateSocketToken = async ( ) : Promise < string > => {
171172 const res = await fetch ( '/api/auth/socket-token' , {
@@ -373,6 +374,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
373374 logger . warn ( `Workflow ${ data . workflowId } has been deleted` )
374375 setCurrentWorkflowId ( ( current ) => {
375376 if ( current === data . workflowId ) {
377+ deletedWorkflowIdRef . current = data . workflowId
376378 setPresenceUsers ( [ ] )
377379 return null
378380 }
@@ -500,7 +502,11 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
500502 if ( error ?. type === 'SESSION_ERROR' ) {
501503 const workflowId = urlWorkflowIdRef . current
502504
503- if ( workflowId && ! isRejoiningRef . current ) {
505+ if (
506+ workflowId &&
507+ ! isRejoiningRef . current &&
508+ deletedWorkflowIdRef . current !== workflowId
509+ ) {
504510 isRejoiningRef . current = true
505511 logger . info ( `Session expired, rejoining workflow: ${ workflowId } ` )
506512 socketInstance . emit ( 'join-workflow' , {
@@ -552,13 +558,25 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
552558 const hydrationPhase = useWorkflowRegistryStore ( ( s ) => s . hydration . phase )
553559
554560 useEffect ( ( ) => {
555- if ( ! socket || ! isConnected || ! urlWorkflowId ) return
561+ if ( ! socket || ! isConnected || ! urlWorkflowId ) {
562+ if ( ! urlWorkflowId ) {
563+ deletedWorkflowIdRef . current = null
564+ }
565+ return
566+ }
556567
557568 if ( hydrationPhase === 'creating' ) return
558569
559570 // Skip if already in the correct room
560571 if ( currentWorkflowId === urlWorkflowId ) return
561572
573+ // Prevent rejoining a workflow that was just deleted. The URL param may
574+ // still reference the old workflow while router.push() propagates.
575+ if ( deletedWorkflowIdRef . current === urlWorkflowId ) {
576+ return
577+ }
578+ deletedWorkflowIdRef . current = null
579+
562580 logger . info (
563581 `URL workflow changed from ${ currentWorkflowId } to ${ urlWorkflowId } , switching rooms`
564582 )
0 commit comments