#document is not defined in HMR

1 messages · Page 1 of 1 (latest)

icy stirrup
#

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
})

Thank you 🙂

#

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
})