mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-25 23:24:09 +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>
38 lines
1.6 KiB
TypeScript
38 lines
1.6 KiB
TypeScript
export { appBaseUrl, resolveAppBaseUrl };
|
|
|
|
/**
|
|
* The path the app is served under, resolved at runtime instead of baked into the bundle.
|
|
*
|
|
* The build is path-agnostic: vite.config.ts uses a relative `base`, so hashed assets and
|
|
* lazy chunks resolve against the URL of the chunk that imports them and `import.meta.env.BASE_URL` is a useless `./`.
|
|
* The one thing that does know where the app lives is the `<base href>` element injected into index.html at build time --
|
|
* the container rewrites it from the BASE_URL environment variable on the way out (see nginx.conf), which is what lets
|
|
* a single image serve any subpath.
|
|
*
|
|
* Anything that builds a URL to a file shipped in `public/`, plus the router base, goes
|
|
* through this. Deployments that serve the app from the domain root -- the default -- get
|
|
* `/`, exactly as before.
|
|
*/
|
|
function resolveAppBaseUrl(href: string | null | undefined, documentUrl: string): string {
|
|
if (!href) {
|
|
// No <base>: a plain `vite dev` (the dev server always serves from the root, even
|
|
// when the build base is relative) or a unit test rendering into a bare document.
|
|
return '/';
|
|
}
|
|
|
|
try {
|
|
// Resolved against the *page* URL, which is how the browser itself resolves a
|
|
// relative `<base href>`. `document.baseURI` would already have the base applied.
|
|
const { pathname } = new URL(href, documentUrl);
|
|
|
|
return pathname.endsWith('/') ? pathname : `${pathname}/`;
|
|
} catch (_) {
|
|
return '/';
|
|
}
|
|
}
|
|
|
|
const appBaseUrl =
|
|
typeof document === 'undefined'
|
|
? '/'
|
|
: resolveAppBaseUrl(document.querySelector('base')?.getAttribute('href'), document.URL);
|