Skip to content

React server side rendering support for Fastify with Next

License

Notifications You must be signed in to change notification settings

developerAkX/fastify-nextjs

 
 

Repository files navigation

fastify-nextjs

CI NPM version Known Vulnerabilities js-standard-style

React server-side rendering support for Fastify with Next.js framework.

Install

npm i fastify-nextjs next react react-dom --save

Usage

Since Next.js 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 next API in Fastify that will handle the rendering for you.

const fastify = require('fastify')()

fastify
  .register(require('fastify-nextjs'))
  .after(() => {
    fastify.next('/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.js 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:

fastify.next('/hello', (app, req, reply) => {
  // your code
  // `app` is the Next instance
  app.render(req.raw, reply.raw, '/hello', req.query, {})
})

If you need to render with Next.js from within a custom handler (such as an error handler), use reply.nextRender

app.setErrorHandler((err, req, reply) => {
  reply.status(err.statusCode || 500)
  return reply.nextRender('/_error')
})

If you need to handle POST routes, you can define the HTTP method:

fastify.next('/api/*', { method: 'GET' });
fastify.next('/api/*', { method: 'POST' });

under-pressure

The plugin includes under-pressure, which can be configured by providing an underPressure property to the plugin options.

Using under-pressure allows implementing a circuit breaker that returns an error when the health metrics are not respected. Because React server side rendering is a blocking operation for the Node.js server, returning an error to the client allows signalling that the server is under too much load.

The available options are the same as those accepted by under-pressure.

For example:

fastify.register(require('fastify-nextjs'), { 
  underPressure: {
    exposeStatusRoute: true
  }
})
  • underPressure - bool|object

    • (default) when false, under-pressure is not registered
    • when true, under-pressure is registered with default options
    • when it is an object, under-pressure is registered with the provided options

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

About

React server side rendering support for Fastify with Next

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.6%
  • TypeScript 2.4%