24 Pages, Zero Media: Paths Measured from the Wrong Directory
One generator change left 24 documentation pages empty: every media file 404'd because src paths were measured from the repo root, not the page's folder.
TL;DR
A static site generator produced 24 pages where all media 404'd, blamed first on disk-opening but actually caused by src paths written from the repo root instead of each page's folder. Relative URLs resolve against the document's own directory, not the generator's working directory. The fix computes each src via path.relative from pageDir, handling Windows separators.
One command, 24 documentation pages regenerated. I opened the first page.html straight from the file manager, just a click, and the page was empty. Every video stopped at a gray player with no image, every photo turned into a broken image icon. DevTools logged 404 for every media file.
The media was there. Video files sat in the same group folder as the pages, and so did the images. I had just changed the generator script in the client portal repo I call KotaPortal, and this was the result: 24 complete pages, zero media loaded.
Blaming file:// first
My first guess: this was a protocol problem. Opening HTML from disk, I told myself, must work under different rules than serving it through a web server; either the server rewrites paths, or the browser restricts file:// for media. The guess felt comfortable because it meant nothing was wrong with my code.
That guess was wrong. If an element's src points to the wrong directory, the file will not be found in any serving mode. And when the same page was served through a server with a different root, the result was identical: 404. What was broken was the content, not the way it was served.
What applies: the page's directory, not the script's cwd
The rule lives in the HTML standard: a relative URL in a document is resolved against the document's own base URL, not against the working directory of the program that wrote the document [3]. In practice, when page.html is opened from the features/<group>/ folder, every relative src inside it is measured from that folder, not from the repo root where the generator ran.
That is where the cwd lie sits. The script thinks in repo-root coordinates, then writes src values as paths from the repo root to a page already sitting inside the group folder. The browser resolves them into layered paths that do not exist. For video, the group folder prefix even appears twice, so the address points even further from the real location. Examples of both are in the code block below.
<!-- salah: ditulis dari koordinat repo root -->
<img src="features/<group>/foto-01.jpg">
<video src="features/<group>/features/<group>/klip.mp4"></video>
<!-- benar: ditulis dari direktori page.html -->
<img src="foto-01.jpg">
<video src="./klip.mp4"></video>
The HTML elements are not at fault. The video element indeed takes the src attribute as the embedded media URL [4], and the image element requires at least one of src or srcset [2]. It is the src values that were mis-measured.
The fix: measure from pageDir, not the repo root
The decision was simple and non-negotiable: the generator must stop speaking in repo-root coordinates. Every src is computed against the directory where page.html is written.
import path from "node:path";
// pageDir = absolute directory where page.html lives
// mediaFile = absolute path to the media file (video/image)
function mediaSrc(pageDir, mediaFile) {
const rel = path.relative(pageDir, mediaFile);
return rel.split(path.sep).join("/");
}
const videoSrc = "./" + group + ".mp4";
const imgSrc = mediaSrc(pageDir, imgFile);
The reason for using path.relative with two absolute arguments: the result is independent of cwd, so the generator stays correct no matter which directory it runs from [1]. For the reverse direction, from src back to the file, path.resolve processes segments from right to left until an absolute path is formed, and only falls back to cwd when no segment is absolute at all [1]. These two functions have a clear contract; what was wrong was the assumption about where measurement starts.
One caveat that must come along: path in Node.js follows the OS. On Windows, results from path.relative use backslashes, and a backslash is never a valid separator in an HTML src. That is why the separator conversion must be explicit, like the last line of the function above. This is not a cosmetic detail. Without it, pages generated on Windows still 404 in any browser.
The alternative I rejected: putting <base> on every page. That element does shift the base URL for all relative URLs in the document, and only one is allowed [3]. But it patches symptoms across 24 HTML files instead of fixing the generator, and <base> does not rescue a src that was mis-measured from the moment it was written.
Verification, and its limits
After the fix, 24 pages were regenerated. The final assert in the script confirms zero broken src values: every address is resolved against its page's directory, then the file's existence at that resolved location is what gets checked. I also reopened several pages straight from disk; video plays, images appear, console clean.
But I want to be honest about its limits. The assert checks file existence at the resolved location, it does not fully simulate the browser's URL resolution. That is the same class of weakness that let the bug slip through in the first place. If your generator writes HTML into nested directories, the stronger check is to compare the URL the document resolves against the file the browser actually requests, not just check file existence. After this incident, I treat a generator script's cwd as the fact most likely to mislead in HTML generation.
Sources:
[1] Node.js: Path documentation (path.relative, path.resolve, path.sep)
[2] MDN: <img>: The Image Embed element