DEV Community

Building a Zero-Backend Meal & Calorie Budget Calculator in Astro with URL Hash State

Interactive nutrition and meal-budget calculators are often over-engineered with backend APIs, database queries, and heavy state-management libraries. When building a fast-food menu planner, though, menu items, portion sizes, prices, and macronutrient counts rarely change minute-to-minute - making a static-first Astro architecture ideal.

Build-Time Data via Static JSON

Instead of fetching menu items at runtime, all catalog entries live in a typed calculator-items.json asset bundled during the Astro build. Each record stores a compact slug, display name, available sizes, unit price, calories, protein, and allergens.

Serving this as static JSON keeps Time to First Byte near zero and eliminates database latency.

Selective Client-Side Hydration

Astro renders the page shell, SEO metadata, and initial category layout as pure HTML/CSS with zero JavaScript overhead. Only the interactive calculator island hydrates on the client (client:load), subscribing to item selection, size toggles, quantity adjustments, and optional bill-splitting inputs without shipping a bloated SPA bundle.

Shareable URL Hash State

To let users bookmark or share custom orders without server-side shortlinks or localStorage lock-in, the current tray serializes directly into the URL hash using a compact dot-delimited format:

#o=slug.size.qty

For example:

#o=royal-new-york-cheesecake-blizzard.medium.1,chicken-strip-basket-4pc.regular.2

On load, a lightweight parser reads window.location.hash, resolves each slug.size.qty tuple against calculator-items.json, and reconstructs the exact meal totals and macro breakdown immediately. Updates use history.replaceState so browser history stays clean.

You can see this pattern running in production on the Dairy Queen Calorie & Price Calculator, where instant macro and cost calculations happen entirely in the browser.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.