From 845da983cad89cdb99988ce5e7d7617310e543d7 Mon Sep 17 00:00:00 2001 From: Pascal Bourque Date: Fri, 15 Nov 2024 12:07:10 -0500 Subject: [PATCH] fix: Site does not load when hosted in a subdirectory of the web host root (#43) Fixes #32 --- README.md | 18 ++++++++++++++++++ next.config.ts | 2 +- src/libs/DataSource.ts | 2 +- weewx/bin/user/meteo.py | 34 ++++++++++++++++++++++++++++++++++ weewx/install.py | 7 +++++-- weewx/skins/me.teo/skin.conf | 7 ++++++- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 weewx/bin/user/meteo.py diff --git a/README.md b/README.md index 764c0f6..f799fdc 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,24 @@ There's not much else needed to get started. You can have a look at the `skin.co liking. The most likely customization you'll want to make is to edit the `observations_current` and `observations_summary` variables to list which observations you want on your Me.teo dashboard and in what order. +### Configuring `HTML_ROOT` and `HTML_SUBDIR` + +If you are installing this skin in a subdirectory of your web server, you need to set `HTML_SUBDIR` to the name of the subdirectory. +For example, if your web server is serving files from `/var/www/html`, and you have set `HTML_ROOT` for this skin to `/var/www/html/meteo`, +you must set `HTML_SUBDIR` to `meteo`. + +```ini +[StdReport] + [[Me.teo]] + skin = me.teo + enable = true + + HTML_ROOT = public_html/foo/bar + + # If web server is configured to serve files from public_html: + HTML_SUBDIR = foo/bar +``` + ### Google Analytics If you have a Google Analytics account and want to track visits to your site, you can add your tracking ID to the skin diff --git a/next.config.ts b/next.config.ts index 147fe49..b46d3c1 100644 --- a/next.config.ts +++ b/next.config.ts @@ -23,7 +23,7 @@ const withNextIntl = createNextIntlPlugin(); const nextConfig: NextConfig = { output: 'export', - basePath: `/${process.env.METEO_BUILD_LOCALE || 'en'}`, + basePath: `/##METEO_BASE_PATH##/${process.env.METEO_BUILD_LOCALE || 'en'}`, distDir: `out/${process.env.METEO_BUILD_LOCALE || 'en'}` }; diff --git a/src/libs/DataSource.ts b/src/libs/DataSource.ts index 7cd2097..d73f70c 100644 --- a/src/libs/DataSource.ts +++ b/src/libs/DataSource.ts @@ -35,7 +35,7 @@ const fetcher = async (url: string | URL | Request) => { return res.json(); }; -const baseUrl = process.env.NODE_ENV === 'production' ? '/data' : '/sample_data'; +const baseUrl = process.env.NODE_ENV === 'production' ? '/##METEO_BASE_PATH##/data' : '/sample_data'; export function useGlobalData() { return useSWR(`${baseUrl}/global.json`, fetcher); diff --git a/weewx/bin/user/meteo.py b/weewx/bin/user/meteo.py new file mode 100644 index 0000000..a38674e --- /dev/null +++ b/weewx/bin/user/meteo.py @@ -0,0 +1,34 @@ +from weewx.reportengine import ReportGenerator +import os.path +import logging + +log = logging.getLogger(__name__) + +class NextJsBasePathGenerator(ReportGenerator): + def run(self): + html_root = self.skin_dict.get('HTML_ROOT', self.config_dict['StdReport']['HTML_ROOT']) + html_dest_dir = os.path.join(self.config_dict['WEEWX_ROOT'], html_root) + html_subdir = self.skin_dict.get('HTML_SUBDIR', '') + + log.info("Replacing ##METEO_BASE_PATH## with '%s' in all html, js, css and txt files in '%s'...", html_subdir, html_dest_dir) + + for root, dirs, files in os.walk(html_dest_dir): + for file in files: + if file.endswith('.html') or file.endswith('.js') or file.endswith('.css') or file.endswith('.txt'): + filepath = os.path.join(root, file) + log.debug("Processing '%s'...", filepath) + + try: + with open(filepath, 'r') as f: + content = f.read() + + if html_subdir == '': + content = content.replace('/##METEO_BASE_PATH##', '') + else: + content = content.replace('##METEO_BASE_PATH##', html_subdir) + + with open(filepath, 'w') as f: + f.write(content) + except Exception as e: + log.error("Unable to process '%s'. Got exception '%s': %s", filepath, type(e), e) + pass diff --git a/weewx/install.py b/weewx/install.py index 646ad80..e2347c2 100644 --- a/weewx/install.py +++ b/weewx/install.py @@ -15,7 +15,7 @@ # along with this program. If not, see . import configobj -from setup import ExtensionInstaller +from weecfg.extension import ExtensionInstaller from io import StringIO import os @@ -41,6 +41,9 @@ def get_files(): class MeteoInstaller(ExtensionInstaller): def __init__(self): + files = get_files() + files.append(('bin/user', ['bin/user/meteo.py'])) + super(MeteoInstaller, self).__init__( version=VERSION, name=NAME, @@ -48,7 +51,7 @@ def __init__(self): author=AUTHOR, author_email=AUTHOR_EMAIL, config=config_dict, - files=get_files() + files=files ) config_string = """ diff --git a/weewx/skins/me.teo/skin.conf b/weewx/skins/me.teo/skin.conf index 26cc8ca..99fd780 100644 --- a/weewx/skins/me.teo/skin.conf +++ b/weewx/skins/me.teo/skin.conf @@ -17,6 +17,11 @@ SKIN_NAME = Me.teo SKIN_VERSION = ##SKIN_VERSION## +# If you are installing this skin in a subdirectory of your web server, you need to set HTML_SUBDIR to the name of the subdirectory. +# For example, if your web server is serving files from "/var/www/html", and you have set HTML_ROOT for this skin to "/var/www/html/meteo", +# set HTML_SUBDIR to "meteo". +# HTML_SUBDIR = meteo + [Extras] # If you have a Google Analytics GA4 tag, uncomment and edit the next line, and # the analytics code will be included in the generated pages. @@ -69,4 +74,4 @@ SKIN_VERSION = ##SKIN_VERSION## copy_once = en, fr [Generators] - generator_list = weewx.cheetahgenerator.CheetahGenerator, weewx.reportengine.CopyGenerator + generator_list = weewx.cheetahgenerator.CheetahGenerator, weewx.reportengine.CopyGenerator, user.meteo.NextJsBasePathGenerator