Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing
Step 7 of 7Renderer plugins + upgrade

Module Authors + Upgrade Lab: Renderer Plugins, Rsbuild, and a Safe 4.7

Use renderer plugins and the render:document hook, adopt defineHandler, move from Rspack to Rsbuild, and finish with the security and upgrade checklist for Nuxt 4.7.

The last step is for the people who extend Nuxt, and for anyone about to upgrade a real app.

Renderer plugins#

Some modules need to add to the initial HTML. @nuxtjs/color-mode injects a small script; an analytics module injects a loader. On @nuxt/vite-server there is no Nitro to register a plugin with, so 4.7 adds a new place to live:

module.ts
addRendererPlugin(resolve('./runtime/server/renderer'))
runtime/server/renderer.ts
import type { RendererPlugin } from 'nuxt/server'

export default (({ hooks }) => {
  hooks.hook('render:html', (html) => {
    html.head.push(`<script>${script}</script>`)
  })
}) satisfies RendererPlugin

A renderer plugin attaches to the renderer, not a particular server, so it works with Nitro v2, Nitro v3, and @nuxt/vite-server. On an older Nuxt, addRendererPlugin registers the same file as a Nitro plugin.

There is also a new render:document hook, called with the finished HTML document of a non-streamed response, after every render:html listener has run.

defineHandler from nuxt/server#

Nuxt 4.7 adds defineHandler to the portable server surface, with defineEventHandler as an alias. It matches Nitro v3:

server/api/hello.ts
import { defineHandler } from 'nuxt/server'

export default defineHandler(() => ({ hello: 'world' }))

On Nuxt 5 the same names are auto-imported from nuxt/server, so explicit imports written on 4.6 keep working unchanged.

Rsbuild instead of Rspack#

@nuxt/rspack-builder is deprecated. The replacement is @nuxt/rsbuild-builder:

bash
pnpm remove @nuxt/rspack-builder
pnpm add -D @nuxt/rsbuild-builder
nuxt.config.ts
export default defineNuxtConfig({
  builder: 'rsbuild',
})

It replaces the Rspack builder, which still works with a warning. Modules can use extendRsbuildConfig() and addRsbuildPlugin(), and addRspackPlugin() keeps working.

More for module authors#

  • build:environment:start, build:environment:done, and build:prerender:* hooks report every production bundle and prerendered route, whatever the builder.
  • updateRuntimeConfig() accepts schemas for your module's options.
  • defineHandler from nuxt/server, with defineEventHandler as an alias.
  • A nuxt.request tracing channel around each request, so APM tools get the same span hierarchy with any server builder.

The security checklist#

The release fixes security issues. The three listed advisories:

  • Route rules on _payload.json (GHSA-jxc9-w3j6-pwp2, medium). A wildcard cache, swr, or isr rule combined with a more specific page rule could cache one user's payload and serve it to another.
  • Encoded slashes on catch-all routes (GHSA-vrh2-cfcx-c3fg, medium). An incomplete fix for an earlier advisory. A repeated or leading encoded slash could bypass route rules for a catch-all page.
  • Dev server runtime config (GHSA-h5xj-x893-p5f7, low, development only). If nuxt dev is reachable from an untrusted network, private runtime config could be read from earlier Nitro build output.

If you use cache, swr, or isr route rules on pages with user-specific data, purge any CDN or edge cache that may already hold a _payload.json after you upgrade. The code fix does not evict old entries.

The upgrade checklist#

  1. Read the heads-up list. DevTools is separate; vue ^3.5.46 is required; template diagnostics are on; @nuxt/rspack-builder is deprecated; <NuxtClientFallback> is deprecated.
  2. Run npx nuxt upgrade --dedupe. It refreshes the lockfile and pulls in Nuxt CLI 4.1, my-bad v1, and vite-plugin-checker v1.
  3. Add @nuxt/devtools if you use it. Nuxt no longer installs it for you.
  4. Fix NUXT_B4025 warnings. Add external to links that point at server routes, public/ files, or other sites.
  5. Check globalThis.$fetch. If you read it from the console, an inline script, or public/, set experimental.globalFetch: true.
  6. Try staticRoutes. If you do not add routes at runtime, it is free throughput.
  7. Pilot Nitro v3 on a branch. Add nitro, fix route rules and imports, turn nitroLegacy off, run the build.
  8. Purge caches if the payload advisory applies to you.
  9. Keep template diagnostics on. The noise is the point.

Do it yourself#

  1. Add a renderer plugin to a small module. Inject a meta tag.
  2. Move one handler to defineHandler from nuxt/server.
  3. Switch a test project to builder: 'rsbuild'.
  4. Run the upgrade checklist on the branch.
  5. Enable staticRoutes and typeCheck: 'dev'.
  6. Run nuxt build and nuxt test (or your CI equivalent).
  7. Deploy a preview, not production, and click through the pages that use route rules.

Gotchas#

  • A renderer plugin is not a Nitro plugin. It runs with any server builder. If your code only works on Nitro, keep it a Nitro plugin.
  • render:document only fires for non-streamed responses. A streamed page does not have a finished document to hand you.
  • Rsbuild does not read the webpack options. Some Rspack hooks change. Read the migration section before you switch.
  • The dev-server advisory is development only, but it is still real. Do not expose nuxt dev to a network you do not trust.
  • Nitro v3 is still a release candidate. Pilot it, do not bet the quarter on it.
  • A green build is not a migration. Click the pages that use route rules, caching, and payload extraction.

Recap#

Renderer plugins give modules a server-agnostic place for HTML injection, defineHandler aligns Nuxt with Nitro v3, Rsbuild replaces Rspack, and the upgrade checklist is short enough to run today. Do the branch, do the preview, then ship.

That is Nuxt 4.7. The runway is real, and for the first time we are moving down it.

← Back to the series: Learning Nuxt 4.7.