svelte: Failing to set `` element with `getAttribute is not a function`
Describe the bug
This Svelte code randomly causes the following issue:
[!CAUTION] Uncaught (in promise) TypeError: element2.getAttribute is not a function
<script>
import logo from '$lib/logo.svg?no-inline';
</script>
<svelte:head>
<link rel="icon" href={logo} />
</svelte:head>
<slot></slot>
Uncaught (in promise) TypeError: element2.getAttribute is not a function
in +layout.svelte
in root.svelte
set_attribute attributes.js:154
_layout +layout.svelte:48
/**
* @param {Element} element
* @param {string} attribute
* @param {string | null} value
* @param {boolean} [skip_warning]
*/
export function set_attribute(element, attribute, value, skip_warning) {
// @ts-expect-error
var attributes = (element.__attributes ??= {});
if (hydrating) {
attributes[attribute] = element.getAttribute(attribute); // here
function _layout($$anchor, $$props) {
$.check_target(new.target);
$.push($$props, true, _layout);
var fragment_1 = $.comment();
$.head(($$anchor) => {
var fragment = root_1();
var link = $.first_child(fragment);
$.set_attribute(link, 'href', logo); // here
Reproduction
Failed to create a reproduction.
It suddenly happens without changing a single-line of code in an existing, working, error free code-base.
Attempt for debugging documented in this issue:
Logs
System Info
System:
OS: Windows 11 10.0.26100
CPU: (8) x64 Intel(R) Core(TM) Ultra 7 258V
Memory: 13.81 GB / 31.51 GB
Binaries:
Node: 23.7.0 - ~\AppData\Local\fnm_multishells\6624_1739936373546\node.EXE
npm: 10.9.2 - ~\AppData\Local\fnm_multishells\6624_1739936373546\npm.CMD
pnpm: 10.4.0 - ~\AppData\Local\fnm_multishells\6624_1739936373546\pnpm.CMD
Browsers:
Edge: Chromium (131.0.2903.112)
Severity
annoyance
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (9 by maintainers)
Here is repro code. +layout.svelte And The error disappeared by hard-coding the variable. I guess is that the issue seems to occur only in scripts that directly access the DOM and trigger hydration.
Thanks for the insights @Sasja.
I checked sentry and the vast majority are Chrome (up to the latest stable versions), with Chromium-based browsers (particularly new Edge), Firefox, Opera, and even Safari mixed in, both mobile and desktop for all.
So it doesn’t seem to have a favorite browser in my case. 😦
Its looks like a race condition between svelte and plugin code. For me it’s the BitWarden plugin on Firefox. It injects a script in the head section in a seemingly random location, it seems that depending on where (or when) it lands this messes up the hydration. Triggering that
s.getAttribute()on a text node. instead of the intended node.svelte seems to iterate over the head elements in a hardcoded order and doesnt expect this element:
note: the function
e(x, 2)here seems to select the next sibling node. note: o() is the methodset_attribute()screenshotted above by @hyunbinseowhat i see happening in the dom before the error is that svelte injected attributes in the unexpected script tag and then tries to step to the next element and unexpectedly gets a text element causing the crash when it calls getAttribute on it.
I guess the problem might be that svelte wrongly assumes the head section cannot be modified before or during running the code stepping over each element.
Hope this helps.
@hyunbinseo . The position where BitWarden injects the script tag looks pretty random which could explain this.
@wildyaboku maybe worth to check if these happen to be Firefox users? To figure out if it’s either an issue with the Firefox plugin system or with specific plugins regardless of browser.
Ever since moving to Svelte 5, I have also been seeing this error appear in sentry.io for an extremely small portion of users.
The stack traces point to the first line inside a
<svelte:head>that uses a variable (usually meta tags or preloads), in virtually every page that does so.I haven’t been able to reproduce at all so far. I tried installing AdGuard and it didn’t trigger it for me.
There is nothing special about my code. It’s effectively the official SvelteKit template with TS, Tailwind, and a meta tag that uses a variable, like in OP’s post.
It looks like AdGuard injects two scripts with
\nbetween them into the head and the scripts remove their own nodes, but the\nis left.If the bug is there we need to fix it…we just need a jump start to not waste hours on something that might not be an issue.