From 9e1789a6c2c297066d4ed77be02f097ac3d7ab0b Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 8 Jul 2024 20:17:38 -0700 Subject: [PATCH] [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 --- reflex/app.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/reflex/app.py b/reflex/app.py index 8f388e37b..9c40db195 100644 --- a/reflex/app.py +++ b/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)