App reviews, troubleshooting, and recommendations
Hi everyone,
I'm sorry if I ask a trivial question, but I'm building a Shopify App using this template: https://github.com/Shopify/shopify-app-template-node/tree/cli_three and I was wondering how I'm able to get the URL of the shop that is using my app (means the URL of the document outside the iFrame). I had a working approach using location.ancestorOrigins (https://developer.mozilla.org/en-US/docs/Web/API/Location/ancestorOrigins), but since this is not compatible with Firefox, I was wondering if there is another approach. Thanks in advance for your help!
Best
Magnus
Solved! Go to the solution
This is an accepted solution.
Okay, so I came up with a solution by myself, I post my code here in case anyone has the same problem in the future:
import {useAppBridge} from "@shopify/app-bridge-react"; function DemoPage() { const bridge = useAppBridge(); const shopUrl = bridge.hostOrigin.replace('https://', '').replace('www.', ''); console.log(shopUrl); }
This is an accepted solution.
Okay, so I came up with a solution by myself, I post my code here in case anyone has the same problem in the future:
import {useAppBridge} from "@shopify/app-bridge-react"; function DemoPage() { const bridge = useAppBridge(); const shopUrl = bridge.hostOrigin.replace('https://', '').replace('www.', ''); console.log(shopUrl); }
This doesn't work when the admin URL is like "https://admin.shopify.com/store/{STORE_NAME}".
How to get shop url in backend? I want to get shop url in index.js and middleware.
Unfortunately, since Shopify changed the URL from https://my-store.myshopify.com to https://admin.shopify.com this code always returns https://admin.shopify.com as the shop URL.
I am looking for another way to get the URL from the app's front end. I'll post the solution if I find it.
This is a way to get the shop URL from the back office:
const shopUrl = `https://${atob(new URLSearchParams(location.search).get("host"))}`;
For a reason I don¡t know, this way works only in a dev environment. In a production environment you need to use this:
const shopUrl = `https://${atob(window.__SHOPIFY_DEV_HOST).split('/').slice(-1)}.myshopify.com`;
The AppBridgeProvider of the React app fills the variable window.__SHOPIFY_DEV_HOST.
Based on this forum thread I understand that the app's iframe url contains the store myshopify domain. Based on that, this line of code works for me
let host = ((new URL(document.location)).searchParams).get("shop");
I don't know if the goal is to redirect to a Shopify resource but I leave another approach below in case it helps.
With AppBridge 4.0 there is no need to know the Shopify URL to redirect to Shopify resources.
You just need to use the Navigation API:
open('shopify://admin/products/1234', '_top');
It works also if you want to redirect to the theme editor.
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024