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.
Data: do not make people wait for the API
Section titled “Data: do not make people wait for the API”- 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 backgroundconst 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 waitsexport 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).
Images
Section titled “Images”- Rivil Gallery images come in two variants: thumbnail (
?size=thumb) and large (up to 1600 px). Use them insrcsetwith a correctsizes; widthandheighton every image, so the page does not jump;- the main image:
fetchpriority="high"and preload; the restloading="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.
Fonts and CSS
Section titled “Fonts and CSS”- self-hosted
woff2fonts withfont-display: swapandunicode-range; - preload only the two files visible on first screen;
- small CSS inlined in the page (in Astro,
build.inlineStylesheets: 'always').
JavaScript
Section titled “JavaScript”- 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>Cart and checkout
Section titled “Cart and checkout”- 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-Keywhen placing the order.
Security
Section titled “Security”- 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).
SEO and accessibility
Section titled “SEO and accessibility”- redirects managed in the ERP are applied on the server;
- sitemap, canonical,
hreflangfor several languages, structured data; - the test site stays
noindexuntil launch; - respect
prefers-reduced-motion, visible focus, everything usable by keyboard.
Check before launch
Section titled “Check before launch”- 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