title: Nuxtjs - 3rd Party Script causes "TypeError: Cannot read properties of null (reading 'document')" loop in console
body: <p>I have a 3rd party ad script that I inject into my nuxt component using postscribe. The website works fine. However if I try to navigate away from the page without letting the script to finish loading. I get hit with the error loop below (in the browser console). The error show every millisecond and it doesn't go away unless I refresh the page.</p>
  <p><code>invoke.js:1 Uncaught TypeError: Cannot read properties of null (reading 'document')</code></p>
  <p>This is the code that I inject using postscribe</p>
  <pre><code>this.$postscribe('#myDivId', `
                      &lt;script type=&quot;text/javascript&quot;&gt;
                              atOptions = {
                                  'key' : 'fiajiewfoj3jiosfo',
                                  'format' : 'iframe',
                                  'height' : 90,
                                  'width' : 728,
                                  'params' : {}
                                  };
                                  document.write('&lt;scr' + 'ipt type=&quot;text/javascript&quot; src=&quot;http' + (location.protocol === 'https:' ? 's' : '') + '://script-src.com/12345767890/invoke.js&quot;&gt;&lt;/scr' + 'ipt&gt;');
                              &lt;\/script&gt;`
                  );
  </code></pre>
  <p>I did try injecting it using vanilla javascript but that did not work as well.</p>
  <pre><code>let a= document.getElementById(&quot;myDiv&quot;)
  let basePath = document.createElement('script')
  basePath.type = &quot;text/javascript&quot;
  basePath.setAttribute('async', 'true');
  basePath.innerHTML = `&lt;script type=&quot;text/javascript&quot;&gt;
                              atOptions = {
                                  'key' : 'fiajiewfoj3jiosfo',
                                  'format' : 'iframe',
                                  'height' : 90,
                                  'width' : 728,
                                  'params' : {}
                                  };
                                  document.write('&lt;scr' + 'ipt type=&quot;text/javascript&quot; src=&quot;http' + (location.protocol === 'https:' ? 's' : '') + '://script-src.com/12345767890/invoke.js&quot;&gt;&lt;/scr' + 'ipt&gt;');
                              &lt;\/script&gt;`
  a.appendChild(basePath)
  
  </code></pre>
  <p>It also isn't fixed, if I'm running it with if (process.client) i.e.
  <code>if(process.client){this.$postscribe.......}</code></p>
  <p>I suspect it has something to do with document.write() but I have no experience with document.write() and its behaviour.</p>
  <p>Note: I didn't put in into the head() because I want this to appear in a particular div / component.</p>