Repolode
fastify

fastify/fastify-nextjs

A Fastify plugin that lets an existing Fastify server render Next.js pages with React server-side rendering.

Stars
557
Forks
63
Open issues
6
Last commit
4 days ago

What is it?

@fastify/nextjs is a Fastify plugin that adds Next.js page rendering to an existing Fastify server. It does not replace Next.js's internal web server with Fastify; instead, it exposes a `next` route API on the Fastify instance so that specific routes render Next.js pages.

Routes are declared inside an `after` callback so that Next.js has time to initialize. Developers can render pages directly, pass custom Next.js options through plugin registration, or write custom handlers that call `reply.nextRender` or `reply.nextRenderError`. Static `_next/*` asset serving is enabled by default and can be disabled for custom preprocessing.

The plugin also integrates @fastify/under-pressure, which can return errors when server load is high, since React rendering blocks the Node.js event loop. It additionally covers sharing request-scoped objects from other Fastify plugins with pages, and raising the plugin timeout for large Next.js projects in development mode.

Key features

  • 01Exposes a `next` route API on Fastify for rendering Next.js pages.
  • 02Supports custom render handlers, plus `reply.nextRender` and `reply.nextRenderError` helpers.
  • 03Serves `_next/*` assets by default, with an option to disable it.
  • 04Includes optional under-pressure load-based circuit breaking.
  • 05Lets custom properties from other Fastify plugins be attached to requests for pages.

Use cases

  • Adding server-rendered React pages to an existing Fastify API service.
  • Serving a Next.js frontend alongside Fastify routes in one Node.js process.
  • Protecting a Next.js-rendering Fastify server from overload with health-based errors.

Quick start

  1. 1

    Install the plugin with Next.js and React

    npm i @fastify/nextjs next react react-dom
  2. 2

    Create a page file at pages/hello.js

  3. 3

    Register the plugin and call fastify.next('/hello') inside an after callback, then listen on port 3000

Things to know

  • Next.js must be installed alongside the plugin, and it is meant for adding Next.js rendering to an existing Fastify server, not replacing Next.js's own web server.
  • Large Next.js projects may hit the default 10-second Fastify plugin timeout during development and need a longer pluginTimeout.

Topics

FAQ

What is fastify-nextjs?
A Fastify plugin that lets an existing Fastify server render Next.js pages with React server-side rendering. @fastify/nextjs is a Fastify plugin that adds Next.js page rendering to an existing Fastify server. It does not replace Next.js's internal web server with Fastify; instead, it exposes a `next` route API on the Fastify instance so that specific routes render Next.js pages.
Is fastify-nextjs free to use? What license does it have?
fastify-nextjs is open source under the MIT license. Check the license terms before using it in commercial products.
Is fastify-nextjs still maintained?
Actively maintained. The last commit to fastify-nextjs was on Nov 4, 2026.
What can I use fastify-nextjs for?
Adding server-rendered React pages to an existing Fastify API service. Serving a Next.js frontend alongside Fastify routes in one Node.js process. Protecting a Next.js-rendering Fastify server from overload with health-based errors.
How do I install fastify-nextjs?
Install the plugin with Next.js and React: npm i @fastify/nextjs next react react-dom Create a page file at pages/hello.js Register the plugin and call fastify.next('/hello') inside an after callback, then listen on port 3000
Where can I get fastify-nextjs?
The source code and installation instructions are on GitHub: https://github.com/fastify/fastify-nextjs

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects