mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 22:02:38 +03:00
dc031dfe2cdf3175dc40cfc5088cf4e329cf8897
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
0a1f5dc450 |
feat(docker): serve the app from any subpath with a runtime BASE_URL (#565)
* feat(docker): serve the app from any subpath with a runtime BASE_URL
BASE_URL was a build argument, so hosting under a subfolder meant building
your own image (or pulling the separate baseurl-it-tools tag, which is only
ever right for /it-tools/). Make it a runtime setting instead, so the regular
image works anywhere:
docker run -e BASE_URL=/it-tools/ ghcr.io/sharevb/it-tools:latest
The build is now path-agnostic. Vite's base is relative, so hashed assets and
lazy chunks resolve against the chunk that imports them, and the single
absolute anchor left in the output is a `<base href="/">` injected into
index.html. nginx rewrites that one tag per request (sub_filter) from the
normalised BASE_URL, and the app reads its base back from it at runtime
(src/utils/base-url.ts) for the router and for every URL it builds into
public/ -- import.meta.env.BASE_URL is meaningless in a relative build.
Also handled:
- both reverse-proxy styles: the prefix may reach the container (stripped by
the rewrite) or be stripped by the proxy first (nothing to do). The assets
location matches with or without it, so a missing chunk is still a 404
rather than the SPA shell.
- `/it-tools` redirects to `/it-tools/`, with a relative Location so the
scheme of the proxy hop does not leak into it.
- the PWA: manifest URLs are relative to the manifest, workbox precache
entries relative to the service worker, and the worker is registered
against the runtime base rather than the build-time one.
- BASE_URL is normalised (`it-tools`, `/it-tools`, `/it-tools/` all work) and
rejected if it is not a plain path, since it lands in an HTML attribute and
in an nginx regex.
Defaults are unchanged: with BASE_URL unset the rendered config and the
served page are what they were, except that index.html loses ETag/Last-Modified
to sub_filter. On a read-only filesystem the template cannot be re-rendered,
so BASE_URL is ignored there and the container now says so, like it already
had to for PORT.
Building with --build-arg BASE_URL still works but only moves the image
default; the baseurl-it-tools tag is now just latest with that default.
Closes sharevb/it-tools#403
* fix(docker): restore the PWA update reload, escape BASE_URL in the nginx regexes
Follow-up from review of the runtime BASE_URL change.
- registerSW() in autoUpdate mode reloaded the page when a newly deployed
worker took over (workbox-window's `activated` handler); hand-rolling the
registration dropped that, so a tab kept running the previous bundle until
it navigated. Reimplemented on `controllerchange`, guarded so a first
install -- which also claims the page -- does not reload. Verified by
counting document loads across an install and a simulated deploy: 1 load on
first visit, exactly 1 reload when a new worker takes over.
- The comment justifying the hand-rolled registration was wrong: register()
does resolve a relative URL against `<base href>`. The real reason is
workbox-window's own urlsMatch(), which resolves against location.href, so
a relative sw URL misidentifies the worker on a route deeper than one
segment. Comment now says that.
- BASE_URL allows `.`, which is a metacharacter in the two nginx regexes it
is spliced into: `/it.tools/` also matched `/itXtools/`. The entrypoint now
exports regex-escaped forms for those, keeping the literal value for the
sub_filter replacement and the redirect target.
- Dropped `$uri/` from try_files. Nothing in the build is a directory with an
index, so the only thing it ever produced was nginx's trailing-slash
redirect -- computed after the prefix rewrite, so `/it-tools/figlet-fonts`
answered `301 /figlet-fonts/` and bounced the browser out of the app.
- The sub_filter comment claimed index.html was the only response losing
ETag/Last-Modified; public/visualsubnetcalc/calc.html is HTML too and is
equally affected. Comment corrected.
- README: in the prefix-stripping proxy setup, `/it-tools` without the
trailing slash never reaches the container, so the proxy has to redirect it
(as the sample does). Said so.
- The new e2e test reloaded a page that had just started pulling in a tool's
chunks; WebKit cancelled the load ~1 run in 3 (the app reloads itself on an
aborted chunk fetch). It opens the URL in a second tab instead: 8/8 on
WebKit, 3/3 browsers green.
* fix(build): validate BASE_URL before it reaches the <base href>
From Copilot's review of #10.
The container validates its runtime BASE_URL, but the build-time one went
straight into an HTML attribute unchecked: a value carrying a quote rewrote
the tag around it, and one carrying a space or `..` quietly produced a page
that loads from the wrong place. Apply the same rule the entrypoint does --
a plain path of unreserved characters -- and fail the build otherwise.
Also: "can not" -> "cannot" in the read-only filesystem warning.
* Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
||
|
|
8b7bc0a650 |
fix(docker): run on read-only filesystems, IPv6-less hosts and cpu-limited containers (#556)
Five fixes to the Docker image, found while running it-tools on OpenShift, where containers run as an arbitrary UID. No application code is changed. Start on a read-only filesystem. `docker run --read-only` and Kubernetes readOnlyRootFilesystem crash-loop the container, because 20-envsubst-on-templates.sh checks its output directory with `[ ! -w $dir ]`. That only reads permission bits, so a read-only mount passes the check, the write fails with EROFS and `set -e` stops the entrypoint. 19-skip-envsubst-if-readonly.envsh now tests the directory with a real write and, on failure, points NGINX_ENVSUBST_TEMPLATE_DIR at a missing path so the envsubst script exits early. That is only safe if the config in the image is already correct, and today it is nginx's stock welcome config, so the Dockerfile renders the template at build time as well. Writable setups are unchanged: the write test passes, nothing is exported and the config is rendered at startup as before, so a custom $PORT still works. Skip IPv6 when the host has none. nginx.conf always listens on IPv6, so where IPv6 is off nginx fails with `socket() [::]:8080 failed (97: Address family not supported by protocol)`. 25-remove-ipv6-listen-if-unavailable.sh comments those lines out, using the same /proc/net/if_inet6 check as nginx's own 10-listen-on-ipv6-by-default.sh, and exits at once when IPv6 works. It probes the directory before editing, because `sed -i` rewrites through a temporary file there, so a writable file in a read-only directory is not enough, and it tolerates a failed edit so it can never stop the entrypoint. Match workers to the cpu limit. `worker_processes auto` counts the host's cores, not the container's cpu limit, so the image starts 256 workers on a 256-core host in any container size. `docker run -m 192m` is enough to be OOM-killed at startup. Setting NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE enables the script nginx already ships. Containers without a cpu limit keep one worker per core. This is the only change that affects a deployment that works today. Return 404 for missing assets. All paths fall back to index.html, so a missing build file returns 200 and text/html, and the browser reports a confusing MIME error instead of a 404. /assets/ now skips the SPA fallback. Sub-folder requests such as /it-tools/assets/... still match `location /`, so those deployments do not change. Stop remapping js. nginx.conf includes mime.types, which already maps js, then maps it again, so every startup logs a duplicate extension warning. mime.types has no entry for mjs, so mjs stays and js goes. Signed-off-by: Sebastian Danielsson <sebastian.danielsson@proton.me> |