Skip to content

Commit 76457c4

Browse files
authored
add option to disable hmr (#5745)
* add option to disable hmr * make it a fullreload if hmr is enabled * woops * add both options
1 parent 0dfbaf4 commit 76457c4

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

reflex/compiler/templates.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,13 @@ def package_json_template(
502502
)
503503

504504

505-
def vite_config_template(base: str):
505+
def vite_config_template(base: str, hmr: bool, force_full_reload: bool):
506506
"""Template for vite.config.js.
507507
508508
Args:
509509
base: The base path for the Vite config.
510+
hmr: Whether to enable hot module replacement.
511+
force_full_reload: Whether to force a full reload on changes.
510512
511513
Returns:
512514
Rendered vite.config.js content as string.
@@ -537,12 +539,25 @@ def vite_config_template(base: str):
537539
}};
538540
}}
539541
542+
function fullReload() {{
543+
return {{
544+
name: "full-reload",
545+
enforce: "pre",
546+
handleHotUpdate({{ server }}) {{
547+
server.ws.send({{
548+
type: "full-reload",
549+
}});
550+
return [];
551+
}}
552+
}};
553+
}}
554+
540555
export default defineConfig((config) => ({{
541556
plugins: [
542557
alwaysUseReactDomServerNode(),
543558
reactRouter(),
544559
safariCacheBustPlugin(),
545-
],
560+
].concat({"[fullReload()]" if force_full_reload else "[]"}),
546561
build: {{
547562
assetsDir: "{base}assets".slice(1),
548563
rollupOptions: {{
@@ -564,6 +579,7 @@ def vite_config_template(base: str):
564579
}},
565580
server: {{
566581
port: process.env.PORT,
582+
hmr: {"true" if hmr else "false"},
567583
watch: {{
568584
ignored: [
569585
"**/.web/backend/**",

reflex/environment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,12 @@ class EnvironmentVariables:
640640
# Enable full logging of debug messages to reflex user directory.
641641
REFLEX_ENABLE_FULL_LOGGING: EnvVar[bool] = env_var(False)
642642

643+
# Whether to enable hot module replacement
644+
VITE_HMR: EnvVar[bool] = env_var(True)
645+
646+
# Whether to force a full reload on changes.
647+
VITE_FORCE_FULL_RELOAD: EnvVar[bool] = env_var(False)
648+
643649

644650
environment = EnvironmentVariables()
645651

reflex/utils/frontend_skeleton.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from reflex import constants
1111
from reflex.compiler import templates
1212
from reflex.config import Config, get_config
13+
from reflex.environment import environment
1314
from reflex.utils import console, path_ops
1415
from reflex.utils.prerequisites import get_project_hash, get_web_dir
1516
from reflex.utils.registry import get_npm_registry
@@ -192,7 +193,11 @@ def _compile_vite_config(config: Config):
192193
base = "/"
193194
if frontend_path := config.frontend_path.strip("/"):
194195
base += frontend_path + "/"
195-
return templates.vite_config_template(base=base)
196+
return templates.vite_config_template(
197+
base=base,
198+
hmr=environment.VITE_HMR.get(),
199+
force_full_reload=environment.VITE_FORCE_FULL_RELOAD.get(),
200+
)
196201

197202

198203
def initialize_vite_config():

0 commit comments

Comments
 (0)