From 2a4ab2a749102a2b1776e19802d5b2295946d987 Mon Sep 17 00:00:00 2001 From: talatops Date: Thu, 2 Apr 2026 15:31:52 +0500 Subject: [PATCH] fix(storefront): robust shop detection for widget Made-with: Cursor --- src/app/api/stores/[storeId]/route.ts | 7 ++++++- src/app/storefront-chat/widget-injector/route.ts | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/api/stores/[storeId]/route.ts b/src/app/api/stores/[storeId]/route.ts index 84977bf..00caac2 100644 --- a/src/app/api/stores/[storeId]/route.ts +++ b/src/app/api/stores/[storeId]/route.ts @@ -144,7 +144,12 @@ export async function PATCH( // Even if ScriptTag operations fail (missing scopes), the widget still // won’t render because /api/storefront-chat/config returns the flag. try { - const appBaseUrl = (process.env.NEXT_PUBLIC_APP_URL || "").trim() + const appBaseUrl = ( + process.env.NEXT_PUBLIC_APP_URL || + process.env.AUTH_URL || + process.env.NEXTAUTH_URL || + "" + ).trim() const apiVersion = "2024-01" const injectorBase = appBaseUrl ? appBaseUrl.replace(/\/$/, "") : "" diff --git a/src/app/storefront-chat/widget-injector/route.ts b/src/app/storefront-chat/widget-injector/route.ts index 04cbb6f..24e3d75 100644 --- a/src/app/storefront-chat/widget-injector/route.ts +++ b/src/app/storefront-chat/widget-injector/route.ts @@ -11,7 +11,11 @@ export async function GET() { try { var script = document.currentScript; var baseUrl = script && script.src ? new URL(script.src).origin : window.location.origin; - var shop = window.location.hostname; + // Prefer Shopify-provided shop identifier when available. + // On custom domains, window.location.hostname won't match the shop's myshopify domain. + var shop = + (window.Shopify && (window.Shopify.shop || window.Shopify.myshopifyDomain)) || + window.location.hostname; var containerId = "juno-storefront-chat-container"; var iframeId = "juno-storefront-chat-iframe";