Fixed relative images not being resolved for instant navigation

This commit is contained in:
squidfunk 2024-02-03 17:48:12 +07:00
parent bcad9cecd1
commit a7c71101f9
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
4 changed files with 15 additions and 9 deletions

View File

@ -249,7 +249,7 @@
</script>
{% endblock %}
{% block scripts %}
<script src="{{ 'assets/javascripts/bundle.caa56a14.min.js' | url }}"></script>
<script src="{{ 'assets/javascripts/bundle.8fd75fb4.min.js' | url }}"></script>
{% for script in config.extra_javascript %}
{{ script | script_tag }}
{% endfor %}

View File

@ -145,16 +145,22 @@ function head(document: Document): Map<string, HTMLElement> {
/**
* Resolve relative URLs in the given document
*
* This function resolves relative `href` and `src` attributes, which can belong
* to all sorts of tags, like meta tags, links, images, scripts and more.
*
* @param document - Document
*
* @returns Document observable
*/
function resolve(document: Document): Observable<Document> {
for (const el of getElements<HTMLLinkElement>("[href], [src]", document))
for (const key in ["href", "src"]) {
for (const el of getElements("[href], [src]", document))
for (const key of ["href", "src"]) {
const value = el.getAttribute(key)
if (!/^(?:[a-z]+:)?\/\//i.test(value!))
el.href = el.href
if (value && !/^(?:[a-z]+:)?\/\//i.test(value)) {
// @ts-expect-error - trick: self-assign to resolve URL
el[key] = el[key]
break
}
}
// Return document observable