React Server Side Rendering Support for Fastify with Next


React server side rendering support for Fastify with Next Framework.


npm i fastify-nextjs next --save


Since Next needs some time to be ready on the first launch, you must declare your routes inside the after callback, after you registered the plugin.
The plugin will expose the api next in Fastify that will handle the rendering for you.

const fastify = require('fastify')()

  .after(() => {'/hello')

fastify.listen(3000, err => {
  if (err) throw err
  console.log('Server listening on http://localhost:3000')

All you server rendered pages must be saved in the folder pages, as you can see in the next documentation.

// /pages/hello.js
export default () => <div>hello world</div>

If you need to pass custom options to next just pass them to register as second parameter.

fastify.register(require('fastify-nextjs'), { dev: true })

If you need to handle the render part yourself, just pass a callback to next:'/hello', (app, req, reply) => {
  // your code
  // `app` is the Next instance
  app.render(req.raw, reply.raw, '/hello', req.query, {})


This project is kindly sponsored by:

Download Details:

Author: fastify

Source Code:

#react #nextjs #reactjs #javascript #fastify

React Server Side Rendering Support for Fastify with Next
73.30 GEEK