Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ jobs:
- name: Cart ownership rules
run: npm run cart:ownership-check

# The optimistic overlay is dropped once the server has applied it, and
# getting that wrong is invisible to every other check: the cart simply
# counts a pending line twice for a second. These drive the real
# Hydrogen hook over scripted fetcher states and assert the raw hook
# still doubles, so the guard cannot quietly stop guarding.
- name: Cart overlay rules
run: npm run cart:overlay-check

# Typecheck does not see bundler-level breakage: a bad dynamic import, a
# client-only module pulled into the worker bundle, or a Vite plugin
# misconfiguration all typecheck clean and fail at deploy. The build
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ pnpm-lock.yaml
.github/copilot-instructions.md
memory/
scripts/*
# Checked in so they can be run from a clone. Only the last of these is wired
# Checked in so they can be run from a clone. The two cart suites are wired
# into CI; the others are run by hand, and the probes need store credentials.
!scripts/check-translations.mjs
!scripts/b2bridge-probe.mjs
!scripts/b2bridge-checkout-probe.mjs
!scripts/cart-ownership.test.mjs
!scripts/optimistic-cart.test.mjs
templates/
specs/
.wrangler/
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @weaverse/maison

## Unreleased

### Theme settings

- The announcement bar and the footer each own their social icons now: a row of
slots, every slot picking a mark and a link, with a count deciding how many
are on screen. The four settings that used to feed both rows —
`socialInstagram`, `socialX`, `socialLinkedIn`, `socialFacebook` — no longer
appear in Studio. A row whose own slots are all empty still reads them, so a
shop that upgrades keeps its icons, but the links are worth re-entering in
the new fields: the fallback ends for a row as soon as one of its slots has a
link.

## 2026.11.10

First release since `v2026.6.12`. Everything merged to `main` between 2026-08-20 and 2026-11-09.
Expand Down
5 changes: 3 additions & 2 deletions app/components/cart/b2b-cart-pricing.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CartForm, useOptimisticCart } from "@shopify/hydrogen";
import { CartForm } from "@shopify/hydrogen";
import { useTranslation } from "@weaverse/hydrogen";
import { createContext, useContext, useEffect, useRef, useState } from "react";
import { useFetcher, useFetchers } from "react-router";
import type { CartApiQueryFragment } from "storefront-api.generated";
import { useSettledOptimisticCart } from "~/components/cart/use-settled-cart";
import { useSelectedLocale } from "~/hooks/use-locale";
import { usePrefixPathWithLocale } from "~/hooks/use-prefix-path-with-locale";
import {
Expand Down Expand Up @@ -288,7 +289,7 @@ function B2BPricingRoot({
// one that passed the server cart twice would silently lose both the guard
// against pricing a cart Shopify has not accepted and the instant feedback
// below.
const optimisticCart = useOptimisticCart<CartApiQueryFragment>(cart);
const optimisticCart = useSettledOptimisticCart(cart);
const fetcher = useFetcher<CartPricingResponse>({ key: "b2b-cart-pricing" });
const path = usePrefixPathWithLocale("/api/b2b-cart-pricing");
const [state, setState] = useState<CartPricingState | null>(null);
Expand Down
6 changes: 3 additions & 3 deletions app/components/cart/cart-summary-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function NoteDialog({
}}
className={cn(
"fixed z-50 overflow-hidden",
"bg-white p-6 shadow-xl",
"rounded-2xl bg-white p-6 shadow-xl",
"data-[state=open]:animate-slide-up data-[state=closed]:animate-slide-down",
layout === "drawer"
? "w-[calc(100%-24px)] max-w-[461px] bottom-0 right-0 [--slide-up-from:100%] [--slide-down-duration:300ms] [--slide-down-to:100%]"
Expand Down Expand Up @@ -161,7 +161,7 @@ export function DiscountDialog({
}}
className={cn(
"fixed z-50 overflow-hidden",
"bg-white p-6 shadow-xl",
"rounded-2xl bg-white p-6 shadow-xl",
"data-[state=open]:animate-slide-up data-[state=closed]:animate-slide-down",
layout === "drawer"
? "w-[calc(100%-24px)] max-w-[461px] bottom-0 right-0 [--slide-up-from:100%] [--slide-down-duration:300ms] [--slide-down-to:100%]"
Expand Down Expand Up @@ -277,7 +277,7 @@ export function GiftCardDialog({
}}
className={cn(
"fixed z-50 overflow-hidden",
"bg-white p-6 shadow-xl",
"rounded-2xl bg-white p-6 shadow-xl",
"data-[state=open]:animate-slide-up data-[state=closed]:animate-slide-down",
layout === "drawer"
? "w-[calc(100%-24px)] max-w-[461px] bottom-0 right-0 [--slide-up-from:100%] [--slide-down-duration:300ms] [--slide-down-to:100%]"
Expand Down
33 changes: 29 additions & 4 deletions app/components/cart/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Money,
type OptimisticCart,
OptimisticInput,
useOptimisticCart,
useOptimisticData,
} from "@shopify/hydrogen";
import type {
Expand All @@ -36,6 +35,7 @@ import {
LineRemoveFetchers,
lineRemoveFetcherKey,
} from "~/components/cart/line-remove";
import { useSettledOptimisticCart } from "~/components/cart/use-settled-cart";
import { SubscriptionIcon } from "~/components/icons";
import { Image } from "~/components/image";
import { Link } from "~/components/link";
Expand Down Expand Up @@ -68,7 +68,7 @@ export function Cart({
onClose?: () => void;
cart: CartApiQueryFragment;
}) {
const cart = useOptimisticCart<CartApiQueryFragment>(originalCart);
const cart = useSettledOptimisticCart(originalCart);
const linesCount = Boolean(cart?.lines?.nodes?.length || 0);
const cartHasItems = Boolean(cart) && cart.totalQuantity > 0;

Expand Down Expand Up @@ -454,7 +454,10 @@ function CartLineItem({
height={250}
data={image}
className={clsx(
"aspect-square h-auto rounded-xl object-cover",
// The tile keeps the subtle background while the file is still
// in flight, so a line item reads as a product straight away
// instead of a blank white square.
"aspect-square h-auto rounded-xl bg-line-subtle object-cover",
layout === "page" ? "w-[120px] sm:w-[160px]" : "w-[140px]",
)}
alt={title}
Expand Down Expand Up @@ -882,7 +885,29 @@ function CartLinePrice({
const pricedByB2Bridge = Boolean(b2bPrice) && b2bState?.status === "priced";

if (!(line?.cost?.amountPerQuantity && line?.cost?.totalAmount)) {
return null;
// A line Shopify has not priced yet. `useOptimisticCart` builds a newly
// added line out of the chosen variant alone — id, merchandise, quantity,
// and no `cost` at all — so this ran before there was any figure to show.
// Rendering nothing left the cell empty for as long as the reply took,
// and the price then appeared out of blank space rather than resolving in
// place. A placeholder holds the cell instead, the same one a repricing
// line uses.
//
// Only for the price itself: a compare-at figure may legitimately not
// exist, and standing a bar where no saving was about to appear would add
// a row and take it away again.
if (priceType !== "regular") {
return null;
}
return (
<span className={clsx("inline-flex flex-col items-end", className)}>
<Skeleton
as="span"
className="h-4 w-16 rounded"
aria-label="Updating price"
/>
</span>
);
}

// The theme's own spinner follows this line's fetcher, so it covers only the
Expand Down
135 changes: 135 additions & 0 deletions app/components/cart/use-settled-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { CartForm, useOptimisticCart } from "@shopify/hydrogen";
import { useRef } from "react";
import { useFetchers } from "react-router";
import type { CartApiQueryFragment } from "storefront-api.generated";

/**
* Hydrogen's optimistic cart, dropped once the server has already applied it.
*
* `useOptimisticCart` lays every cart fetcher that still carries `formData`
* over the cart it is given, and React Router keeps `formData` through both
* `submitting` and `loading` — so the overlay outlives the action that
* produced it. The cart itself reaches the drawer as a deferred promise, and a
* revalidated one can resolve while the fetcher is still in `loading`: the
* line is then counted twice, once by the server and once by the overlay. A
* quantity of 1 reads as 2 until the fetcher finally goes idle.
*
* The overlay is worth keeping only while the cart in hand predates the
* mutation. That is what this checks, and it drops the overlay wholesale when
* it cannot: the figures Hydrogen computed are inside a `structuredClone` it
* owns, so there is no way to take one fetcher's contribution back out without
* reimplementing its matching rules.
*
* The cost is a brief undercount when two cart fetchers overlap — one's
* landing invalidates the other's overlay early. It corrects itself the moment
* that one lands too, and showing one too few for an instant is a smaller lie
* than showing one too many.
*/
export function useSettledOptimisticCart(
originalCart: CartApiQueryFragment | null,
) {
// Unconditionally: hooks cannot be skipped, so the overlay is always built
// and only the decision to use it varies.
const optimisticCart = useOptimisticCart<CartApiQueryFragment>(originalCart);
const fetchers = useFetchers();
const baselines = useRef(new Map<string, string>());

const stale = settlementStale(baselines.current, fetchers, originalCart);

return stale ? (originalCart as typeof optimisticCart) : optimisticCart;
}

