From c15839da402c9e47fb7a147d747d63925d32c076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Fri, 14 Jul 2023 00:33:01 +0200 Subject: [PATCH] parameter for turning off nextJS compression (#1316) --- reflex/.templates/web/next.config.js | 1 + reflex/config.py | 3 +++ reflex/constants.py | 2 ++ reflex/utils/prerequisites.py | 17 +++++++++++++++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/reflex/.templates/web/next.config.js b/reflex/.templates/web/next.config.js index da1bb770f..1954c92f0 100644 --- a/reflex/.templates/web/next.config.js +++ b/reflex/.templates/web/next.config.js @@ -1,3 +1,4 @@ module.exports = { reactStrictMode: true, + compress: true, }; diff --git a/reflex/config.py b/reflex/config.py index 5765acc08..4e2345b23 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -203,6 +203,9 @@ class Config(Base): # Timeout when launching the gunicorn server. timeout: int = constants.TIMEOUT + # Whether to enable or disable nextJS gzip compression. + next_compression: bool = True + def __init__(self, *args, **kwargs): """Initialize the config values. diff --git a/reflex/constants.py b/reflex/constants.py index 2ba4adeaa..3a39ff3f0 100644 --- a/reflex/constants.py +++ b/reflex/constants.py @@ -85,6 +85,8 @@ WEB_ASSETS_DIR = os.path.join(WEB_DIR, "public") TAILWIND_CONFIG = os.path.join(WEB_DIR, "tailwind.config.js") # Default Tailwind content paths TAILWIND_CONTENT = ["./pages/**/*.{js,ts,jsx,tsx}"] +# The NextJS config file +NEXT_CONFIG_FILE = "next.config.js" # The sitemap config file. SITEMAP_CONFIG_FILE = os.path.join(WEB_DIR, "next-sitemap.config.js") # The node modules directory. diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 394df208b..7d1919517 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -207,11 +207,24 @@ def initialize_app_directory(app_name: str, template: constants.Template): def initialize_web_directory(): """Initialize the web directory on reflex init.""" console.log("Initializing the web directory.") - path_ops.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.NODE_MODULES)) - path_ops.rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.PACKAGE_LOCK)) path_ops.cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR) path_ops.mkdir(constants.WEB_ASSETS_DIR) + # update nextJS config based on rxConfig + next_config_file = os.path.join(constants.WEB_DIR, constants.NEXT_CONFIG_FILE) + + with open(next_config_file, "r") as file: + lines = file.readlines() + for i, line in enumerate(lines): + if "compress:" in line: + new_line = line.replace( + "true", "true" if get_config().next_compression else "false" + ) + lines[i] = new_line + + with open(next_config_file, "w") as file: + file.writelines(lines) + # Write the current version of distributed reflex package to a REFLEX_JSON.""" with open(constants.REFLEX_JSON, "w") as f: reflex_json = {"version": constants.VERSION}