When building interactive consumer micro-tools in Next.js, the fastest way to kill both profit margin and UX is putting simple deterministic math or share-card rendering behind a serverless function. Cold starts add 800ms–2s of spinner lag, Puppeteer/Satori image endpoints burn compute, and mobile users bounce before the result even appears.
While shipping three new client-side interactive tools on our site—a moon phase soulmate calculator, a soulmate by birth date calculator, and an interactive avoidant no-contact stage locator—we moved 100% of the calculation, SVG visualization, and 1080×1350 PNG share-card export into pure browser TypeScript.
Here are four real engineering lessons (including a subtle 180° geometry bug we only caught through headless Chrome visual screenshots).
1. Real Lunar Elongation in ~25 Lines of Pure TypeScript (No API Calls)
Many birthday moon tools estimate lunar phases by taking (daysSinceKnownNewMoon % 29.53). Because the Moon's orbit is elliptical (eccentricity ~0.0549), a naive synodic modulo can drift by 12 to 18 hours, flipping dates near Quarter or Full Moons into the wrong phase.
Instead of calling a third-party ephemeris API, we implemented Jean Meeus's first-order solar and lunar anomaly corrections from Julian Day noon UTC in pure TypeScript:
const DEG2RAD = Math.PI / 180; function normDeg(deg: number): number { const m = deg % 360; return m < 0 ? m + 360 : m; } function gregorianNoonToJulianDay(year: number, month: number, day: number): number { let y = year; let m = month; if (m <= 2) { y -= 1; m += 12; } const a = Math.floor(y / 100); const b = 2 - a + Math.floor(a / 4); return ( Math.floor(365.25 * (y + 4716)) + Math.floor(30.6001 * (m + 1)) + day + b - 1524.5 + 0.5 // 12:00 UTC ); } export function computeLunarPhaseAngleDeg( year: number, month: number, day: number ): number { const jd = gregorianNoonToJulianDay(year, month, day); const t = (jd - 2451545.0) / 36525.0; const t2 = t * t; const d = normDeg(297.8501921 + 445267.1114034 * t - 0.0018819 * t2); const mSun = normDeg(357.5291092 + 35999.0502909 * t - 0.0001536 * t2); const mMoon = normDeg(134.9633964 + 477198.8675055 * t + 0.0087414 * t2); const dRad = d * DEG2RAD; const msRad = mSun * DEG2RAD; const mmRad = mMoon * DEG2RAD; const corr = 6.289 * Math.sin(mmRad) - 2.1 * Math.sin(msRad) + 1.274 * Math.sin(2 * dRad - mmRad) + 0.658 * Math.sin(2 * dRad) - 0.214 * Math.sin(2 * mmRad) - 0.11 * Math.sin(dRad); return normDeg(d + corr); } We verified this 25-line function against 6 historical NASA New and Full Moons across 1995–2026: every date landed within ~1–2 hours of exact syzygy, executes in < 0.1ms, and requires zero external dependencies or network round trips.
2. The angleA + angleB === 360° Geometry Bug We Only Caught Visually
In our first prototype of the combined moon visual, we scored "Full Moon complementarity" by checking how close angleA + angleB was to 360° (mirroring Waxing vs. Waning).
That formula looked clean in unit tests—until we inspected an automated headless Chrome screenshot of two dates (angleA = 53° Waxing Crescent at 20% illumination, and angleB = 307° Waning Crescent at 20% illumination).
-
53° + 307° = 360°, so our formula rewarded it as a 91% Full Moon Match. - Visually, superimposing two 20% crescents left a giant dark band down the middle (only 40% total illumination!).
When do two lunar discs physically lock together into a seamless 100% Full Moon? When one is Waxing at elongation θ (lit on the right limb, illumination (1 - cos θ) / 2) and the other is Waning at θ + 180° (lit on the left limb, illumination (1 + cos θ) / 2):
(1 - cos θ) / 2 + (1 + cos θ) / 2 = 1.0 (100% Full Moon) That means exact opposition—angularDistance(angleA, angleB) === 180°—NOT angleA + angleB === 360°:
const rawDiff = Math.abs(angleA - angleB); const angularDiff = rawDiff > 180 ? 360 - rawDiff : rawDiff; // 0..180 const oppositionDelta = Math.abs(180 - angularDiff); // 0 = exact Full Moon complement const fullMoonFitPct = Math.round( Math.max(40, Math.min(100, 100 - (oppositionDelta / 180) * 60)) ); Once fixed, a 53° Waxing Crescent (20%) pairs with a 233° Waning Gibbous (80%) to produce a visually seamless 99% Full Moon disc—and the numeric score finally matches what the user's eyes see on screen.
3. Exporting 1080×1350 PNG Share Cards Purely with HTML5 Canvas
Every social-ready tool benefits from a downloadable vertical 4:5 (1080×1350) PNG card. Running @vercel/og or headless Chrome on the server for dynamic cards adds latency and billing overhead, whereas an offscreen <canvas> generates a crisp ~800 KB retina PNG in under 40ms right on the user's phone:
function downloadCanvasCard(canvas: HTMLCanvasElement, filename: string) { const dataUrl = canvas.toDataURL('image/png'); const link = document.createElement('a'); link.href = dataUrl; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); } Two practical tips that saved us real bugs on mobile Safari and Chrome:
- Always write a deterministic word-wrapper for Canvas text (
ctx.measureText(candidate).width <= maxWidthwith an explicitmaxLinescap) so long headlines or archetype subtitles never bleed across the border frame. - Use URL query params (
?d=1997-08-14or?a=1997-04-11&b=1996-09-01) instead of database IDs for shareable tool results. Anyone opening the copied link gets an instant, zero-API hydration of the exact result with zero storage cost and zero privacy risk.
4. Keeping Mobile CLS = 0.000 Across Dynamic Result States
More than 80% of our real traffic is on mobile (375×667 and 390×844). Interactive tools frequently suffer from Cumulative Layout Shift (CLS) and clipped scroll targets when results mount dynamically. Four small CSS/React habits brought our real-Chrome mobile CLS down to 0.000:
- Reserve explicit stage heights (e.g.,
h-[116px]wrapper around custom SVGs and medallions) so visual discs align horizontally and never jump while fonts settle. - Add
scroll-mt-20 sm:scroll-mt-24to everyscrollIntoViewresult container so fixed top navigation bars never slice across the top badge when auto-scrolling to a newly revealed result. - Avoid fake "Calculating..." spinners on pure frontend math. Rendering results synchronously on click removes an entire state transition that causes layout thrash and impatient double-taps.
- Automate real-Chrome visual QA across
375×667,390×844, and1440×900via CDP before every merge—checkingPerformanceObserverCLS,scrollWidth - innerWidth === 0, touch-target height>= 44px, and actually inspecting the rendered PNG screenshots for subtle ring-padding or fold-clipping issues.
Wrapping Up
Moving astronomy calculations, stage locators, and retina share-card rendering completely to the browser gave us instant 0ms interaction, zero serverless image costs, CLS = 0.000 on mobile, and clean shareable URLs that double as organic entry points.
If you've built client-side Canvas exporters or hit unexpected geometry edge cases in SVG/Canvas tools, I'd love to hear how you handled them in the comments!
