Add Snipcart to Gatsby











up vote
3
down vote

favorite












I'm trying to integrate Snipcart into Gatsby (v2).



I edit the html.js file like this:



import React from "react"
import PropTypes from "prop-types"

export default class HTML extends React.Component {
render() {
return (
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{this.props.headComponents}

{/* Snipcart */}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://cdn.snipcart.com/scripts/2.0/snipcart.js" id="snipcart" data-api-key="UF45pIjZjAtZWJkYS00MGEwLWIxZWEtNjljZThjNTRiNjA4NjM2NDg1MzAzMzQyNfDrr48"></script>
<link href="https://cdn.snipcart.com/themes/2.0/base/snipcart.min.css" type="text/css" rel="stylesheet" />

</head>
<body {...this.props.bodyAttributes}>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}

</body>
</html>
)
}
}

HTML.propTypes = {
htmlAttributes: PropTypes.object,
headComponents: PropTypes.array,
bodyAttributes: PropTypes.object,
preBodyComponents: PropTypes.array,
body: PropTypes.string,
postBodyComponents: PropTypes.array,
}


What works:



If I create a div:



<a href="#" class="snipcart-edit-profile">
   Edit profile
</a>


Snipcart works and opens the user profile when clicked.



What does not work:



When I want to use the public API, for example if I call:



Snipcart.api.user.logout() in a function.



=> error 'Snipcart' is not defined no-undef



How to have the global Snipcart object in all my app?










