소스 검색

inline memoize components in compiled page files, to prevent hot reload errors (#2527)

jackie-pc 1 년 전
부모
커밋
0f3857ec9a
1개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 12 4
      reflex/compiler/compiler.py

+ 12 - 4
reflex/compiler/compiler.py

@@ -63,6 +63,10 @@ def _compile_theme(theme: dict) -> str:
     return templates.THEME.render(theme=theme)
 
 
+def _is_dev_mode() -> bool:
+    return os.environ.get("REFLEX_ENV_MODE", "dev") == "dev"
+
+
 def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
     """Compile the initial state and contexts.
 
@@ -72,16 +76,15 @@ def _compile_contexts(state: Optional[Type[BaseState]]) -> str:
     Returns:
         The compiled context file.
     """
-    is_dev_mode = os.environ.get("REFLEX_ENV_MODE", "dev") == "dev"
     return (
         templates.CONTEXT.render(
             initial_state=utils.compile_state(state),
             state_name=state.get_name(),
             client_storage=utils.compile_client_storage(state),
-            is_dev_mode=is_dev_mode,
+            is_dev_mode=_is_dev_mode(),
         )
         if state
-        else templates.CONTEXT.render(is_dev_mode=is_dev_mode)
+        else templates.CONTEXT.render(is_dev_mode=_is_dev_mode())
     )
 
 
@@ -238,7 +241,12 @@ def _compile_stateful_components(
 
         # When the component is referenced by more than one page, render it
         # to be included in the STATEFUL_COMPONENTS module.
-        if isinstance(component, StatefulComponent) and component.references > 1:
+        # Skip this step in dev mode, thereby avoiding potential hot reload errors for larger apps
+        if (
+            isinstance(component, StatefulComponent)
+            and component.references > 1
+            and not _is_dev_mode()
+        ):
             # Reset this flag to render the actual component.
             component.rendered_as_shared = False