Cache team pages on demand instead of re-rendering them per request
/team/[id] carried revalidate = 600 but no generateStaticParams, and in Next 16 a dynamic segment is only cacheable when the route declares static params — so every hit was a fresh render of up to 240 cards (1.3 MB of HTML, 0.14-0.28 s on one thread). It now returns the ten largest teams, which puts the route in static-with-fallback mode: each page is rendered on first request and then served from the cache. Measured on a local build of this commit: /team/Q822877 (prerendered) HIT, 1,303,014 bytes /team/Q5095633 (never seen) MISS -> HIT, s-maxage=600, stale-while-revalidate /team/Q99999999999 (bogus) 404, still 404 after caching survives a restart (entry persisted under .next/server/route-cache) The ten prerendered pages keep the route explicitly in static mode and cost 33 MB. Prerendering the 200 busiest teams instead measured 179 MB of HTML and 470 MB of build output for pages that are read once — the on-demand fallback caches the same pages for nothing, so the expensive set was dropped.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import type { Metadata } from "next";
|
||||
import { getTeam, squadOf } from "@/lib/data";
|
||||
import { getCatalogue, getTeam, squadOf } from "@/lib/data";
|
||||
import { CardLink } from "@/components/CardTile";
|
||||
import { RugbyBall } from "@/components/RugbyBall";
|
||||
import { SPORT_LABEL, flag } from "@/lib/format";
|
||||
@@ -13,6 +13,30 @@ import type { Sport } from "@/lib/types";
|
||||
// traffic built a queue that ran into minutes and made the site feel frozen.
|
||||
export const revalidate = 600;
|
||||
|
||||
/**
|
||||
* Team pages are cached (ISR), not re-rendered per request. In Next 16 a dynamic segment is only
|
||||
* eligible for the cache when the route declares `generateStaticParams`; without it every hit to
|
||||
* /team/[id] is a fresh render, with it the first hit renders and the rest are served from the
|
||||
* cache for `revalidate` seconds. Measured: first request `x-nextjs-cache: MISS`, every later
|
||||
* request `HIT` with `s-maxage=600`, and the entry survives a restart (it is persisted under
|
||||
* .next/server/route-cache).
|
||||
*
|
||||
* Only the ten largest teams are prerendered at build time. Prerendering the 200 busiest ones was
|
||||
* measured at 179 MB of HTML per build, for pages that are read once, and the same caching is
|
||||
* available for nothing through the on-demand fallback; those ten exist purely to keep the route
|
||||
* explicitly in static-with-fallback mode. `dynamicParams` stays true, so every other team is
|
||||
* rendered on first request and cached like the rest, and unknown ids still 404.
|
||||
*/
|
||||
const PRERENDER_TEAMS = 10;
|
||||
|
||||
export async function generateStaticParams() {
|
||||
return getCatalogue()
|
||||
.teams.slice()
|
||||
.sort((a, b) => (b.c0 ?? 0) - (a.c0 ?? 0))
|
||||
.slice(0, PRERENDER_TEAMS)
|
||||
.map((t) => ({ id: t.q }));
|
||||
}
|
||||
|
||||
type Props = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
||||
|
||||
Reference in New Issue
Block a user