share|improve this question


























    up vote
    3
    down vote

    favorite












    I'm trying to integrate Snipcart into Gatsby (v2).



    I edit the html.js file like this:



    import React from "react"
    import PropTypes from "prop-types"

    export default class HTML extends React.Component {
    render() {
    return (
    <html {...this.props.htmlAttributes}>
    <head>
    <meta charSet="utf-8" />
    <meta httpEquiv="x-ua-compatible" content="ie=edge" />
    <meta
    name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    {this.props.headComponents}

    {/* Snipcart */}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://cdn.snipcart.com/scripts/2.0/snipcart.js" id="snipcart" data-api-key="UF45pIjZjAtZWJkYS00MGEwLWIxZWEtNjljZThjNTRiNjA4NjM2NDg1MzAzMzQyNfDrr48"></script>
    <link href="https://cdn.snipcart.com/themes/2.0/base/snipcart.min.css" type="text/css" rel="stylesheet" />

    </head>
    <body {...this.props.bodyAttributes}>
    {this.props.preBodyComponents}
    <div
    key={`body`}
    id="___gatsby"
    dangerouslySetInnerHTML={{ __html: this.props.body }}
    />
    {this.props.postBodyComponents}

    </body>
    </html>
    )
    }
    }

    HTML.propTypes = {
    htmlAttributes: PropTypes.object,
    headComponents: PropTypes.array,
    bodyAttributes: PropTypes.object,
    preBodyComponents: PropTypes.array,
    body: PropTypes.string,
    postBodyComponents: PropTypes.array,
    }


    What works:



    If I create a div:



    <a href="#" class="snipcart-edit-profile">
       Edit profile
    </a>


    Snipcart works and opens the user profile when clicked.



    What does not work:



    When I want to use the public API, for example if I call:



    Snipcart.api.user.logout() in a function.



    => error 'Snipcart' is not defined no-undef



    How to have the global Snipcart object in all my app?










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm trying to integrate Snipcart into Gatsby (v2).



      I edit the html.js file like this:



      import React from "react"
      import PropTypes from "prop-types"

      export default class HTML extends React.Component {
      render() {
      return (
      <html {...this.props.htmlAttributes}>
      <head>
      <meta charSet="utf-8" />
      <meta httpEquiv="x-ua-compatible" content="ie=edge" />
      <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
      />
      {this.props.headComponents}

      {/* Snipcart */}
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
      <script src="https://cdn.snipcart.com/scripts/2.0/snipcart.js" id="snipcart" data-api-key="UF45pIjZjAtZWJkYS00MGEwLWIxZWEtNjljZThjNTRiNjA4NjM2NDg1MzAzMzQyNfDrr48"></script>
      <link href="https://cdn.snipcart.com/themes/2.0/base/snipcart.min.css" type="text/css" rel="stylesheet" />

      </head>
      <body {...this.props.bodyAttributes}>
      {this.props.preBodyComponents}
      <div
      key={`body`}
      id="___gatsby"
      dangerouslySetInnerHTML={{ __html: this.props.body }}
      />
      {this.props.postBodyComponents}

      </body>
      </html>
      )
      }
      }

      HTML.propTypes = {
      htmlAttributes: PropTypes.object,
      headComponents: PropTypes.array,
      bodyAttributes: PropTypes.object,
      preBodyComponents: PropTypes.array,
      body: PropTypes.string,
      postBodyComponents: PropTypes.array,
      }


      What works:



      If I create a div:



      <a href="#" class="snipcart-edit-profile">
         Edit profile
      </a>


      Snipcart works and opens the user profile when clicked.



      What does not work:



      When I want to use the public API, for example if I call:



      Snipcart.api.user.logout() in a function.



      => error 'Snipcart' is not defined no-undef



      How to have the global Snipcart object in all my app?










      share|improve this question













      I'm trying to integrate Snipcart into Gatsby (v2).



      I edit the html.js file like this:



      import React from "react"
      import PropTypes from "prop-types"

      export default class HTML extends React.Component {
      render() {
      return (
      <html {...this.props.htmlAttributes}>
      <head>
      <meta charSet="utf-8" />
      <meta httpEquiv="x-ua-compatible" content="ie=edge" />
      <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
      />
      {this.props.headComponents}

      {/* Snipcart */}
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
      <script src="https://cdn.snipcart.com/scripts/2.0/snipcart.js" id="snipcart" data-api-key="UF45pIjZjAtZWJkYS00MGEwLWIxZWEtNjljZThjNTRiNjA4NjM2NDg1MzAzMzQyNfDrr48"></script>
      <link href="https://cdn.snipcart.com/themes/2.0/base/snipcart.min.css" type="text/css" rel="stylesheet" />

      </head>
      <body {...this.props.bodyAttributes}>
      {this.props.preBodyComponents}
      <div
      key={`body`}
      id="___gatsby"
      dangerouslySetInnerHTML={{ __html: this.props.body }}
      />
      {this.props.postBodyComponents}

      </body>
      </html>
      )
      }
      }

      HTML.propTypes = {
      htmlAttributes: PropTypes.object,
      headComponents: PropTypes.array,
      bodyAttributes: PropTypes.object,
      preBodyComponents: PropTypes.array,
      body: PropTypes.string,
      postBodyComponents: PropTypes.array,
      }


      What works:



      If I create a div:



      <a href="#" class="snipcart-edit-profile">
         Edit profile
      </a>


      Snipcart works and opens the user profile when clicked.



      What does not work:



      When I want to use the public API, for example if I call:



      Snipcart.api.user.logout() in a function.



      => error 'Snipcart' is not defined no-undef



      How to have the global Snipcart object in all my app?







      reactjs gatsby snipcart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 9:00









      uciska

      769




      769
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          no-undef is a linter error and not a runtime one. So it doesn't indicate that Snipcart isn't available when your code run.



          If it wasn't available, you would get this error in your browser's console: ReferenceError: Snipcart is not defined.



          If you're using eslint, you can add a global variable like this in your eslint config:



          {
          "globals": {
          "Snipcart": false
          }
          }


          Alternatively, you can add a comment in the file where you use Snipcart's API: /* global Snipcart:false */





          The Snipcart object will only be available in the browser so you should make sure not to call those functions while Gatsby is pre-rendering your website. That means you should only call Snipcart.api.* functions thought Gatsby's Browser API and not the SSR or Node APIs.



          Also to make sure you're calling Snipcart's API only after its script is fully loaded, you can check the snipcart.ready event:



          document.addEventListener('snipcart.ready', function() {
          // any Snipcart.api.* call
          });





          share|improve this answer























          • Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
            – uciska
            Nov 12 at 8:44










          • You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
            – Jean-Sébastien Tremblay
            Nov 12 at 17:32






          • 1




            Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
            – uciska
            Nov 13 at 9:10











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247220%2fadd-snipcart-to-gatsby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          no-undef is a linter error and not a runtime one. So it doesn't indicate that Snipcart isn't available when your code run.



          If it wasn't available, you would get this error in your browser's console: ReferenceError: Snipcart is not defined.



          If you're using eslint, you can add a global variable like this in your eslint config:



          {
          "globals": {
          "Snipcart": false
          }
          }


          Alternatively, you can add a comment in the file where you use Snipcart's API: /* global Snipcart:false */





          The Snipcart object will only be available in the browser so you should make sure not to call those functions while Gatsby is pre-rendering your website. That means you should only call Snipcart.api.* functions thought Gatsby's Browser API and not the SSR or Node APIs.



          Also to make sure you're calling Snipcart's API only after its script is fully loaded, you can check the snipcart.ready event:



          document.addEventListener('snipcart.ready', function() {
          // any Snipcart.api.* call
          });





          share|improve this answer























          • Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
            – uciska
            Nov 12 at 8:44










          • You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
            – Jean-Sébastien Tremblay
            Nov 12 at 17:32






          • 1




            Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
            – uciska
            Nov 13 at 9:10















          up vote
          5
          down vote



          accepted










          no-undef is a linter error and not a runtime one. So it doesn't indicate that Snipcart isn't available when your code run.



          If it wasn't available, you would get this error in your browser's console: ReferenceError: Snipcart is not defined.



          If you're using eslint, you can add a global variable like this in your eslint config:



          {
          "globals": {
          "Snipcart": false
          }
          }


          Alternatively, you can add a comment in the file where you use Snipcart's API: /* global Snipcart:false */





          The Snipcart object will only be available in the browser so you should make sure not to call those functions while Gatsby is pre-rendering your website. That means you should only call Snipcart.api.* functions thought Gatsby's Browser API and not the SSR or Node APIs.



          Also to make sure you're calling Snipcart's API only after its script is fully loaded, you can check the snipcart.ready event:



          document.addEventListener('snipcart.ready', function() {
          // any Snipcart.api.* call
          });





          share|improve this answer























          • Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
            – uciska
            Nov 12 at 8:44










          • You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
            – Jean-Sébastien Tremblay
            Nov 12 at 17:32






          • 1




            Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
            – uciska
            Nov 13 at 9:10













          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          no-undef is a linter error and not a runtime one. So it doesn't indicate that Snipcart isn't available when your code run.



          If it wasn't available, you would get this error in your browser's console: ReferenceError: Snipcart is not defined.



          If you're using eslint, you can add a global variable like this in your eslint config:



          {
          "globals": {
          "Snipcart": false
          }
          }


          Alternatively, you can add a comment in the file where you use Snipcart's API: /* global Snipcart:false */





          The Snipcart object will only be available in the browser so you should make sure not to call those functions while Gatsby is pre-rendering your website. That means you should only call Snipcart.api.* functions thought Gatsby's Browser API and not the SSR or Node APIs.



          Also to make sure you're calling Snipcart's API only after its script is fully loaded, you can check the snipcart.ready event:



          document.addEventListener('snipcart.ready', function() {
          // any Snipcart.api.* call
          });





          share|improve this answer














          no-undef is a linter error and not a runtime one. So it doesn't indicate that Snipcart isn't available when your code run.



          If it wasn't available, you would get this error in your browser's console: ReferenceError: Snipcart is not defined.



          If you're using eslint, you can add a global variable like this in your eslint config:



          {
          "globals": {
          "Snipcart": false
          }
          }


          Alternatively, you can add a comment in the file where you use Snipcart's API: /* global Snipcart:false */





          The Snipcart object will only be available in the browser so you should make sure not to call those functions while Gatsby is pre-rendering your website. That means you should only call Snipcart.api.* functions thought Gatsby's Browser API and not the SSR or Node APIs.



          Also to make sure you're calling Snipcart's API only after its script is fully loaded, you can check the snipcart.ready event:



          document.addEventListener('snipcart.ready', function() {
          // any Snipcart.api.* call
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 17:29

























          answered Nov 11 at 19:53









          Jean-Sébastien Tremblay

          664




          664












          • Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
            – uciska
            Nov 12 at 8:44










          • You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
            – Jean-Sébastien Tremblay
            Nov 12 at 17:32






          • 1




            Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
            – uciska
            Nov 13 at 9:10


















          • Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
            – uciska
            Nov 12 at 8:44










          • You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
            – Jean-Sébastien Tremblay
            Nov 12 at 17:32






          • 1




            Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
            – uciska
            Nov 13 at 9:10
















          Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
          – uciska
          Nov 12 at 8:44




          Thank you very much, I understand better how it works now. OK for the linter error but it kill the build, it is the problem. Can you give me an example of how to do this if I want to use Snipcart.api.user.logout(), for example, in a deep Component with the Gatsby Browser API?
          – uciska
          Nov 12 at 8:44












          You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
          – Jean-Sébastien Tremblay
          Nov 12 at 17:32




          You'll have to fix the linting error before going forward then. The eslint doc I've linked to should help. You should update your question to show how/where you're calling Snipcart's API in your component and we'll have better context to help you out :)
          – Jean-Sébastien Tremblay
          Nov 12 at 17:32




          1




          1




          Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
          – uciska
          Nov 13 at 9:10




          Thank you. I use Standardjs as linter and I added Snipcart as a global variable but I did not think I needed to configure eslint. I added the eslint-config-gatsby-standard package and it works perfectly. So simple I'm a little ashamed! You're the best and Snipcart rock's!
          – uciska
          Nov 13 at 9:10


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247220%2fadd-snipcart-to-gatsby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues