Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better relative urls support #1980

Merged
merged 2 commits into from
Mar 18, 2025
Merged

Conversation

dscho
Copy link
Member

@dscho dscho commented Mar 15, 2025

Changes

  • This improves support for relative URLs (which are used by default when running Hugo locally, although they are not used when deploying the site). This helps local testing.

Context

I was annoyed enough with these problems when testing #1888 in my development setup that I had to fix them, but since they have nothing to do with dark mode support, I've split them out into their own PR.

dscho added 2 commits March 15, 2025 14:22
When running Hugo with relative URLs, as is the case when a developer
runs `hugo` in a regular checkout of this repository, there is no
`https://` prefix, and therefore the current logic failed.

Fix this logic, and while at it, simplify the preceding code a bit.

Note that this logic needs further adjustment, then, for importing the
`pagefind.js` library: This `import`, if relative, is relative to the
location of `application.js` (which is not the case where
`baseURLPrefix` is used to set `href` and `src` attributes, which are
relative to the top-level directory of the site). This _happened_ to
work before because the bug was hidden by the bug where `baseURLPrefix`
was not adjusted correctly for `HUGO_RELATIVEURLS=true`.

Signed-off-by: Johannes Schindelin <[email protected]>
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]>
@dscho dscho self-assigned this Mar 15, 2025
@To1ne
Copy link
Contributor

To1ne commented Mar 18, 2025

I have no idea what this does, but sure :shipit: !

@dscho
Copy link
Member Author

dscho commented Mar 18, 2025

I have no idea what this does,

@To1ne It fixes the $baseURLPrefix computation in application.js.

The idea of $baseURLPrefix is that the script will first determine the location of itself, know that it is in <base-URL>/js/application.min.js, and therefore now know the prefix to use when replacing, say, the download links based on the User-Agent.

The problem solved by this PR is that this computation did not work when the script location was ./js/application.js, as it expected something like https://git-scm.com/js/application.js or file:///home/To1ne/git-scm.com/public/js/application.js instead, i.e. a URL with a protocol.

The other problem solved by this PR is that when testing locally via script/serve-public.js and accidentally browsing to an invalid URL, it would only show the properly-styled 404 page on top-level URLs, but not in "subdirectories". For example, http://localhost:5000/does/not/exist would look like this:

Before After
image image

but sure :shipit: !

👍

@dscho dscho merged commit 01ce53a into git:gh-pages Mar 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants