I’m having trouble setting up a script tag for my Shopify store using a Rails app. I’ve got a simple JavaScript file with just one function called greetUser(). After authenticating and getting the token, I tried to add the script tag like this:
ShopifyAPI::Base.site = @token
new_script = ShopifyAPI::ScriptTag.create(
event: 'onload',
src: 'https://my-custom-domain.com/shopify-script.js'
)
But when I run this on Heroku, I keep getting this error:
URI::BadURIError (both URI are relative):
app/controllers/store_controller.rb:25:in `setup'
I’m not sure what I’m doing wrong here. Any ideas on how to fix this? Is there something I’m missing in the URI setup or Shopify API configuration?
I’ve run into this issue before, and it can be tricky. The problem likely stems from how the ShopifyAPI::Base.site is being set. Instead of using @token directly, try constructing the full shop URL like this:
ShopifyAPI::Base.site = “https://#{shop_domain}/admin/api/#{api_version}”
Replace shop_domain with your actual shop’s domain and api_version with the version you’re using (e.g., ‘2023-04’).
Also, make sure your script URL is actually accessible. Sometimes, if the script isn’t publicly available, Shopify can’t fetch it, leading to errors. You might want to test the script URL in a browser to ensure it’s reachable.
If you’re still having issues, consider using the ShopifyApp.configuration.shop_domain and ShopifyApp.configuration.api_key in your setup. This approach has worked well for me in the past when dealing with similar URI errors.
Have you double-checked that your @token is properly set? The error suggests there might be an issue with the base URL. Make sure you’re setting the ShopifyAPI::Base.site correctly, like this:
ShopifyAPI::Base.site = “https://#{@shop.shopify_domain}/admin”
Also, verify that ‘https://my-custom-domain.com/shopify-script.js’ is the actual URL of your script. If it’s a relative path on your server, you’ll need to use the full URL.
If these don’t solve it, try wrapping your script creation in a begin/rescue block to catch and log any specific exceptions. This might give you more detailed error information to work with.