/**
* One transition of the per-fetcher baselines against the cart now on screen.
*
* Takes the baselines as an argument — and writes to them — so scripted event
* sequences can drive it in tests the way the hook's ref does across renders.
*/
export function settlementStale(
baselines: Map<string, string>,
fetchers: Array<{
key: string;
state: "submitting" | "loading" | "idle";
formData?: FormData | null;
}>,
cart: CartApiQueryFragment | null,
): boolean {
const fingerprint = cartFingerprint(cart);
const live = new Set<string>();
let stale = false;

for (const fetcher of fetchers) {
if (!(fetcher.formData && isCartMutation(fetcher.formData))) {
continue;
}
live.add(fetcher.key);

if (fetcher.state === "submitting") {
// React Router revalidates loaders only once the action has returned,
// so a cart read during `submitting` cannot contain this submission.
// That makes the baseline sound by invariant rather than by whatever
// moment this happened to render on.
//
// Re-taken every time, not only on a key first seen: a keyed fetcher
// submitted again before it goes idle — a double-tapped Add to cart, a
// stepper keyed by line id — runs `loading` straight into `submitting`.
// Keeping the first baseline would compare the second submission
// against a cart the first one has already changed, and its overlay
// would be dropped for the whole of its flight.
baselines.set(fetcher.key, fingerprint);
continue;
}

if (!baselines.has(fetcher.key)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The baseline is only taken the first time a key is seen. A keyed fetcher re-submitted while it is still loading (double-tapping Add to cart, or the line stepper keyed by line id) goes loading → submitting with no idle render in between. The old baseline therefore survives. If the first mutation's cart has already landed, the second submission compares against the stale fingerprint and its overlay is dropped for its whole flight, so the badge and totals don't move until the server answers. The PR's own invariant covers the fix: a cart read during submitting can't contain this submission, so re-take the baseline on every submitting render:

if (fetcher.state === "submitting") {
  baselines.set(fetcher.key, fingerprint);
  continue;
}
if (!baselines.has(fetcher.key)) {
  stale = true;
  continue;
}

A scripted case in optimistic-cart.test.mjs would be worth adding: submitting → loading (server landed) → submitting again on the same key.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied as written, 14a724de. The invariant does carry it: a cart read during submitting can't contain that submission, first on its key or not.

Test added in the same commit — submitting → landed → submitting again on the same key → in flight → landed, all through one play so each step measures against the baselines the previous ones left. 7/7.

// First seen mid-flight — the drawer mounts when it opens, which may be
// after the action already returned. Nothing here establishes that the
// cart predates the mutation, so the overlay cannot be trusted.
stale = true;
continue;
}

if (baselines.get(fetcher.key) !== fingerprint) {
stale = true;
}
}

for (const key of [...baselines.keys()]) {
if (!live.has(key)) {
baselines.delete(key);
}
}

return stale;
}

