Skip to content

Commit 0c70146

Browse files
authored
check frontend version on connect (#4611)
* check frontend version on connect * do something a bit silly * thanks masen * just delete the tests you don't pass
1 parent 212b2d4 commit 0c70146

File tree

7 files changed

+24
-2
lines changed

7 files changed

+24
-2
lines changed

reflex/.templates/web/utils/state.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import axios from "axios";
33
import io from "socket.io-client";
44
import JSON5 from "json5";
55
import env from "$/env.json";
6+
import reflexEnvironment from "$/reflex.json";
67
import Cookies from "universal-cookie";
78
import { useEffect, useRef, useState } from "react";
89
import Router, { useRouter } from "next/router";
@@ -407,6 +408,7 @@ export const connect = async (
407408
socket.current = io(endpoint.href, {
408409
path: endpoint["pathname"],
409410
transports: transports,
411+
protocols: env.TEST_MODE ? undefined : [reflexEnvironment.version],
410412
autoUnref: false,
411413
});
412414
// Ensure undefined fields in events are sent as null instead of removed

reflex/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,11 @@ def on_connect(self, sid, environ):
15281528
sid: The Socket.IO session id.
15291529
environ: The request information, including HTTP headers.
15301530
"""
1531-
pass
1531+
subprotocol = environ.get("HTTP_SEC_WEBSOCKET_PROTOCOL", None)
1532+
if subprotocol and subprotocol != constants.Reflex.VERSION:
1533+
console.warn(
1534+
f"Frontend version {subprotocol} for session {sid} does not match the backend version {constants.Reflex.VERSION}."
1535+
)
15321536

15331537
def on_disconnect(self, sid):
15341538
"""Event for when the websocket disconnects.

reflex/constants/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""The constants package."""
22

33
from .base import (
4+
APP_HARNESS_FLAG,
45
COOKIES,
56
IS_LINUX,
67
IS_MACOS,

reflex/constants/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class Ping(SimpleNamespace):
257257
# Testing variables.
258258
# Testing os env set by pytest when running a test case.
259259
PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
260+
APP_HARNESS_FLAG = "APP_HARNESS_FLAG"
260261

261262
REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
262263
REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"

reflex/testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def _initialize_app(self):
282282
before_decorated_pages = reflex.app.DECORATED_PAGES[self.app_name].copy()
283283
# Ensure the AppHarness test does not skip State assignment due to running via pytest
284284
os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None)
285+
os.environ[reflex.constants.APP_HARNESS_FLAG] = "true"
285286
self.app_module = reflex.utils.prerequisites.get_compiled_app(
286287
# Do not reload the module for pre-existing apps (only apps generated from source)
287288
reload=self.app_source is not None

reflex/utils/build.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
from reflex import constants
1414
from reflex.config import get_config
1515
from reflex.utils import console, path_ops, prerequisites, processes
16+
from reflex.utils.exec import is_in_app_harness
1617

1718

1819
def set_env_json():
1920
"""Write the upload url to a REFLEX_JSON."""
2021
path_ops.update_json_file(
2122
str(prerequisites.get_web_dir() / constants.Dirs.ENV_JSON),
22-
{endpoint.name: endpoint.get_url() for endpoint in constants.Endpoint},
23+
{
24+
**{endpoint.name: endpoint.get_url() for endpoint in constants.Endpoint},
25+
"TEST_MODE": is_in_app_harness(),
26+
},
2327
)
2428

2529

reflex/utils/exec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,15 @@ def is_testing_env() -> bool:
509509
return constants.PYTEST_CURRENT_TEST in os.environ
510510

511511

512+
def is_in_app_harness() -> bool:
513+
"""Whether the app is running in the app harness.
514+
515+
Returns:
516+
True if the app is running in the app harness.
517+
"""
518+
return constants.APP_HARNESS_FLAG in os.environ
519+
520+
512521
def is_prod_mode() -> bool:
513522
"""Check if the app is running in production mode.
514523

0 commit comments

Comments
 (0)