Skip to content

Commit 9b59a30

Browse files
committed
script/serve-public.js(404): handle relative URLs
When building the site with `HUGO_RELATIVEURLS=true` (which is the default when building locally), the `404.html` page rightfully assumes that its resources, such as images, can be found via relative URLs like `./images/favicon.ico`. That leads to a problem when the `serve-public.js` script wants to Use it in case it does not have certain file that is in a subdirectory. For example, when a developer directs their web browser to http://localhost:5000/docs/git-undo, the result will be the 4O4 page, but it will be unstyled because it looks for the CSS and the wrong location. The solution is to specify the base URL explictly in the served 404 page. In this instance it is easy because we know exactly that this script will serve the site via http://localhost:5000/ and therefore the base url is simply the `/`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 41436cc commit 9b59a30

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

script/serve-public.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ const handler = (request, response) => {
4949
} catch(e) {
5050
console.log(`Could not read ${filename}`);
5151
response.writeHead(404, {'Content-Type': 'text/html'});
52-
fs.createReadStream(path.join(basePath, '404.html')).pipe(response);
52+
// insert <base> to fix styling
53+
const html = fs.readFileSync(path.join(basePath, '404.html'), 'utf-8')
54+
.replace(/<head>/, '\n <base href="/" />')
55+
response.write(html)
56+
response.end()
5357
return;
5458
}
5559
};

0 commit comments

Comments
 (0)