Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

How I built a free roof calculator in Astro that scores 100 on Lighthouse

Roof quotes are written in "squares", and most homeowners have no easy way to check if 28 squares is right for their house. I went looking for a calculator. Most of the ones I found loaded a wall of ads, asked for an em

Roof quotes are written in "squares", and most homeowners have no easy way to check if 28 squares is right for their house.

I went looking for a calculator. Most of the ones I found loaded a wall of ads, asked for an email before showing a number, or took five seconds to become usable on a phone. So I built my own. This post covers the math, the stack and a few small decisions that kept the site fast.

The math is smaller than you think

A roof is mostly a tilted rectangle. If you know the footprint of the house and how steep the roof is, you can get the real surface area with one multiplier.

Roof pitch in the US is written as rise over 12 inches of run, like 6/12. The sloped length of one foot of run is the hypotenuse of a right triangle, so the multiplier is:

/** Pitch multiplier for a rise of `rise` inches per 12 inches of run. */
export function pitchMultiplier(rise: number): number {
  return Math.sqrt(1 + (rise / 12) ** 2);
}

A 6/12 roof gives 1.118, so a 1,344 sq ft footprint becomes about 1,503 sq ft of actual roof. A 12/12 roof is 1.414, which is 41% more material than a flat roof on the same house.

People also measure with a phone level app, which gives degrees, so I added both directions:

export function pitchToAngle(rise: number): number {
  return (Math.atan(rise / 12) * 180) / Math.PI;
}

export function angleToPitch(angleDeg: number): number {
  return 12 * Math.tan((angleDeg * Math.PI) / 180);
}

The rest is roofing conventions:

  • Overhang is added on every side of the footprint before the multiplier.
  • Waste depends on the roof shape. I default to 10% for gable, 15% for hip (lots of angled cuts) and 5% for flat.
  • Squares are 100 sq ft each. That is how shingles are sold.
  • Bundles are usually 3 per square, 4 for heavier designer shingles.

All of this lives in one file of pure functions with no DOM access, which made it easy to unit test with Vitest. When you are dealing with someone's roofing budget, tests on the math are worth it.

Why Astro

The site is mostly content: one calculator, a pitch chart, an FAQ and about 20 guides on things like roof pitch, sheathing thickness and replacement cost. Astro fits that well. Pages are static HTML, and the calculator is a small vanilla TypeScript script. There is no React or Vue on the page, because a form with a dozen inputs does not need a framework.

A few config choices did most of the performance work:

export default defineConfig({
  output: 'static',
  trailingSlash: 'never',
  build: {
    inlineStylesheets: 'always', // no render-blocking CSS request
    format: 'file',              // /about -> about.html
  },
  vite: {
    build: { assetsInlineLimit: 0 }, // never inline scripts (CSP)
  },
});

Inlining the CSS removes a render-blocking request. Tailwind v4 keeps the CSS small enough that this is a good trade.

A strict CSP without breaking anything

I wanted a Content Security Policy with script-src 'self' and no unsafe-inline. The catch is that Vite inlines small scripts into the HTML by default, and the CSP blocks those. Setting assetsInlineLimit: 0 forces every script into its own file, so everything loads from the same origin.

One surprise came after going live. My Best Practices score dropped from 100 to 92. The cause was Cloudflare's Web Analytics, which injects a beacon script into every page. My CSP blocked it, which produced a console error. Turning off the automatic injection brought the score back to 100.

Shareable results with URL params

Every input is mirrored to the query string, so a result is just a link:

/?l=40&w=30&p=6&o=12

That opens the calculator with a 40 by 30 ft house, a 6/12 pitch and a 12 inch overhang. A homeowner can send it to a roofer, and the blog posts use the same links as calls to action. It also costs nothing: no database, no accounts.

Printing a clean report instead of the page

My first version of "Print or save as PDF" printed the whole page, with the nav, the dark background and half the FAQ. It looked broken.

The fix was to build a separate report element on click, add a class to <html>, and use a print stylesheet that hides everything except that report:

@media print {
  html.printing body > *:not(#print-report) { display: none; }
}

The class is removed on afterprint. The PDF now has a title, the date, a simple table of results and one link back.

Mobile first, for real

Most people use a roof calculator on a phone, often standing outside the house. On small screens I tightened the spacing, hid the helper text that was pushing results off screen and kept every tap target at least 44px tall. That cut the scrolling a lot without making anything feel cramped.

Hosting and indexing

The site runs on Cloudflare Pages. A few things I would do again:

  • Redirect the pages.dev subdomain and www to the main domain with a 301, so search engines see one copy of each page.
  • Use a _headers file for the CSP, HSTS and long cache headers on hashed assets.
  • Ping IndexNow after each deploy. It is a single POST with your URL list, and Bing picks up changes much faster.

Results

Lighthouse scores are 100 for Performance, Accessibility, Best Practices and SEO on mobile and desktop, and the whole calculator page is about 25 KB of compressed HTML.

You can try it at RoofSqft. If you are into the geometry side, I also made a free printable roof pitch chart with angles, slope percentages and area multipliers from 1/12 to 24/12.

I'd love feedback on the calculator UX, especially from anyone who has measured a roof before.

📰 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.