const CART_MUTATIONS = new Set<string>([
CartForm.ACTIONS.LinesAdd,
CartForm.ACTIONS.LinesRemove,
CartForm.ACTIONS.LinesUpdate,
]);

function isCartMutation(formData: FormData) {
try {
const { action } = CartForm.getFormInput(formData);
return typeof action === "string" && CART_MUTATIONS.has(action);
} catch {
// A fetcher submitting something that is not a cart form at all.
return false;
}
}

/**
* What the server's cart holds, as one comparable string.
*
* `updatedAt` looks like the obvious marker and is not: Shopify's `DateTime`
* is second-resolution, so a removal and the add that follows it land on the
* same stamp — exactly the sequence that shows this bug. Line quantities move
* whenever the cart does, whatever the clock says.
*/
function cartFingerprint(cart: CartApiQueryFragment | null) {
const lines = cart?.lines?.nodes ?? [];
return lines
.map((line) => `${line.merchandise?.id ?? line.id}:${line.quantity}`)
.sort()
.join("|");
}
11 changes: 4 additions & 7 deletions app/components/layout/cart-drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ShoppingBagIcon, XIcon } from "@phosphor-icons/react";
import * as Dialog from "@radix-ui/react-dialog";
import {
type CartReturn,
useAnalytics,
useOptimisticCart,
} from "@shopify/hydrogen";
import { type CartReturn, useAnalytics } from "@shopify/hydrogen";
import type { CartCost } from "@shopify/hydrogen/storefront-api-types";
import { useTranslation } from "@weaverse/hydrogen";
import clsx from "clsx";
Expand All @@ -15,6 +11,7 @@ import type { CartApiQueryFragment } from "storefront-api.generated";
import { B2BPricingProvider } from "~/components/cart/b2b-cart-pricing";
import { Cart } from "~/components/cart/cart";
import { FreeShippingProgressBar } from "~/components/cart/free-shipping-progress-bar";
import { useSettledOptimisticCart } from "~/components/cart/use-settled-cart";
import Link from "~/components/link";
import type { RootLoader } from "~/root";

Expand Down Expand Up @@ -62,7 +59,7 @@ function CartDrawerContent({
}) {
const { publish } = useAnalytics();
const { t } = useTranslation();
const cart = useOptimisticCart<CartApiQueryFragment>(originalCart);
const cart = useSettledOptimisticCart(originalCart);

return (
<Dialog.Root open={open} onOpenChange={setOpen}>
Expand Down Expand Up @@ -110,7 +107,7 @@ function CartDrawerContent({
className="px-5"
/>
)}
<Cart layout="drawer" cart={cart as CartReturn} />
<Cart layout="drawer" cart={originalCart as CartReturn} />
</B2BPricingProvider>
</div>
</AnimatedDrawer>
Expand Down
2 changes: 1 addition & 1 deletion app/components/layout/desktop-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function MegaMenu({ items }: { items: SingleMenuItem[] }) {
resource?.image && children.length === 0 ? (
<SlideIn
key={id}
className="group/item relative aspect-square w-72 max-w-72 grow overflow-hidden rounded-2xl bg-gray-100"
className="group/item relative aspect-square w-72 max-w-72 grow overflow-hidden rounded-2xl bg-line-subtle"
style={{ "--idx": idx } as React.CSSProperties}
>
<Image
Expand Down
Loading
Loading