Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check frontend version on connect #4611

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions reflex/.templates/web/utils/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from "axios";
import io from "socket.io-client";
import JSON5 from "json5";
import env from "$/env.json";
import reflexEnvironment from "$/reflex.json";
import Cookies from "universal-cookie";
import { useEffect, useRef, useState } from "react";
import Router, { useRouter } from "next/router";
Expand Down Expand Up @@ -407,6 +408,7 @@ export const connect = async (
socket.current = io(endpoint.href, {
path: endpoint["pathname"],
transports: transports,
protocols: env.TEST_MODE ? undefined : [reflexEnvironment.version],
autoUnref: false,
});
// Ensure undefined fields in events are sent as null instead of removed
Expand Down
6 changes: 5 additions & 1 deletion reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,11 @@ def on_connect(self, sid, environ):
sid: The Socket.IO session id.
environ: The request information, including HTTP headers.
"""
pass
subprotocol = environ.get("HTTP_SEC_WEBSOCKET_PROTOCOL", None)
if subprotocol and subprotocol != constants.Reflex.VERSION:
console.warn(
f"Frontend version {subprotocol} for session {sid} does not match the backend version {constants.Reflex.VERSION}."
)

def on_disconnect(self, sid):
"""Event for when the websocket disconnects.
Expand Down
1 change: 1 addition & 0 deletions reflex/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The constants package."""

from .base import (
APP_HARNESS_FLAG,
COOKIES,
IS_LINUX,
IS_MACOS,
Expand Down
1 change: 1 addition & 0 deletions reflex/constants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class Ping(SimpleNamespace):
# Testing variables.
# Testing os env set by pytest when running a test case.
PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
APP_HARNESS_FLAG = "APP_HARNESS_FLAG"

REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"
1 change: 1 addition & 0 deletions reflex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def _initialize_app(self):
before_decorated_pages = reflex.app.DECORATED_PAGES[self.app_name].copy()
# Ensure the AppHarness test does not skip State assignment due to running via pytest
os.environ.pop(reflex.constants.PYTEST_CURRENT_TEST, None)
os.environ[reflex.constants.APP_HARNESS_FLAG] = "true"
self.app_module = reflex.utils.prerequisites.get_compiled_app(
# Do not reload the module for pre-existing apps (only apps generated from source)
reload=self.app_source is not None
Expand Down
6 changes: 5 additions & 1 deletion reflex/utils/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
from reflex import constants
from reflex.config import get_config
from reflex.utils import console, path_ops, prerequisites, processes
from reflex.utils.exec import is_in_app_harness


def set_env_json():
"""Write the upload url to a REFLEX_JSON."""
path_ops.update_json_file(
str(prerequisites.get_web_dir() / constants.Dirs.ENV_JSON),
{endpoint.name: endpoint.get_url() for endpoint in constants.Endpoint},
{
**{endpoint.name: endpoint.get_url() for endpoint in constants.Endpoint},
"TEST_MODE": is_in_app_harness(),
},
)


Expand Down
9 changes: 9 additions & 0 deletions reflex/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ def is_testing_env() -> bool:
return constants.PYTEST_CURRENT_TEST in os.environ


def is_in_app_harness() -> bool:
"""Whether the app is running in the app harness.

Returns:
True if the app is running in the app harness.
"""
return constants.APP_HARNESS_FLAG in os.environ


def is_prod_mode() -> bool:
"""Check if the app is running in production mode.

Expand Down
Loading