S
SolidJS•2y ago
Martnart

document is not defined in HMR

I have some logic that is running in onMount and onCleanup that adds/removes a data-attribute from an element. It works. However when I make a change somewhere in my project, I get document is not defined error from HMR. It seems to be triggered by cleanNode so I am pretty sure it's from the onCleanup. The code itself looks like this shouldn't happen though because I use optional chaining. Any ideas?
onMount(() => {
const outlet = document?.getElementById('main-outlet')
outlet && (outlet.dataset.actionbar = 'true')
})

onCleanup(() => {
const outlet = document?.getElementById('main-outlet')
outlet?.dataset.actionbar && delete outlet.dataset.actionbar
})
onMount(() => {
const outlet = document?.getElementById('main-outlet')
outlet && (outlet.dataset.actionbar = 'true')
})

onCleanup(() => {
const outlet = document?.getElementById('main-outlet')
outlet?.dataset.actionbar && delete outlet.dataset.actionbar
})
Thank you 🙂
2 Replies
Martnart
MartnartOP•2y ago
Could this be a bug? Looking at the code I would never expect to get this error. If I change it to this it works
onCleanup(() => {
const outlet = globalThis?.document?.getElementById('main-outlet')
outlet?.dataset.actionbar && delete outlet.dataset.actionbar
})
onCleanup(() => {
const outlet = globalThis?.document?.getElementById('main-outlet')
outlet?.dataset.actionbar && delete outlet.dataset.actionbar
})
lxsmnsyc
lxsmnsyc•2y ago
Not a bug. There's just no document on the server. I would recommend moving your onCleanup inside onMount
Want results from more Discord servers?
Add your server