Static GitHub Issues

[2962] Nuxt amp example: should not remove all script tags

prev: Update router base dynamically
next: Middleware store dispatch or commit before redirect will not write data on server

Hi,

I'm testing nuxt for an Amp site. Everything is working perfectly (thanks ❤️ ) except one tiny thing. In the examples you gave

// Remove every script tag from generated HTML
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')

All scripts tags are removed without consideration, but the solution it not good because amp does allow <script type="application/json"></script> (in amp-analytics for example) It's not really a bug but it can be confusing for newcomers if they simply copy the code :)

Because I'm not a regex master, I didn't succeed in modifying the regex to only match script tags that does not have that type (screw you negative lookahead). So I'v done this (which is absolutely not bulletproof but a quick fix)

html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, (str) => {
    if (/application\/json/.test(str)) {
      return str;
    }
    return '';
  });

If someone have a better fix 👍

thanks

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This feature request is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c2569">#c2569</a>)</em></sub></div>