[Docs] Fix 404 asset base path for previews#1066
Conversation
Signed-off-by: Raunak Madan <madanraunak24@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request adds a script block to layouts/partials/head.html for 404 pages to dynamically set the <base> tag's href attribute when in a PR preview environment. The reviewer suggested updating the JavaScript variable declarations from var to const to align with modern standards and improve block-scoping.
| (function () { | ||
| var path = window.location.pathname; | ||
| var previewPath = path.match(/^((?:\/[^/]+)?\/pr-preview\/pr-[^/]+\/)/); | ||
| var basePath = previewPath ? previewPath[1] : "/"; | ||
| var base = document.createElement("base"); | ||
| base.href = basePath; | ||
| document.head.insertBefore(base, document.head.firstChild); | ||
| })(); |
There was a problem hiding this comment.
Using var for variable declarations is an outdated practice in modern JavaScript. It is highly recommended to use const (or let if re-assignment is needed) to ensure block-scoping, prevent accidental re-declarations, and improve overall code maintainability and readability.
| (function () { | |
| var path = window.location.pathname; | |
| var previewPath = path.match(/^((?:\/[^/]+)?\/pr-preview\/pr-[^/]+\/)/); | |
| var basePath = previewPath ? previewPath[1] : "/"; | |
| var base = document.createElement("base"); | |
| base.href = basePath; | |
| document.head.insertBefore(base, document.head.firstChild); | |
| })(); | |
| (function () { | |
| const path = window.location.pathname; | |
| const previewPath = path.match(/^((?:\/[^/]+)?\/pr-preview\/pr-[^/]+\/)/); | |
| const basePath = previewPath ? previewPath[1] : "/"; | |
| const base = document.createElement("base"); | |
| base.href = basePath; | |
| document.head.insertBefore(base, document.head.firstChild); | |
| })(); |
|
🚀 Preview deployment: https://layer5io.github.io/docs/pr-preview/pr-1066/
|
Description
Fixes the Layer5 docs 404 page asset base path so missing nested routes render with styling in production while preserving relative behavior for GitHub Pages PR previews.
Root cause
The generated 404 page emits relative asset URLs like ./scss/... and ./js/.... On missing nested production paths such as /foo/ or /foo/bar, those relative URLs resolve against the missing route depth and can load incorrectly.
Changes
Validation