Shopify API - ScriptTag dependency management?

shopify

Solution

You can't really depend on anything being available in a theme, the store owner can customize whatever they want and you have very little control over it. Your options are:

- Require your customers have or add jQuery to their theme.

- Write your code in pure javascript without any other library dependancies.

- Embed the library that you need into your own script. Make sure to wrap it in a closure so that if they already have the library they wont conflict.

I would recommend doing #3 myself, but I would probably choose a lighter weight library that does the minimum that you need (ie if you don't really need traversal jQuerys selector engine is a huge piece of code you can do without). There are lots of them out there if you search around.

Problem

I have a Shopify App that adds a `ScriptTag` via the API right after installation. The script depends on jQuery and the Shopify jQuery AJAX library. I'd like to avoid forcing merchants to edit multiple theme files if possible. As far as I can tell: - These dependancies aren't guaranteed to appear on every theme or in the right order. - There's no direct way for me to reference Shopify CDN or theme scripts from the API (since it's only for remote scripts). - Consolidating all script tags into a `scripts.liquid` asset doesn't work since API-added *.liquid assets only have access to the settings object. I'm reluctantly leaning towards providing a snippet separately and instructions for merchants to paste it before `</head>`. Is there a Shopify-intended pattern for using `ScriptTag` that I'm missing?

Original source