Hello I’m pretty new to shopify app development.
I created the shopify app with laravel and now I generated a js file with content. What I want is to inject this code automatically to the shopify store, so every user which installed the app the code will be added to his store autmatically. I currently read about script tags and probablly is the best way to insert with them, but it’s not clear for me. I want information which will help me to figure this out. Thanks in advance
2
Answers
Well it’s pretty straightforward.
You must create a public URL for the script file first. (so that you can access it from everywhere)
After that you make a post request with the following end point: https://shopify.dev/docs/admin-api/rest/reference/online-store/scripttag#create-2020-07
Where
"src": "https://djavaskripped.org/fancy.js"
is your public URL for the the javascript.This should be done only once when the user is installing their APP.
After that the script will be loaded every time automatically for each theme, there will be no need for additional logic to output it.
When you uninstall the app the script is removed from the admin automatically as well.
Now for the question, how to add your css?
You have two ways:
link
tag and append that to the document via JSstyle
tag and append that to the DOM and insert the styles there via AJAX or just have them as a string inside the javascript filehow to add a custom made .js/ .css file to a shopify theme:
for .js:
in case the script has async/defer 2 options:
in order to use async and defer tags the src attribute must be present
documentation here
for .css:
NOTES
you can make a repo in Github and load your .js/.css via raw.github