Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing
Step 6 of 7staticRoutes + client diet

staticRoutes and the Client Diet: Free Performance You Can Turn On

Enable experimental.staticRoutes for a 2.2x throughput win, understand what leaves the client bundle, and take the smaller hydration and build-loop improvements without a rewrite.

Most framework performance releases ask you to change something. Nuxt 4.7 has one flag that does most of the work, and the rest is subtraction.

What's new#

The big one is experimental.staticRoutes:

nuxt.config.ts
export default defineNuxtConfig({
  experimental: {
    staticRoutes: true,
  },
});

By default, Nuxt creates a fresh vue-router matcher for every server request, in case your app adds routes at runtime. If your routes are fixed at build time, tell Nuxt. The server builds the matcher once.

On Nuxt's benchmark app with about 170 pages, this more than doubled throughput for a small page: 890 → 1,976 requests per second.

The trade-off: router.addRoute(), removeRoute(), and clearRoutes() throw and disappear from the type of useRouter(). Add routes with the pages:extend hook instead.

Why you'd care#

This is the rare optimization that is both free and large. If you do not add routes at runtime, you are paying a per-request cost for a feature you do not use. Turning it off is a one-line win.

It also enables early404, so a request for a missing route can be answered before the full render.

Before#

Every request builds a matcher:

txt
request → create router → resolve → render → response

At 170 pages, that matcher is not free. At 1,000 links, the render is the bigger cost, but the matcher still shows up.

After#

The matcher is built once:

txt
build → matcher
request → resolve → render → response

Same HTML, less work.

The client diet#

The rest of the release is subtraction:

  • ofetch leaves the client bundle when nothing uses $fetch.
  • unhead leaves when nothing on the client touches the head. If your app has no error.vue, the built-in error page brings it only when it renders, taking about 6 kB gzip off the entry of a minimal app.
  • Static useHead() / useSeoMeta() in app.vue are hoisted into the server renderer, so SPA shells get them in the HTML.
  • <NuxtLink> with a static internal to compiles to a plain anchor in production client builds, with no component instance.
  • Page component names are set at build time.

The starter measurements: 80.4 kB and 14 requests became 66.3 kB and 3 requests. Server output dropped from 2.17 MB to 1.23 MB. Installed packages dropped by 50.

Faster hydration#

Nuxt adds V8 compile hints to the entry, layout, and page chunks, so Chrome compiles them off the main thread while they download. In the PR measurements, hydration of a heavy landing page went from 477 ms to 311 ms, and total blocking time from 137 ms to 40 ms.

The browser also paints before hydrating, and hydration yields between boot phases so it does not run one long task. Prefetch hints for lazy chunks wait until the app is ready, so they do not compete with the resources the page needs now.

On the Nuxt UI landing page: hydration 757 → 644 ms (-15%), total blocking time 276 → 180 ms (-35%).

Faster builds and dev#

  • With experimental.viteEnvironmentApi (or compatibilityVersion: 5), the client and server build at the same time, about 11% faster on a starter. Opt out with experimental.parallelBuild: false.
  • Auto-imported components are resolved inside the Vue template compiler instead of a separate pass.
  • Babel only runs on server files that actually contain a decorator, making builds about 30% faster with experimental.decorators.
  • nuxt.schema files load natively without jiti: about 100 ms to 3 ms.
  • Vite scans the app directory of every layer when pre-bundling, so cold nuxt dev reloads the page far less often.
  • A request that is still rendering when the server rebuilds now finishes on the old code instead of failing with a 503.

And Nuxt CLI 4.1 renders the production build itself, with one coherent output instead of each tool's logs. nuxt generate lists prerendered routes and suggests where to deploy.

Do it yourself#

  1. Enable experimental.staticRoutes: true.
  2. Run your benchmark or a simple autocannon against a page. Compare before and after.
  3. Search for router.addRoute, removeRoute, or clearRoutes. Move those routes to pages:extend.
  4. Run nuxt build and compare the client entry size.
  5. Check the network panel: fewer files before hydration.
  6. Try nuxt dev --https and watch the modules load in parallel over HTTP/2.

Gotchas#

  • staticRoutes is not free if you add routes at runtime. It throws. Audit first.
  • early404 changes behavior. A missing route is a 404 earlier. If you rely on a catch-all page, check it.
  • Tree-shaking is usage-based. If something reads globalThis.$fetch in a way Nuxt cannot see at build time, set experimental.globalFetch: true.
  • The numbers are benchmark-specific. Your app will differ. Measure your own page, not the landing page.
  • sourcemap: { client: 'nosource' } is for publishing maps without source. It is not a security boundary for secrets.
  • Nuxt DevTools is a separate install now. Do not mistake the smaller install for a missing feature.

Recap#

staticRoutes is the free throughput win: build the matcher once, skip the per-request work. The client diet, V8 hints, and yield points make hydration cheaper without a rewrite. Measure your own page, then ship it.

Next: Module Authors + Upgrade Lab.