Fastify plugin for Next.js, the React framework.
This is an alternative to the official @fastify/nextjs plugin. See comparison.
npm install fastify-next next react react-dom
If you want your Fastify server to work similar to the Next.js CLI, do the following:
import fastify from 'fastify';
import FastifyNext from 'fastify-next';
// Registering the plugin adds the 'next' decorator
await fastify.register(FastifyNext);
// Pass '*' to handle all incoming requests with Next.js
fastify.next('*');
For the most part, this package will work as a drop-in replacement for @fastify/nextjs. However, there are a few differences.
_next/*
route handlingBy default @fastify/nextjs handles all incoming requests to /_next/*
. To mimic this behaviour with fastify-next, add the following:
fastify.next('/_next/*');
The @fastify/nextjs plugin includes under-pressure as a dependency which can be enabled by passing the underPressure: true
option. This package does not currently include support for under-pressure.
Next.js type-checks and compiles the application when the dev server first starts. The @fastify/nextjs plugin waits for the Next.js server to finish doing this before resolving plugin initialization. This can lead to plugin timeout in large Next.js projects.
This plugin initializes immediately, allowing you to make requests to the Fastify server immediately, but will wait for Next.js to complete startup before handling incoming requests.
Note: this has no effect in production. The dev server can be enabled using the dev: true
option or setting NODE_ENV=development
in the environment.