Browse Source

[REF-3076] Do not purge .web/pages in dev mode (#3592)

* [REF-3067] Do not purge .web/pages in dev mode

Avoid race condition in hot reload that occurs when pages are removed before
they are recreated.

Fix #3416

* use constants instead of hardcoded string
Masen Furer 10 months ago
parent
commit
9e1789a6c2
1 changed files with 14 additions and 3 deletions
  1. 14 3
      reflex/app.py

+ 14 - 3
reflex/app.py

@@ -15,6 +15,7 @@ import platform
 import sys
 import traceback
 from datetime import datetime
+from pathlib import Path
 from typing import (
     Any,
     AsyncIterator,
@@ -1018,9 +1019,6 @@ class App(MiddlewareMixin, LifespanMixin, Base):
 
         progress.advance(task)
 
-        # Empty the .web pages directory.
-        compiler.purge_web_pages_dir()
-
         progress.advance(task)
         progress.stop()
 
@@ -1038,6 +1036,19 @@ class App(MiddlewareMixin, LifespanMixin, Base):
             transpile_packages=transpile_packages,
         )
 
+        if is_prod_mode():
+            # Empty the .web pages directory.
+            compiler.purge_web_pages_dir()
+        else:
+            # In dev mode, delete removed pages and update existing pages.
+            keep_files = [Path(output_path) for output_path, _ in compile_results]
+            for p in Path(prerequisites.get_web_dir() / constants.Dirs.PAGES).rglob(
+                "*"
+            ):
+                if p.is_file() and p not in keep_files:
+                    # Remove pages that are no longer in the app.
+                    p.unlink()
+
         for output_path, code in compile_results:
             compiler_utils.write_page(output_path, code)