0

I have a script on a page:

<script type="text/javascript">app_id="ID_HERE";distribution_key="dist_6";</script><script type="text/javascript" src="https://loader.knack.com/ID_HERE/dist_6/knack.js"></script><div id="knack-dist_6">Loading...</div>

If I go to the page via a NuxtLink in the navigation the script runs, however if I type the URL to the browser address bar it doesn't.

Is there a way to force the NuxtLink result when the page is accessed directly?

The script tag is coming from a CMS so there isn't a way to hardcode it somewhere nice.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59
  • Does this answer your question? [How to add a 3rd party script code into Nuxt?](https://stackoverflow.com/questions/67534304/how-to-add-a-3rd-party-script-code-into-nuxt) – kissu Sep 27 '21 at 08:33

1 Answers1

0

There is a head method (Docs) which will let you to load external scripts but I can't find the documentation for the script property.

I would dynamically load the script only on the client side like this:

<template>
  <div id="knack-dist_6">Loading...</div>
</template>

<script>
export default {
  // ...
  async created() {
    // Do not load the script on the server side
    if (!process.client) return
   
    // Function to load the external script
    function loadLib(id, distKey) {
      const scriptId = 'knack-js'

      return new Promise((resolve, reject) => {
        // If script already exists than do not load it again
        if (document.getElementById(scriptId)) {
          resolve()
          return
        }

        const s = document.createElement('script')

        s.src = `https://loader.knack.com/${id}/${distKey}/knack.js`
        s.id = scriptId
        s.type = 'text/javascript'

        s.onload = () => {
          resolve()
        }

        s.onerror = (e) => {
          reject(e)
        }

        document.head.appendChild(s)
      })
    }

    try {
     if (!window.app_id) {
        window.app_id = 'ID_HERE'
        window.distribution_key = 'dist_6'
     }

      await loadLib('ID_HERE', 'dist_6')
    } catch (e) {
      // Handle script loading error
      console.error(e)
    }
  }
}
</script>
Florin Relea
  • 513
  • 6
  • 15
  • 1
    Hi Florin this is fantastic and it works great when going to the page directly not via a nuxtlink - yay! I can see in my terminal: `window is not defined` and it get lots of `Invalid 'X-Frame-Options' header encountered when loading 'https://ap-southeast-2-renderer-read.knack.com/': ''allow-from' https://ap-southeast-2-renderer-read.knack.com' is not a recognized directive. The header will be ignored.` – Michael Story Sep 27 '21 at 08:28
  • 1
    I wrapped the whole thing in `if (process.browser) {` and changed `try { if (!window.app_id) {` to `try { if (process.browser) {` and I don't get errors in dev anymore – Michael Story Sep 27 '21 at 09:06
  • Hi Michael! Glad to help! Also, `if (process.browser) {` is the right fix but did you add `if (!process.client) return` after `async created() {` ? That should've solve the server problem. – Florin Relea Sep 28 '21 at 05:00
  • Thanks Florin I really appreciate it :) – Michael Story Sep 28 '21 at 22:00