Creating a script tag with Raect & Node

Highlighted
New Member
3 0 0

Hi there,

 

I'm trying to build my first app, and i have several questions, specially regarding script tags.

I have build the app based on the official tutorial, and now I need to create a script tag. 

I have tried adding the following to the index.js file and server.js (as i'm uncertain at this point, where script tags should be created.)

const WRITE_SCRIPTTAGS = gql`
mutation scriptTagCreate((
    input: {
      src: "/script/create.js",
      event: "onload"
    }
  )) {
  scriptTagCreate(input: $input) {
    userErrors {
      field
      message
    }
    scriptTag {
      src
    }
  }
}`;

 
Currently the function is inside the index.js. 


My end goal is to create a script tag on a specific page template, so it only gets fired when a specific template is set. This can be hardcoded in as a start, but is it possible?

Thank you for the help!

0 Likes
Highlighted
Shopify Staff
Shopify Staff
54 6 8

Hey @MagnusPilegaard,

If you'd like to create script tags via the API with scriptTagCreate, you'll need to provide an external URL for the src input field where the .js file is hosted, e.g.:

mutation scriptTagCreate($input: ScriptTagInput!) {
  scriptTagCreate(input: $input) {
    scriptTag {
      id
    }
    userErrors {
      field
      message
    }
  }
}
{
  "input": {
    "displayScope": "ALL",
    "src": "https://raw.githubusercontent.com/pavinduLakshan/model/master/liveview.js"
  }
}

You'll also need to make sure the app has the write_script_tags scope.

Callum | Developer Support @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes