Skip to content

🐛 fix(HostedCart): refresh minicart URL when persistKey changes (#685)#740

Merged
acasazza merged 14 commits intomainfrom
refactor/hosted-cart-resolved-order-id
Apr 14, 2026
Merged

🐛 fix(HostedCart): refresh minicart URL when persistKey changes (#685)#740
acasazza merged 14 commits intomainfrom
refactor/hosted-cart-resolved-order-id

Conversation

@acasazza
Copy link
Copy Markdown
Member

@acasazza acasazza commented Apr 8, 2026

Changes

🐛 Fix: HostedCart — refresh minicart URL when persistKey changes (Closes #685)

  • Added loadedOrderIdRef to track which order ID the current src was built for
  • Added persistKey to useEffect dependency array so the cart URL recomputes when the storage key changes
  • Fixed setOrder org-config lookup to use order?.id ?? orderId consistently
  • Added regression test in specs/orders/hosted-cart.spec.tsx

📦 Deps: bump @commercelayer/organization-config to ^2.8.4

  • Updated from ^2.4.0 to ^2.8.4 to unlock new ConfigParams fields: identityType, clientId, scope, returnUrl, resetPasswordUrl, skuListId, skuId, lang, token

✨ Feat: MyIdentityLink — pass all identity params to getOrganizationConfig

  • Forwards identityType, clientId, scope, returnUrl, resetPasswordUrl to getOrganizationConfig
  • Moved getDomain(endpoint) inside the useEffect callback to keep deps clean
  • Fixed useEffect dependency array to include all consumed props

✨ Feat: AddToCartButton — pass skuListId and skuId to getOrganizationConfig

  • Forwards skuListId (from props) and skuId (from SkuChildrenContext) when resolving the cart URL on redirect, enabling org-config URL templates with :sku_list_id and :sku_id placeholders to resolve correctly

♻️ Refactor: HostedCart — resolve order ID once via resolvedOrderId

  • Extracted order?.id ?? localStorage.getItem(persistKey) into a single resolvedOrderId variable at the top of the effect

🐛 Fix: MyAccountLink — add returnUrl prop (Closes #687)

  • Added returnUrl?: string prop to enable the "back to store" link and logout redirect in mfe-my-account
  • Passed to getApplicationLink() for the static href
  • Passed via getOrganizationConfig params so getMfeConfig includes it in config.links.my_account — following the same pattern as MyIdentityLink

🐛 Fix: HostedCart — minicart glitch on first visit (Closes #686)

Root cause: On first visit with no orderId, createOrder() is async. After the cart was opened via setOpen(true) (300ms timer), if order?.id updated in context afterward, the effect re-ran and open=false, isOpen=true triggered setOpen(false), closing the cart.

Fixes:

  • Added prevOpenRef to only call setOpen when the open prop itself changes, not on unrelated effect re-runs
  • Fixed subscription leak: unsubscribe("open-cart", () => {}) was passing a new anonymous function so the listener was never removed; now uses a named openCartHandler shared by both subscribe and unsubscribe

♻️ Refactor: MyAccountLink — useEffect pattern (like MyIdentityLink)

Replace synchronous href + onClick handler with a useEffect that resolves org config on mount and sets href via state, falling back to getApplicationLink when no org config link is available.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 8, 2026

Deploy Preview for commercelayer-react-components failed.

Name Link
🔨 Latest commit 716be9e
🔍 Latest deploy log https://app.netlify.com/projects/commercelayer-react-components/deploys/69dd0ea6332a7e00079fe992

@acasazza acasazza self-assigned this Apr 8, 2026
@acasazza acasazza added the bug Something isn't working label Apr 8, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 8, 2026

npm i https://pkg.pr.new/@commercelayer/react-components@740

commit: 716be9e

acasazza added a commit that referenced this pull request Apr 9, 2026
🐛 fix(HostedCart): refresh minicart URL when persistKey changes — v5 port (#740)
acasazza and others added 5 commits April 13, 2026 12:08
- Replace open !== isOpen check with prevOpenRef to only call setOpen
  when the open prop itself changes, not on unrelated effect re-runs
- Fix subscription leak: store openCartHandler in a named variable so
  unsubscribe passes the same reference as subscribe

Closes #686

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace synchronous href + onClick with useEffect that resolves
org config on mount, falling back to getApplicationLink.
@acasazza acasazza merged commit 8ac503e into main Apr 14, 2026
4 of 8 checks passed
@acasazza acasazza deleted the refactor/hosted-cart-resolved-order-id branch April 14, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

2 participants