diff --git a/pynecone/compiler/templates.py b/pynecone/compiler/templates.py index 19c747b1c..879af88c5 100644 --- a/pynecone/compiler/templates.py +++ b/pynecone/compiler/templates.py @@ -164,8 +164,14 @@ USE_EFFECT = join( " if(!isReady) {{", " return;", " }}", - f" if (!{SOCKET}.current) {{{{", - f" connect({SOCKET}, {{state}}, {{set_state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})", + " const reconnectSocket = () => {{", + f" {SOCKET}.current.reconnect()", + " }}", + f" if (typeof {SOCKET}.current !== 'undefined') {{{{", + f" if (!{SOCKET}.current) {{{{", + f" window.addEventListener('focus', reconnectSocket)", + f" connect({SOCKET}, {{state}}, {{set_state}}, {RESULT}, {SET_RESULT}, {ROUTER}, {EVENT_ENDPOINT})", + " }}", " }}", " const update = async () => {{", f" if ({RESULT}.{STATE} != null) {{{{",