goatcounter icon indicating copy to clipboard operation
goatcounter copied to clipboard

Unexpected token < in JSON at position 0 when using on Nuxt js site

Open mdunbavan opened this issue 3 years ago • 1 comments

Hi,

We are using this on a Nuxt site. When we refresh the site we get the error above.

The code we are using is below in the nuxt.config.js site.

script: [
      {
        type: 'text/javascript',
        src: '//gc.zgo.at/count.js',
        'data-goatcounter': 'https://securityscorecards.goatcounter.com/count',
        async: true,
      },
      {
        type: 'text/javascript',
        innerHTML:`
          window.goatcounter = {no_onload: true}

          window.addEventListener("hashchange", function(e) {
              window.goatcounter.count({
                  path: location.pathname + location.search + location.hash,
              })
          })`
      }
    ],

This basically loads the site into the head.

Does anyone know why we might hit this error?

mdunbavan avatar Apr 12 '22 17:04 mdunbavan

I found this, which links to https://staging-netlify-test--elastic-noether-393172.netlify.app/, and it seems it works correctly now?

The goatcounter count script doesn't really do much with JSON; it only loads the data-goatcounter-settings attribute as JSON, and you're not using that, and you should also get a invalid JSON in data-goatcounter-settings: ' + err message if that fails.

So it seems like somehow Nuxt is expecting JSON ... somewhere, but not getting it?

arp242 avatar Jun 06 '22 17:06 arp242