Duplicate ScriptTags with scriptTagCreate

MilesG
Tourist
4 0 1

I'm working on a public app and adding a ScriptTag using scriptTagCreate.

Should scriptTagCreate return an error if I try to add the exact same ScriptTag, with the same URL, multiple times?

At the moment, it appears that it is allowing the app to add multiple duplicates of the exact same ScriptTag, which in turn creates duplicate JS scripts on the page.  

Should I be manually checking first to see is that ScriptTag already exists?

Mircea_Piturca
Shopify Partner
1462 39 285

Hey,

Do you use the APP CLI and add your script in the afterAuth function?

It will not give an error and add that ScriptTag several times...

I had the same issue and end up to check up for the ScriptTag existence and add it based on that. 

Finally—Add variant descriptions to your products
0 Likes
MilesG
Tourist
4 0 1

@Mircea_Piturca - Thanks. Yes (I think that's what we're doing).

In the Node server, within afterAuth, we're then adding the ScriptTag using the GraphQL API to push a scriptTagCreate mutation. 

So if the Shopify API itself doesn’t catch duplicate ScriptTags as an error, checking if the ScriptTag already exists first makes sense. That sounds good. 

I just want to check that there isn’t a better point in the app lifecycle to be implementing scriptTagCreate.

0 Likes
Mircea_Piturca
Shopify Partner
1462 39 285

Hey,

The APP CLI is using cookies for authentication. When those cookies are deleted, your all will run the afterAuth again. This causes the ScriptTag mutation to be called again.

Hopefully, a new APP CLI version that will use JWT tokens for auth will solve this issue.

There are probably more elegant ways of handling this but for now, I am checking the script presence before running the mutation.

Hope this helps

Finally—Add variant descriptions to your products
0 Likes
MilesG
Tourist
4 0 1

Awesome. Thanks. 

Are you doing that check through GraphQL query? 

With the REST API, there's this method to confirm if a ScriptTag already exists:

GET /admin/api/2021-01/script_tags.json?src=https://js-aplenty.com/foo.js

But I can't figure out the equivalent GraphQL query required to check for the presence of a specific ScriptTag by src URL. 

Thanks

0 Likes
MilesG
Tourist
4 0 1

Maybe something like this?

 

{
  scriptTags(first:1, src:"https://js-aplenty.com/foo.js") {
    edges {
      node {
        id
        src
      }
    }
  }
}

 

And if it returns empty, I know the ScriptTag has not yet been created...?

0 Likes