<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Posts tagged #external scripts</title><description>All posts tagged with external scripts on queen.raae.codes</description><link>https://queen.raae.codes/tag/external-scripts/</link><item><title>💬 💬 ~ Using the Gatsby Script component to add a Hopscotch tour</title><link>https://queen.raae.codes/2022-10-14-script-component-hopscotch/</link><guid isPermaLink="true">https://queen.raae.codes/2022-10-14-script-component-hopscotch/</guid><description>Yesterday, we added an onboarding tour to &quot;Prune your follows&quot; together with Cam Sloan of Hopscotch unauthorized and rum-fueled treasure hunt in the sharky…</description><pubDate>Fri, 14 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Yesterday, we added an onboarding tour to &amp;quot;Prune your follows&amp;quot; together with Cam Sloan of Hopscotch &lt;a href=&quot;https://youtu.be/yDxF8FUNUbI&quot;&gt;unauthorized and rum-fueled treasure hunt&lt;/a&gt; in the sharky waters around the Gatsby islands 🏴‍☠️&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/yDxF8FUNUbI&quot;&gt;&lt;img src=&quot;screengrab-show.jpg&quot; alt=&quot;Screengrab from the show&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To do so, we needed to add an external script. Since I wrote about &lt;a href=&quot;/2022-01-05-external-scripts/&quot;&gt;Third party scripts with Gatsby&lt;/a&gt; back in January, Gatsby has added a new way: the &lt;a href=&quot;https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-script/&quot;&gt;&lt;code&gt;Script&lt;/code&gt; component&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It can be used to add both inline scripts and external scripts.&lt;/p&gt;
&lt;p&gt;Hopscotch provides us with an inline script:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;&amp;lt;script id=&amp;quot;hopscotch-script&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;(function (w, d) {if (typeof w === &amp;quot;undefined&amp;quot;) return;w.Hopscotch = w.Hopscotch || function () {(w.Hopscotch.q = w.Hopscotch.q || []).push(arguments);};var elm = d.createElement(&amp;quot;div&amp;quot;);elm.setAttribute(&amp;quot;data-widget-host&amp;quot;, &amp;quot;hopscotch&amp;quot;);elm.setAttribute(&amp;quot;data-props-api-key&amp;quot;, &amp;quot;39311cf6-d7b7-5e00-b8e7&amp;quot;);d.getElementsByTagName(&amp;quot;body&amp;quot;)[0].appendChild(elm);var s = d.createElement(&amp;quot;script&amp;quot;);s.src = &amp;quot;https://widgets.hopscotch.club/v1/widget.js?&amp;quot;;s.async = 1;s.defer = 1;d.getElementsByTagName(&amp;quot;body&amp;quot;)[0].appendChild(s);  })(window, document);&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To make it work as a &lt;code&gt;Script&lt;/code&gt; component, we had to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Exchange &lt;code&gt;script&lt;/code&gt; for &lt;code&gt;Script&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Make the inline code a &amp;quot;template literal&amp;quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-jsx&quot;&gt;&amp;lt;Script
  id=&amp;quot;hopscotch-script&amp;quot;
  type=&amp;quot;text/javascript&amp;quot;
&amp;gt;{`(function (w, d) {if (typeof w === &amp;quot;undefined&amp;quot;) return;w.Hopscotch = w.Hopscotch || function () {(w.Hopscotch.q = w.Hopscotch.q || []).push(arguments);};var elm = d.createElement(&amp;quot;div&amp;quot;);elm.setAttribute(&amp;quot;data-widget-host&amp;quot;, &amp;quot;hopscotch&amp;quot;);elm.setAttribute(&amp;quot;data-props-api-key&amp;quot;, &amp;quot;39311cf6-d7b7-5e00-b8e7&amp;quot;);d.getElementsByTagName(&amp;quot;body&amp;quot;)[0].appendChild(elm);var s = d.createElement(&amp;quot;script&amp;quot;);s.src = &amp;quot;https://widgets.hopscotch.club/v1/widget.js?&amp;quot;;s.async = 1;s.defer = 1;d.getElementsByTagName(&amp;quot;body&amp;quot;)[0].appendChild(s);  })(window, document);`}&amp;lt;/Script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, we set up the tour in Hopscotch 🎉&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./product-tour.jpg&quot; alt=&quot;Screengrab of product tour&quot;&gt;&lt;/p&gt;
&lt;p&gt;I think the whole thing took us less than 15 minutes, but we managed to stream for a full hour anyhow: chatting, testing things out, and meeting Cam&apos;s dog 🤪&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;All the best,&lt;br&gt;
Queen Raae&lt;/p&gt;
</content:encoded></item><item><title>﹤ ﹥ ~ The new Script Component - recommended reading</title><link>https://queen.raae.codes/2022-06-09-script-tag/</link><guid isPermaLink="true">https://queen.raae.codes/2022-06-09-script-tag/</guid><description>There is a new solutions to Third-party scripts with Gatsby: the Script Component. Get the low down from Ty Hopp, Senior Software Engineer at Gatsby. - Using…</description><pubDate>Thu, 09 Jun 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;There is a new solutions to &lt;a href=&quot;/2022-01-05-external-scripts/&quot;&gt;Third-party scripts with Gatsby&lt;/a&gt;: the Script Component.&lt;/p&gt;
&lt;p&gt;Get the low down from Ty Hopp, Senior Software Engineer at Gatsby.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.gatsbyjs.com/blog/using-gatsby-script-component-to-decrease-page-load-times/&quot;&gt;Using Gatsby Script Component to Decrease Page Load Times (article)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://youtu.be/ioBiW1_g-cg&quot;&gt;Gatsby Script Component (demo)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;All the best,&lt;br&gt;
Queen Raae&lt;/p&gt;
</content:encoded></item><item><title>🔴 👩‍🏫 ~ Third-party scripts with Gatsby</title><link>https://queen.raae.codes/2022-01-05-external-scripts/</link><guid isPermaLink="true">https://queen.raae.codes/2022-01-05-external-scripts/</guid><description>Let&apos;s say you want to embed a Tweet or add website analytics. How would you go about including the external third-party script required? That was the topic for…</description><pubDate>Wed, 05 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Let&apos;s say you want to embed a Tweet or add website analytics. How would you go about including the external third-party script required?&lt;/p&gt;
&lt;p&gt;That was the topic for my first ever Live Screencasts — concise, instructional videos on a single Gatsby topic.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://youtu.be/Kldx6d5XBSE&quot;&gt;Watch the Live Screeencast&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have found three practical ways of doing so with Gatsby.&lt;/p&gt;
&lt;h2&gt;1. &lt;code&gt;onRenderBody&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;An excellent solution for global scripts must be available on every page, such as website analytics.&lt;/p&gt;
&lt;p&gt;With &lt;code&gt;onRenderBody&lt;/code&gt;, the script is added when the site statically generates. The script tag is part of the initial HTML even before the site rehydrates.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Kldx6d5XBSE&amp;amp;t=74s&quot;&gt;Jump in to see &lt;code&gt;onRenderBody&lt;/code&gt; in action&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;import React from &amp;quot;react&amp;quot;;

const headComponents = [
  &amp;lt;script
    key=&amp;quot;fathom-script&amp;quot;
    src=&amp;quot;https://cdn.usefathom.com/script.js&amp;quot;
    data-site=&amp;quot;CNOZPPSE&amp;quot;
    defer
  &amp;gt;&amp;lt;/script&amp;gt;,
];

export const onRenderBody = ({setHeadComponents}) =&amp;gt; {
  setHeadComponents(headComponents);
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;2. React Helmet&lt;/h2&gt;
&lt;p&gt;Using React Helmet is another method for adding the script as part of the statically generated pages. However, it makes it easy to include the script only on certain pages.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Kldx6d5XBSE&amp;amp;t=360s&quot;&gt;Jump in to to see React Helmet in action&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;import React from &amp;quot;react&amp;quot;;
import {Helmet} from &amp;quot;react-helmet&amp;quot;;

const TweetPageReactHelmet = () =&amp;gt; {
  return (
    &amp;lt;main&amp;gt;
      &amp;lt;Helmet&amp;gt;
        &amp;lt;script async src=&amp;quot;https://platform.twitter.com/widgets.js&amp;quot; /&amp;gt;
      &amp;lt;/Helmet&amp;gt;

      &amp;lt;section&amp;gt;
        &amp;lt;blockquote class=&amp;quot;twitter-tweet&amp;quot;&amp;gt;
          {/* ... rest of tweet embed code */}
        &amp;lt;/blockquote&amp;gt;{&amp;quot; &amp;quot;}
      &amp;lt;/section&amp;gt;
    &amp;lt;/main&amp;gt;
  );
};

export default TweetPageReactHelmet;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;3. &lt;code&gt;document.createElement&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Suppose you want to have more control over when a script is loaded. In that case, you can do so by manipulating the DOM inside a &lt;code&gt;useEffect&lt;/code&gt; (or &lt;code&gt;onClick&lt;/code&gt; or another event that makes sense...).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Kldx6d5XBSE&amp;amp;t=619s&quot;&gt;Jump in to see &lt;code&gt;document.createElement&lt;/code&gt; in action&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;import React, {useEffect} from &amp;quot;react&amp;quot;;

const TweetPageCreateElement = () =&amp;gt; {
  useEffect(() =&amp;gt; {
    const script = document.createElement(&amp;quot;script&amp;quot;);
    script.src = &amp;quot;https://platform.twitter.com/widgets.js&amp;quot;;
    document.head.appendChild(script);
    return () =&amp;gt; {
      script.remove();
    };
  }, []);

  return (
    &amp;lt;main&amp;gt;
      &amp;lt;section&amp;gt;
        &amp;lt;blockquote class=&amp;quot;twitter-tweet&amp;quot;&amp;gt;
          {/* ... rest of tweet embed code */}
        &amp;lt;/blockquote&amp;gt;
      &amp;lt;/section&amp;gt;
    &amp;lt;/main&amp;gt;
  );
};

export default TweetPageCreateElement;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;br&gt;
Have you used any of these?&lt;br&gt;
Do you know of another way?&lt;/p&gt;
&lt;p&gt; &lt;br&gt;
All the best,&lt;br&gt;
Queen Raae&lt;/p&gt;
</content:encoded></item></channel></rss>