Fix crashes on *-once elements when called at bad timings

This commit is contained in:
2026-05-06 23:19:35 +02:00
parent 38f2b6aa7b
commit 0405ef424d
2 changed files with 8 additions and 4 deletions
@@ -84,8 +84,12 @@ const refreshElement = <
return; return;
} }
// This might be called at some bad timing
// This prevents crashes of the observer
if (element.refresh) {
element.refresh(); element.refresh();
} }
}
}; };
// Since we need to pause the observer, we make an helper to start it with consistent arguments // Since we need to pause the observer, we make an helper to start it with consistent arguments
@@ -130,7 +134,7 @@ startObserver(observer);
export class LinkOnce extends elementOnce("link") { export class LinkOnce extends elementOnce("link") {
getElementQuerySelector(): string { getElementQuerySelector(): string {
// We get href from node.attributes instead of node.href to avoid getting the domain part // We get href from node.attributes instead of node.href to avoid getting the domain part
return `link[href='${this.node.attributes.getNamedItem("href").nodeValue}']`; return `link[href='${this.node.attributes.getNamedItem("href")?.nodeValue}']`;
} }
} }
@@ -142,6 +146,6 @@ export class LinkOnce extends elementOnce("link") {
export class ScriptOnce extends inheritHtmlElement("script") { export class ScriptOnce extends inheritHtmlElement("script") {
getElementQuerySelector(): string { getElementQuerySelector(): string {
// We get href from node.attributes instead of node.src to avoid getting the domain part // We get href from node.attributes instead of node.src to avoid getting the domain part
return `script[src='${this.node.attributes.getNamedItem("src").nodeValue}']`; return `script[src='${this.node.attributes.getNamedItem("src")?.nodeValue}']`;
} }
} }
@@ -3,7 +3,7 @@
<script-once type="module" src="{{ js }}"></script-once> <script-once type="module" src="{{ js }}"></script-once>
{% endfor %} {% endfor %}
{% for css in statics.css %} {% for css in statics.css %}
<link-once rel="stylesheet" type="text/css" href="{{ css }}" defer></link-once> <link-once rel="stylesheet" type="text/css" href="{{ css }}"></link-once>
{% endfor %} {% endfor %}
<{{ component }} name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %}> <{{ component }} name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %}>