Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

How I Built a 50-State Cottage Food Label Maker & FDA Rounding Engine on Cloudflare Workers + D1 FTS5

Home bakers and micro food brands in the US live in an awkward tooling gap: spreadsheets break as soon as packaging waste or pound-to-gram unit conversions get complicated, Word templates misalign on Avery 5163 sheets or

Home bakers and micro food brands in the US live in an awkward tooling gap: spreadsheets break as soon as packaging waste or pound-to-gram unit conversions get complicated, Word templates misalign on Avery 5163 sheets or 4x6 thermal rolls, and commercial nutrition label tools often charge $49–$99/month.

I built CottageFoodTools.com so a food maker can type one ingredient list once and get four print-ready outputs on the Cloudflare edge:

  1. 50-State Cottage Food Label Maker (Avery 5163/5164/22806 & 4x6/2x4/2x3 thermal PDFs at 72 pt/in)
  2. Recipe Cost & Profit Margin Calculator (wholesale bag conversions, trim waste %, labor, packaging & reverse retail pricing)
  3. FDA 21 CFR 101.9 Nutrition Facts Generator (deterministic rounding + vector SVG/PDF)
  4. US Top-9 & UK/EU 14 Allergen Matrix & QR Menu

While shipping it, I also open-sourced the structured 50-state rules CSV & rounding spec under MIT:
-> us-cottage-food-laws-50-states on GitHub

1. Deterministic FDA 21 CFR 101.9 Rounding (Pure TypeScript)

Nutrition labels cannot just call Math.round(). Federal rules define banded rounding per nutrient so tiny floating-point drifts near 0.5g or 50 kcal render identically everywhere:

  • Calories: <= 5 -> 0; > 5 && <= 50 -> nearest 5; > 50 -> nearest 10.
  • Total / Saturated / Trans Fat: < 0.5g -> 0g; < 5g -> nearest 0.5g; >= 5g -> nearest 1g.
  • Carbs, Fiber, Sugars, Protein: < 0.5g -> 0g; 0.5g to < 1g -> <1g; >= 1g -> nearest 1g.
  • Micronutrients (%DV): < 1% -> 0%, <= 10% -> 2% increments, <= 50% -> 5% increments, > 50% -> 10% increments.

We run these rules deterministically in TypeScript across baked batches after applying moisture evaporation percentages (shrinking baked serving weight while keeping dry macro totals exact) and cover every band with unit tests before exporting vector SVG or pdf-lib PDFs.

2. Avoiding False-Positive Allergen Bugs & Sorting Ingredients by Weight

Every cottage food label requires ingredients sorted strictly in descending order by weight (grams) β€” converting cups, tablespoons, teaspoons, pounds, and ounces using ingredient-specific portion density weights before sorting.

Keyword-naive allergen detectors also trigger nasty false positives in bakeries:

  • cocoa butter and cream of tartar are not dairy (Milk)
  • peanut butter is peanuts, not dairy
  • almond milk is tree nuts, not dairy

Explicitly modeling ingredient exceptions alongside explicit USDA allergen tags keeps the bold Contains: line and downloadable US Top-9 / UK-EU 14 matrices trustworthy.

3. Lean Edge Stack on Cloudflare Workers + D1 FTS5

Rather than paying for runtime Node SSR containers, all 68 tool, comparison, label-size, and 50-state pages are statically pre-rendered at build time with structured JSON-LD (SoftwareApplication, BreadcrumbList, FAQPage) and served by a single Hono Worker on Cloudflare.

  • Sub-10ms Ingredient Search (D1 FTS5): Curated USDA FoodData Central staples indexed via usda_foods_fts return common baking staples first (All-Purpose Flour ahead of obscure fortified derivatives).
  • Exact Print Geometry (72 pt/in): The pdf-lib bundle (~435 KB) is code-split and loaded lazily only when a user clicks PDF exportβ€”keeping initial interactive page load fast.
  • Zero-Friction Auth & OAuth Draft Recovery: Anyone can test formulas with zero login. When users click Continue with Google (OAuth 2.0 + S256 PKCE) to save recipes, sessionStorage seamlessly preserves their unsaved ingredient draft across the external redirect.

If you are building vertical SaaS on Cloudflare Workers or need structured cottage food reference data, feel free to check out the MIT dataset repo or test the interactive workbench.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.