Skip to content

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
hamur build --analyze=html
hamur build --analyze=json
  1. 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
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>
  )
}
  1. 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.
  2. Cache what you can. swr and isr route 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.
  3. 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). Images and fonts have their own modules that size, compress and preload: Image and Fonts.

See also: Rendering modes, Build output.