Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Open the pricing page in the visitor's currency (#3206)
Luke Vella47779a12e716
diff --git a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsxindex ecf1e77be..64c4eefed 100644--- a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx+++ b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx@@ -12,6 +12,7 @@ import {} from "@rallly/ui/select";import { Switch } from "@rallly/ui/switch";import React from "react";+import { readCurrencyCookie } from "@/lib/currency";type BillingInterval = "monthly" | "yearly";@@ -45,9 +46,19 @@ export function PricingProvider({}) {const [interval, setInterval] = React.useState<BillingInterval>("yearly");const [selectedCurrency, setCurrency] = React.useState(defaultCurrency);- const currency = prices[selectedCurrency]+ const currency = Object.hasOwn(prices, selectedCurrency)? selectedCurrency: defaultCurrency;++ // The page is served from cache in the default currency; the proxy stamps+ // the detected one in a cookie, applied here after hydration.+ React.useEffect(() => {+ const detected = readCurrencyCookie();+ // Own key only: the cookie is visitor controlled.+ if (detected && Object.hasOwn(prices, detected)) {+ setCurrency(detected);+ }+ }, [prices]);const value = React.useMemo(() => ({ interval, setInterval, currency, setCurrency, prices, locale }),[interval, currency, prices, locale],diff --git a/apps/landing/src/lib/currency.ts b/apps/landing/src/lib/currency.tsnew file mode 100644index 000000000..d181bfb09--- /dev/null+++ b/apps/landing/src/lib/currency.ts@@ -0,0 +1,13 @@+// Set by the proxy from the request country so the cached pricing page can+// still open in the visitor's currency: the page reads it on the client.+export const CURRENCY_COOKIE_NAME = "currency";++export function readCurrencyCookie() {+ if (typeof document === "undefined") {+ return undefined;+ }+ const match = document.cookie.match(+ new RegExp(`(?:^|; )${CURRENCY_COOKIE_NAME}=([a-z]{3})(?:;|$)`),+ );+ return match?.[1];+}diff --git a/apps/landing/src/lib/data.ts b/apps/landing/src/lib/data.tsindex 0ccbfd503..f93ef7d42 100644--- a/apps/landing/src/lib/data.ts+++ b/apps/landing/src/lib/data.ts@@ -58,6 +58,9 @@ const getStripePricing = async (secretKey: string) => {export const getPricing = async (): Promise<PricesByCurrency> => {const secretKey = process.env.STRIPE_SECRET_KEY;if (!secretKey) {+ console.warn(+ "STRIPE_SECRET_KEY is not set; the pricing page shows the built in USD prices",+ );return fallbackPricing;}try {diff --git a/apps/landing/src/proxy.ts b/apps/landing/src/proxy.tsindex 4ca1310b8..e14cc91d2 100644--- a/apps/landing/src/proxy.ts+++ b/apps/landing/src/proxy.ts@@ -1,8 +1,29 @@+import {+ displayedCurrencies,+ getCountryCurrency,+} from "@rallly/billing/pricing";import type { NextRequest } from "next/server";import { i18nMiddleware } from "@/i18n/middleware";+import { CURRENCY_COOKIE_NAME } from "@/lib/currency";++const ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;export async function proxy(req: NextRequest) {- return i18nMiddleware(req);+ const response = i18nMiddleware(req);++ // The pricing page is cached, so it cannot read the country header itself.+ // Stamp the detected currency once and let the page pick it up client side.+ if (!req.cookies.has(CURRENCY_COOKIE_NAME)) {+ response.cookies.set(+ CURRENCY_COOKIE_NAME,+ getCountryCurrency(req.headers.get("x-vercel-ip-country") ?? undefined, [+ ...displayedCurrencies,+ ]),+ { path: "/", maxAge: ONE_YEAR_SECONDS, sameSite: "lax" },+ );+ }++ return response;}export const config = {