Skip to content

Performance and quality

The practices below are the ones used in Casa Lumen and in the demo sites. Target: LCP under 2.5 s and CLS under 0.1 on mobile. Measure with PageSpeed on the published page, not on your computer.

  • keep responses in server memory for a short time (one minute);
  • when they expire, serve the old value and refresh in the background;
  • merge identical requests started at the same time;
  • limit background refreshes (the demo allows two at once);
  • purge from the webhook only what changed;
  • start independent requests in parallel;
  • put a time limit on every call (the demo uses 20 s).
// in-memory cache: serve the old value at once and refresh in the background
const memo = new Map<string, { at: number; value: unknown; stale?: boolean }>()
const flight = new Map<string, Promise<unknown>>()
export function cached<T>(key: string, load: () => Promise<T>, ttl = 60_000): Promise<T> {
const hit = memo.get(key)
if (hit && !hit.stale && Date.now() - hit.at < ttl) return Promise.resolve(hit.value as T)
// identical requests started together become one
let next = flight.get(key) as Promise<T> | undefined
if (!next) {
next = load()
.then((value) => (memo.set(key, { at: Date.now(), value }), value))
.finally(() => flight.delete(key))
flight.set(key, next)
}
if (!hit) return next
next.catch(() => undefined)
return Promise.resolve(hit.value as T)
}
// called from the webhook: mark as stale instead of deleting, so nobody waits
export const purge = () => memo.forEach((e) => (e.stale = true))
  • catalog pages: Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=300;
  • pages with personal data: private, no-store. Never mix the two;
  • without a CDN in front, keep the public HTML in memory, already compressed;
  • compress HTML and JSON with Brotli or gzip if your proxy does not;
  • warm the cache at startup and report “ready” only afterwards, so the first visit after a deploy is not slow;
  • slow or personal parts (reviews, account menu) load separately (in Astro, server:defer).
  • Rivil Gallery images come in two variants: thumbnail (?size=thumb) and large (up to 1600 px). Use them in srcset with a correct sizes;
  • width and height on every image, so the page does not jump;
  • the main image: fetchpriority="high" and preload; the rest loading="lazy";
  • in lists only the first visible cards load eagerly;
  • in galleries and sliders, hidden images get their address only when you reach them;
  • for your own static photos: AVIF or WebP, with a separate phone variant.
  • self-hosted woff2 fonts with font-display: swap and unicode-range;
  • preload only the two files visible on first screen;
  • small CSS inlined in the page (in Astro, build.inlineStylesheets: 'always').
  • server-rendered HTML; JavaScript only where people really interact;
  • heavy React components start on first touch or after a few seconds, and a click made meanwhile is not lost;
  • no large libraries for small things;
  • the next page can be prefetched, excluding cart, account and checkout:
<script type="speculationrules">
{"prefetch":[{"where":{"and":[{"href_matches":"/*"},{"not":{"href_matches":["/api/*","/cont*","/cos","/checkout"]}}]},"eagerness":"moderate"}]}
</script>
  • price and totals are always computed by the API;
  • keep the response of a cart change, so the next page does not ask again;
  • cache shipping quotes briefly and drop them on any cart change;
  • Idempotency-Key when placing the order.
  • headers on every response: Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options: nosniff, Referrer-Policy, Permissions-Policy;
  • visitor address: with a single proxy in front, the last value of X-Forwarded-For; the first can be forged;
  • behind a proxy, declare the allowed domain for forms (in Astro, security.allowedDomains).
  • redirects managed in the ERP are applied on the server;
  • sitemap, canonical, hreflang for several languages, structured data;
  • the test site stays noindex until launch;
  • respect prefers-reduced-motion, visible focus, everything usable by keyboard.
  • PageSpeed on mobile: home, a list, a detail page
  • No image without dimensions
  • API down: the site shows the last version or a clear message, not a blank error
  • The first visit after a deploy does not take seconds
  • A change in the ERP shows on the site without a restart