title: "window is not defined" service worker
body: <p>I am trying to register a service worker for my django project. This is the code I am using for registration:</p>
  
  <pre><code>&lt;!-- register service worker --&gt;
  &lt;script type="text/javascript"&gt;
    if('serviceWorker' in navigator) {
      window.addEventListener('load', function() {
        navigator.serviceWorker.register("{% url 'sw.js' %}").then(function(registration) {
          console.log("Service worker registered.");
          console.log("Registered at: "+registration.scope);
        });
      });
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>and this is the code in my service_worker.js:</p>
  
  <pre><code>// App install banner
  window.addEventListener('beforeinstallprompt', function(e) {
    e.userChoice.then(function(choiceResult) {
      console.log(choiceResult.outcome);
  
      if(choiceResult.outcome == 'dismissed') {
        console.log('User cancelled home screen install');
      } else {
        console.log('User added to home screen');
      }
    });
  });
  </code></pre>
  
  <p>I see this error in console: <code>Uncaught ReferenceError: window is not defined</code></p>
  
  <p>How to fix this?</p>