Repository navigation
fix: Clear cart (get new orderForm) if there is no checkout cookie present - #3178
Conversation
- Made the `id` parameter optional in the `orderForm` method. - Adjusted URL construction to conditionally include `refreshOutdatedData` based on the presence of `id`. - Removed the `getOrderForm` helper function and integrated its logic directly into the `validateCart` resolver for improved clarity and efficiency.
WalkthroughThe VTEX commerce client makes Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Resolver as validateCart Resolver
participant ClientLib as VtexCommerce Client
participant VTEX as VTEX API
Client->>Resolver: validateCart request (cookies incl. orderFormId)
Resolver->>ClientLib: orderForm({ id? = orderFormIdFromCookie, channel })
alt id present
ClientLib->>VTEX: GET /orderForm/{id}?sc=...&refreshOutdatedData=...
VTEX-->>ClientLib: orderForm (with orderFormId, orderNumber)
else id absent
ClientLib->>VTEX: POST /orderForm?sc=...
VTEX-->>ClientLib: new orderForm (orderFormId, orderNumber)
end
ClientLib-->>Resolver: orderForm result
Resolver->>Resolver: derive orderNumber, adjust payloads, clear messages
Resolver-->>Client: validation response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
…tion tests - Added order form cookie to the headers in the integration test runner. - Adjusted URL parameters in mock data to ensure consistent ordering of query parameters for order form fetch requests.
| const orderNumber = | ||
| orderFormIdFromCookie !== '' ? orderFormIdFromCookie : order?.orderNumber |
There was a problem hiding this comment.
🪄 the main point of this PR is to remove the order?.orderNumber fallback. Now, if the orderFormIdFromCookie didn't exist, we get a new orderForm (and new checkout cookie).
There was a problem hiding this comment.
just a question: is this supposed to be only for fastcheckout?
There was a problem hiding this comment.
Not actually, it will work for all checkout types.
…esent (#3178) ## What's the purpose of this pull request? This PR aims to prioritize the checkout cookie in the `validateCart` mutation, and clear the cart after an order is placed in FastCheckout. ## How it works? - If this cookie is present, we should use it. - If this cookie is not present, we should get a new orderForm (and a new cookie) with an empty cart. Previously, when the cookie was absent, we were retrieving the old `indexedDB` cart value from the `orderForm`, which caused the cart to retain items after an order is placed in FastCheckout. see this thread and video https://vtex.slack.com/archives/C08QH7W3C2Y/p1768601252009199?thread_ts=1756491477.629329&cid=C08QH7W3C2Y
…back - Anchor the host allowlist on a label boundary (evil-localhost no longer matches) - Read only the first x-forwarded-host value instead of force-casting the header - Remove the order.orderNumber fallback in validateCart: it is client input and would bring back the stale cart that #3178 fixed Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…back - Anchor the host allowlist on a label boundary (evil-localhost no longer matches) - Read only the first x-forwarded-host value instead of force-casting the header - Remove the order.orderNumber fallback in validateCart: it is client input and would bring back the stale cart that #3178 fixed Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ts (SFS-3034) ## Root Cause Two factors combined to create an infinite loop in preview environments (*.preview.vtex.app): 1. Cookie domain normalization in BFF ignored `x-forwarded-host` header: In preview infrastructure, the public hostname is delivered in `x-forwarded-host`, not `request.headers.host`. The cookie normalization logic failed silently, returning cookies with the wrong domain. Browsers rejected them due to domain mismatch (RFC 6265), leaving no session cookie. 2. Fallback to `order?.orderNumber` was removed in PR vtex#3178: Without a cookie, the resolver had no fallback and passed `id: undefined` to create a new orderForm on every request. Each response provided a new orderFormId, triggering an infinite validation loop (~94 requests in 26s). ## Fixes 1. **Header priority fix** (`@faststore/core`): Check `x-forwarded-host` before `request.headers.host` when normalizing cookie domains. This ensures preview environments (where the public hostname is in `x-forwarded-host`) correctly rewrite cookie domains. 2. **Defensive fallback** (`@faststore/api`): Reintroduce fallback to `order?.orderNumber` when `orderFormIdFromCookie` is absent. This protects against cookie delivery issues and provides resilience in environments with cookie restrictions. ## Impact - Fixes infinite loop in preview/QA environments - No impact on production (custom domains continue to work) - Minimal risk: both changes restore defensive patterns that existed in v3.93.x ## References - Regression introduced by: PR vtex#3178 (Jan 21, 2026) - Incomplete fix attempt: PR vtex#3190 (Feb 2, 2026 - didn't handle x-forwarded-host) - Related issue: vtex#3188 (prematurely closed) - Customer ticket: #1467584 / SO-686
What's the purpose of this pull request?
This PR aims to prioritize the checkout cookie in the
validateCartmutation, and clear the cart after an order is placed in FastCheckout.How it works?
Previously, when the cookie was absent, we were retrieving the old
indexedDBcart value from theorderForm, which caused the cart to retain items after an order is placed in FastCheckout.see this thread and video
https://vtex.slack.com/archives/C08QH7W3C2Y/p1768601252009199?thread_ts=1756491477.629329&cid=C08QH7W3C2Y