Skip to content
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: 1 addition & 1 deletion packages/reflex-base/hatch_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
if frontend.is_dir():
prefix = "src/" if self.target_name == "sdist" else ""
build_data["force_include"][str(frontend)] = prefix + WEB_TEMPLATE
elif not (root / "src" / WEB_TEMPLATE).is_dir():
elif not (root / "src" / WEB_TEMPLATE / "utils" / "state.js").is_file():
msg = (
f"No frontend to embed: neither {frontend} nor an sdist's copy exists."
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prefer `packages/reflex-frontend/src` over a leftover or stale `.templates/web` directory in source checkouts, and report a dedicated `storage_blocked` connection error when browser site storage is blocked.
13 changes: 8 additions & 5 deletions packages/reflex-base/src/reflex_base/constants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,23 @@ class ReflexHostingCLI(SimpleNamespace):
def _web_template_dir(root_dir: Path) -> Path:
"""Locate the frontend package that is copied into a project's web directory.

A built distribution embeds it under ``.templates/web``. A source checkout
has nothing there and reads ``packages/reflex-frontend/src`` in place, so
an edit to the frontend is never shadowed by a stale copy.
A source checkout reads ``packages/reflex-frontend/src`` in place, so an
edit to the frontend is never shadowed by a leftover ``.templates/web``
directory. A built distribution embeds it under ``.templates/web``.

Args:
root_dir: The root directory of the reflex_base package.

Returns:
The directory holding the frontend files.
"""
checkout = root_dir.parents[2] / "reflex-frontend" / "src"
if (checkout / "utils" / "state.js").is_file():
return checkout
embedded = root_dir / ".templates" / "web"
if embedded.is_dir():
if (embedded / "utils" / "state.js").is_file():
return embedded
return root_dir.parents[2] / "reflex-frontend" / "src"
return checkout


class Templates(SimpleNamespace):
Expand Down
1 change: 1 addition & 0 deletions packages/reflex-base/src/reflex_base/constants/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ClientErrorType(SimpleNamespace):
DISPATCH_MISSING = "dispatch_function_missing"
STATE_UPDATE = "state_update_processing_error"
HYDRATION_PAYLOAD_TOO_LARGE = "hydration_payload_too_large"
STORAGE_BLOCKED = "storage_blocked"


class EventTriggers(SimpleNamespace):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show the browser storage error message immediately in `ConnectionToaster`, `ConnectionBanner`, and `ConnectionModal` when site storage is blocked instead of reporting that the server is unreachable.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from reflex_base import constants
from reflex_base.components.component import Component
from reflex_base.constants import Dirs, Hooks, Imports
from reflex_base.constants.compiler import CompileVars
from reflex_base.constants.compiler import (
CompileVars,
MemoizationDisposition,
MemoizationMode,
)
from reflex_base.environment import environment
from reflex_base.utils.imports import ImportVar
from reflex_base.vars import VarData
Expand Down Expand Up @@ -54,6 +58,14 @@
_var_data=connect_error_var_data,
Comment thread
MilindChatrabhuji marked this conversation as resolved.
Resolved
).to(BooleanVar)

has_storage_blocked_error = Var(
_js_expr=(
"((connectErrors.length > 0) && "
f"(connectErrors[connectErrors.length - 1].code === '{constants.ClientErrorType.STORAGE_BLOCKED}'))"
),
_var_data=connect_error_var_data,
).to(BooleanVar)


class WebsocketTargetURL(Var):
"""A component that renders the websocket target URL."""
Expand Down Expand Up @@ -84,10 +96,16 @@ def default_connection_error() -> list[str | Var | Component]:
The default connection error message.
"""
return [
"Cannot connect to server: ",
connection_error,
". Check if server is reachable at ",
WebsocketTargetURL.create(),
cond(
has_storage_blocked_error | has_hydration_payload_error,
connection_error,
Fragment.create(
"Cannot connect to server: ",
connection_error,
". Check if server is reachable at ",
WebsocketTargetURL.create(),
),
),
]


Expand All @@ -103,6 +121,7 @@ def add_hooks(self) -> list[str | Var]:
toast_id = "websocket-error"
# Its own id, so a pending dismiss of the connection toast cannot remove it.
hydration_toast_id = "hydration-payload-error"
storage_blocked_toast_id = "storage-blocked-error"
target_url = WebsocketTargetURL.create()
props = ToastProps(
description=LiteralVar.create(
Expand Down Expand Up @@ -168,11 +187,16 @@ def add_hooks(self) -> list[str | Var]:
() => {{
if ({has_hydration_payload_error!s}) {{
toast?.error({connection_error!s}, {{id: "{hydration_toast_id}", closeButton: true, duration: Infinity}});
}} else if ({has_storage_blocked_error!s}) {{
toast?.dismiss("{toast_id}");
toast?.error({connection_error!s}, {{id: "{storage_blocked_toast_id}", closeButton: true, duration: Infinity}});
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}} else if ({has_too_many_connection_errors!s}) {{
toast?.dismiss("{storage_blocked_toast_id}");
if (!userDismissed) {{
{toast_var!s}
}}
}} else {{
toast?.dismiss("{storage_blocked_toast_id}");
toast?.dismiss("{toast_id}");
setUserDismissed(false); // after reconnection reset dismissed state
}}
Expand Down Expand Up @@ -248,15 +272,59 @@ def create(cls, comp: Component | None = None) -> Component:
"""
if not comp:
comp = el.span(*default_connection_error())
return cond(
has_too_many_connection_errors,
el.dialog(
el.h2("Connection Error"),
comp,
open=has_too_many_connection_errors,
z_index=9999,
has_modal_error = (
has_too_many_connection_errors
| has_storage_blocked_error
| has_hydration_payload_error
)
modal_dismissed_var_data = VarData.merge(
has_modal_error._get_all_var_data(),
VarData(
imports={"react": ("useEffect", "useState")},
hooks={
"const [modalDismissed, setModalDismissed] = useState(false);": None,
f"useEffect(() => {{ if (!({has_modal_error!s})) {{ setModalDismissed(false); }} }}, [connectErrors]);": None,
},
),
)
modal_dismissed = Var(
"modalDismissed",
_var_type=bool,
_var_data=modal_dismissed_var_data,
).to(BooleanVar)
show_modal = has_modal_error & ~modal_dismissed
close_button = el.button(
"Close",
type="button",
custom_attrs={
"on_click": Var(
_js_expr="() => setModalDismissed(true)",
_var_data=modal_dismissed_var_data,
),
},
)
close_row = el.div(close_button, margin_top="0.75rem")
dialog = el.dialog(
el.h2("Connection Error"),
comp,
close_row,
open=show_modal,
z_index=9999,
)
modal = cond(show_modal, dialog)
# Keep the modal chrome in one component scope without suppressing auto-memoization
# of caller-supplied `comp`, so custom `on_mount`/`on_unmount` stay scoped to `comp`.
modal_cond = modal.children[0]
true_branch = (
modal_cond.children[0]
if isinstance(modal_cond, Component) and modal_cond.children
else None
)
unmemoized = MemoizationMode(disposition=MemoizationDisposition.NEVER)
for chrome in (modal, modal_cond, true_branch, dialog, close_row, close_button):
if isinstance(chrome, Component):
chrome._memoization_mode = unmemoized
return modal


class WifiOffPulse(Icon):
Expand Down
Loading