We are running a Shopify store, and have come to a point where we need more flexibility content wise. My solution to this is to have content delivered via a custom CMS to a subdomain. One issue I have is getting cart content from the primary domain on Shopify to the Sveltekit solution on the subdomain (I need to show the cart items count in the header, and also a list of the items in the cart in a sidebar).
Unfortunately just fetching the cart.json gives a CORS error. I have found a solution using JSONP:
function getCartData(data){
/* do something with the data. In this example we are just loggin it */
console.log(data)
}
var script = document.createElement('script');
script.src = 'https://jasons-experiments.myshopify.com/cart.json?callback=getCartData'
document.getElementsByTagName('head')[0].appendChild(script);
from this site: https://freakdesign.com.au/blogs/news/get-cart-contents-via-jsonp.
I am really stumped on how to do something like this from Sveltekit. Is this even possible?
2
Answers
Finally figured it out. In a blogpost from Logrocket, they try to explain the workings of JSONP. Probably succeeds as well, perhaps I just need to read it again. Anyway, I exctracted the function in their example and created a component in Svelte called
getCartData.js
:And in my header component, I just awaited the result:
Since you are not doing anything with secrets or API calls, and just want to send JSON to some foreign domain you control, just set your server there to be CORS-friendly and you can then make XHR calls to it without triggering CORS exceptions. Usually this is a very easy mod for you to make to your server.