mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-23 06:11:06 +03:00
* 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>
73 lines
4.0 KiB
Nginx Configuration File
73 lines
4.0 KiB
Nginx Configuration File
server {
|
|
listen ${PORT};
|
|
listen [::]:${PORT};
|
|
server_name _;
|
|
root /usr/share/nginx/html;
|
|
index index.html;
|
|
|
|
#add_header X-Frame-Options SAMEORIGIN;
|
|
#add_header X-Content-Type-Options nosniff;
|
|
#add_header Referrer-Policy strict-origin-when-cross-origin;
|
|
#add_header X-Permitted-Cross-Domain-Policies none;
|
|
#add_header Cross-Origin-Resource-Policy same-site;
|
|
add_header Cross-Origin-Opener-Policy same-origin;
|
|
add_header Cross-Origin-Embedder-Policy require-corp;
|
|
#add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http: https: blob:; style-src 'self' 'unsafe-inline' https: http:; img-src 'self' data: blob: https: http:; connect-src data: blob: https: http:; font-src 'self' https: http:; object-src 'none'; base-uri 'self'; form-action 'self';";
|
|
|
|
include /etc/nginx/mime.types;
|
|
types {
|
|
# mime.types already maps js, remapping it here only emits a
|
|
# "duplicate extension" warning on every startup.
|
|
application/javascript mjs;
|
|
}
|
|
|
|
# `/it-tools` is the same place as `/it-tools/`, but only the second one gives the
|
|
# browser the right directory to resolve the app's relative URLs against, so send the
|
|
# first one there. Renders to `^$` -- which no request URI can match -- when the app is
|
|
# served from the root. Relative Location headers, because $scheme is whatever the
|
|
# reverse proxy used to reach the container, not what the browser used.
|
|
absolute_redirect off;
|
|
rewrite ^${BASE_URL_NO_SLASH_REGEX}$ ${BASE_URL} permanent;
|
|
|
|
# Serving the app from a subpath (BASE_URL, normalised by
|
|
# docker-entrypoint.d/18-resolve-base-url.envsh, `/` by default).
|
|
#
|
|
# The build is path-agnostic: every asset is referenced relatively, and the one
|
|
# absolute anchor is a `<base href="/">` in index.html that the app itself reads its
|
|
# base from (src/utils/base-url.ts). Rewriting that single tag on the way out is
|
|
# therefore enough to move the whole app to another path -- no rebuild, no
|
|
# subpath-specific image.
|
|
#
|
|
# sub_filter drops Last-Modified/ETag from every response it touches, and text/html is
|
|
# always in scope (sub_filter_types can add types, not exclude HTML). So: index.html,
|
|
# which an SPA must not cache anyway and which the service worker precaches regardless,
|
|
# and the one other HTML document in the build -- the visual-subnet-calculator's iframe,
|
|
# public/visualsubnetcalc/calc.html -- which loses conditional revalidation and is
|
|
# refetched in full when that tool is opened.
|
|
sub_filter '<base href="/">' '<base href="${BASE_URL}">';
|
|
sub_filter_once on;
|
|
|
|
# Hashed build output, with or without the deployment prefix in front of it: a reverse
|
|
# proxy may pass `/it-tools/assets/...` through as-is or strip the prefix first, and
|
|
# both have to land on the same file. Without this, a missing chunk falls through to
|
|
# index.html below and is served as text/html with a 200, which the browser rejects
|
|
# with an opaque MIME error instead of a plain 404.
|
|
location ~ ^/(?:.*/)?assets/(?<asset>.+)$ {
|
|
try_files /assets/$asset =404;
|
|
}
|
|
|
|
location / {
|
|
# Same story for everything else the app fetches by path (the JSON config files,
|
|
# the WASM helpers in public/): drop the deployment prefix when the proxy forwarded
|
|
# it instead of stripping it. Renders to `^/(.*)$` -- a no-op -- for the default
|
|
# BASE_URL of `/`.
|
|
rewrite ^${BASE_URL_REGEX}(.*)$ /$1 break;
|
|
# No `$uri/`: nothing in the build is a directory with an index in it, so the only
|
|
# thing matching one ever produced was nginx's own "add the trailing slash"
|
|
# redirect -- computed from the URI *after* the rewrite above, so under a subpath
|
|
# `/it-tools/figlet-fonts` bounced the browser out of the app to `/figlet-fonts/`
|
|
# (and then to a 403). Unknown paths belong to the SPA, which has a 404 page.
|
|
try_files $uri /index.html;
|
|
}
|
|
}
|