Skip to content
Discussion options

You must be logged in to vote

onMount runs only in the browser, but onDestroy also runs on the server side (during SSR), and in your code there's a check missing if this is a browser environment. This should solve it:

<script>
  import { browser } from '$app/environment'; // (was '$app/env' in a pre 1.0 SvelteKit version)
  // ...
  onDestroy(() => {
    if (browser) {
      document.removeEventListener('keydown', onKeydown);
    }
  });

https://kit.svelte.dev/docs#modules-$app-env

Edit: you can also use onMount for this, see the subanswer below.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@SiegfriedEhret
Comment options

@CaptainCodeman
Comment options

@SiegfriedEhret
Comment options

@Explosion-Scratch
Comment options

@bitdom8
Comment options

Answer selected by SiegfriedEhret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #2740 on November 04, 2021 13:08.