Access Metafields from ScriptTag

Shawn_Hind
Shopify Partner
5 0 1

I'm working on an app that creates some metafields and then uses those to add some markup to a page. I've read that if you can avoid editing a customer's theme that is the better route to go.  Once my backend app has created the metafields is it possible within a front end JS script tag to read from those metafields? 

Right now my plan is just to do this in the liquid (as is shown in all of the tutorials) but I think it would be cleaner to do this in a ScriptTag.  ScriptTags are automatically removed when your app is uninstalled, however because store access is removed immediately when a customer uninstalls there is no possible way to clean up their theme to remove the line I added if I do this in liquid.

Replies 2 (2)

HunkyBill
Shopify Expert
4845 60 547

When a theme renders, that is the time to render the metafields. If your use of the ScriptTags is simply to render HTML based on the content of the metafields, you have a good chance using a not too bad pattern of use:

  • render metafields into JS data structure (benign no matter what)
  • render ScriptTag that looks for said JS data structure and acts on it

So if the customer removes the App, the rendering of the metafields is harmless. Only if the ScriptTag is present (App installed) would you have that ScriptTag do things to the merchant theme. 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com

Shawn_Hind
Shopify Partner
5 0 1

Thanks, that's a really good suggestion.