The PDF Travels With Its Page, the Link Does Not Wander
A ../../../ link out of a generated docs folder is valid while the tree is whole, and dead the moment the folder is handed over. The PDF now moves with it.
TL;DR
A relative link that navigates up the directory tree breaks the moment the docs folder gets handed off as a standalone package. The fix copies PDFs next to their referencing pages so each folder becomes a self-contained unit with working internal links. Failing loudly on missing files prevents shipping broken artifacts, and a simple folder-relocation test catches any remaining dependency issues.
The documentation generator finished regenerating, and one docs page carried a link reading ../../../laporan/2025/q1/official-report.pdf. The docs/ folder was handed over to another team as a standalone package. The moment the folder stood on its own, the PDF at the end of that link vanished. Three levels up, straight out of the folder being carried.
The first guess: path.relative computed the wrong depth. Perhaps the root directory moved, or the folder depth differed at build time. The first solution that came to mind was to deepen the path.relative call until the direction matched the new structure.
It was never about path depth. The ../../../ link is perfectly valid; the browser resolves it correctly against the document's base URL [3]. A correct resolution points at the right file for as long as the directory tree stays complete. Once docs/ split off from the reports tree, the resolution stayed correct and the target still did not come along. What was wrong was the shape of the link itself.
Copy, do not point
The decision: at regeneration, the PDF is copied next to the page that needs it, and the page emits a ./<id>.pdf link. Nothing new for this project. Media has worked this way for a long time: a group's video lives as features/<group>/<group>.mp4, one folder holding the page and its media together. The PDF simply follows a convention that already proved itself.
const pdfSrc = path.join(REPORT_DIR, dir, `${id}.pdf`);
const pdfDest = path.join(docsDir, id, `${id}.pdf`);
if (!fs.existsSync(pdfSrc)) {
throw new Error(`PDF sumber tidak ditemukan: ${pdfSrc}`);
}
fs.copyFileSync(pdfSrc, pdfDest);
emitLink(`./${id}.pdf`);
fs.copyFileSync overwrites the destination file by default when it already exists [1], so every regeneration carries the newest version without any extra flags. The docs/<id>/ folder stops being a mere container for pages; it becomes a complete unit: the page, the official PDF, and the link connecting them.
Fail loudly when the input is missing
Copying introduces one new risk: the source file might not exist. That is why the generator checks for the file before copying. fs.existsSync returns true when the path exists and false when it does not [1]. The moment the source cannot be found, throw new Error halts the whole regeneration.
Throwing an error feels excessive for one missing PDF. Ignoring it and continuing is far more comfortable. That option exists, and its outcome is predictable: pages get published, links hang in the air, and the problem surfaces only when the client clicks. Fail-loud closes that door. When one required file is missing, there is no reason to publish a package known to be broken.
In Node.js, an uncaught exception prints a stack trace to stderr and exits with code 1 [2]. No partial output hides the problem, no page ships with a dangling link. The Error object is the base class for user-defined exceptions [4], so the message can name exactly which file is missing and where — precisely what the person reading the build log needs.
An artifact that carries its own dependencies
The <a> element binds a page to anything a URL can address [5], but a hyperlink only works when its target comes along. With the PDF at docs/<id>/<id>.pdf, the folder can be moved, zipped, or deployed elsewhere without breaking a single link.
Relative URL resolution does anchor to the document's base URL [3], which is exactly why ../../../ works while the directory tree is whole. The lesson reaches further than PDFs: every cross-tree relative link binds the generated artifact to the shape of the source tree. The moment the two part ways — and in a client handoff they always do — that binding becomes a debt that comes due. A generator's output should carry its own dependencies.
The test is simple and anyone can run it: move the generated folder to another location, open one of its pages, click every link. A single dead one means the artifact is not self-contained yet.