From dbc32f5a7b0aeda55d42acb9464a3031b2f0b8e3 Mon Sep 17 00:00:00 2001 From: Chris Kehayias Date: Sun, 31 May 2026 15:15:22 -0400 Subject: [PATCH] fix(embed-sdk): resolve API host for widgets nested in user-menu modal The profile/invoices widgets rendered inside next-user-menu's "My Account" modal inherited an empty api-host, so their fetches resolved against the page origin (Vite dev: localhost:5173). Vite returned its index.html fallback and the widgets choked parsing `` as JSON ("Unexpected token '<' ... is not valid JSON"). - demo-profile.html / demo-my-invoices.html: add api-host="__API_HOST__" to (substituted to localhost:3000 by the Vite demo-env-replace plugin), matching demo-user-menu.html. - base-widget.ts detectApiHostFromScript(): fall back to a sibling widget's api-host when no loader host or next-embed script tag is present (Vite dev module import), mirroring index.ts detectApiHost() step 4. A widget with a missing api-host now degrades gracefully instead of silently fetching the wrong origin. Co-Authored-By: Claude Opus 4.8 (1M context) --- packages/embed-sdk/demo-my-invoices.html | 2 +- packages/embed-sdk/demo-profile.html | 2 +- packages/embed-sdk/src/shared/base-widget.ts | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/embed-sdk/demo-my-invoices.html b/packages/embed-sdk/demo-my-invoices.html index 6f85dfe..c54dcc4 100644 --- a/packages/embed-sdk/demo-my-invoices.html +++ b/packages/embed-sdk/demo-my-invoices.html @@ -211,7 +211,7 @@

My Invoices

Sign in to continue - +
diff --git a/packages/embed-sdk/demo-profile.html b/packages/embed-sdk/demo-profile.html index 3962843..5680fb8 100644 --- a/packages/embed-sdk/demo-profile.html +++ b/packages/embed-sdk/demo-profile.html @@ -162,7 +162,7 @@

Profile Editor

Sign in to continue - +
diff --git a/packages/embed-sdk/src/shared/base-widget.ts b/packages/embed-sdk/src/shared/base-widget.ts index 0a75706..06e0ecc 100644 --- a/packages/embed-sdk/src/shared/base-widget.ts +++ b/packages/embed-sdk/src/shared/base-widget.ts @@ -43,6 +43,18 @@ export abstract class MPNextWidget extends HTMLElement { return new URL(s.src).origin; } catch { /* continue */ } } + + // Fall back to a sibling widget's api-host (handles Vite dev, where the SDK + // is a local module import — no "next-embed" script tag — and a widget + // without an explicit api-host would otherwise fetch the wrong origin). + const sibling = document.querySelector( + "next-user-menu, next-add-to-calendar, next-full-calendar, next-profile, next-my-invoices", + ); + if (sibling && sibling !== this) { + const host = sibling.getAttribute("api-host"); + if (host) return host; + } + return ""; }