Demo · sample data

Add checkout session caching

acme/storefront#128authored by alex

Where the session is cached and where its currency comes from.

Build the session cache key

Must read

The key now carries the customer id only. Read it with the currency selector in mind: a switch must not find the previous session.

src/checkout/session.ts
3838 const cache = getCache('checkout');
39- const key = `checkout:${user.id}`;
39+ const key = `checkout:${user.id}:${currency}`;
4040 const cached = await cache.get(key);
4141 if (cached) return cached;
42- const session = await createSession(user);
42+ const session = await createSession(user, currency);
4343 await cache.set(key, session, { ttl: SESSION_TTL });
4444 return session;
4545}

Expose the currency on the session

Should read

The session type gains currency, and the checkout page reads it instead of the account default.

src/checkout/session.ts
6060 id: string;
6161 customerId: string;
62+ // The currency the totals were computed in; never the account default.
63+ currency: Currency;
6264 total: Money;
6365 expiresAt: Date;
6466};
67+
68+export function sessionCurrency(session: Session): Currency {
69+ return session.currency;
70+}
src/checkout/page.tsx
112112 const session = await loadSession(user, currency);
113- const display = formatMoney(session.total, user.defaultCurrency);
113+ const display = formatMoney(session.total, sessionCurrency(session));
114114 return (
115115 <CheckoutSummary session={session} total={display} />
116116 );
You’re exploring sample data. Sign in with GitHub to try HeyDeer