# Performance

Goal: send less script and make fewer requests, and measure it before and after.

1. Measure what ships. `hamur build --analyze=html` writes a treemap of each client chunk to `.hamur/analyze/`, and `json` writes the numbers. Look for a large module that every page loads.

````sh [recipes/performance.sh]
hamur build --analyze=html
hamur build --analyze=json
````

2. Hydrate a heavy part late. `defineLazyHydrationComponent` renders 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:

````tsx [recipes/react/Performance.tsx]
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>
  )
}
````

3. Ship none for what does not change. A page that needs no interaction can be a [server-only page](/islands-and-streaming), and a page with a few interactive parts can be an island page, which hydrates only those parts.
4. Cache what you can. `swr` and `isr` route rules keep rendered pages; [Caching and ISR](/caching-and-isr) says which to take. A page that is the same for everyone can be prerendered and cost nothing per request.
5. Let `Link` prefetch. By default a link loads the page's script and its data on hover or focus, so the click finds them ready ([Navigation](/navigation)). Images and fonts have their own modules that size, compress and preload: [Image](/image) and [Fonts](/fonts).

See also: [Rendering modes](/rendering-modes), [Build output](/build-output).
