diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index 2b5bdb3bf8..cee9d2cfb4 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -233,17 +233,19 @@ export async function createRouter( ); } + let shouldUnsubscribe = false; for (const event of events) { res.write( `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`, ); if (event.type === 'completion') { - unsubscribe(); + shouldUnsubscribe = true; // Closing the event stream here would cause the frontend // to automatically reconnect because it lost connection. } } res.flush(); + if (shouldUnsubscribe) unsubscribe(); }, ); // When client closes connection we update the clients list