Performance
Goal: send less script and make fewer requests, and measure it before and after.
- Measure what ships.
hamur build --analyze=htmlwrites a treemap of each client chunk to.hamur/analyze/, andjsonwrites the numbers. Look for a large module that every page loads.
hamur build --analyze=html
hamur build --analyze=json
- Hydrate a heavy part late.
defineLazyHydrationComponentrenders on the server as usual and loads its chunk and hydrates only when its trigger fires:"visible","idle","interaction", a media query or a timer. Until then its server HTML stays as it is and no script for it is requested:
import { defineLazyHydrationComponent } from "@hamur/adapter-react"
const Comments = defineLazyHydrationComponent("visible", () => import("./Comments.tsx"), { rootMargin: "200px" })
export default function Post() {
return (
<article>
<h1>A long post</h1>
<Comments postId="42" />
</article>
)
}
- Ship none for what does not change. A page that needs no interaction can be a server-only page, and a page with a few interactive parts can be an island page, which hydrates only those parts.
- Cache what you can.
swrandisrroute rules keep rendered pages; Caching and ISR says which to take. A page that is the same for everyone can be prerendered and cost nothing per request. - Let
Linkprefetch. By default a link loads the page's script and its data on hover or focus, so the click finds them ready (Navigation). Images and fonts have their own modules that size, compress and preload: Image and Fonts.
See also: Rendering modes, Build output.