fastify-nuxtjs icon indicating copy to clipboard operation
fastify-nuxtjs copied to clipboard

Vue server side rendering support for Fastify with Nuxt

fastify-nuxtjs

fastify-nuxtjs

npm version Dependencies npm downloads code style: prettier License: MIT CI workflow

Vue server side rendering support for Fastify with Nuxt.js Framework.

Install

Install with yarn:

yarn add fastify-nuxtjs nuxt

Install with npm:

npm install fastify-nuxtjs nuxt

Usage

Since Nuxt 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 nuxt in Fastify that will handle the rendering for you.

const fastify = require('fastify')();

fastify.register(require('fastify-nuxtjs')).after(() => {
  fastify.nuxt('/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 nuxt documentation.

<template>
  <HelloWorld />
</template>

If you need to handle the render part yourself, just pass a callback to nuxt:

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

Serve all routes from your pages/ folder

Using *:

const fastify = require('fastify')();

fastify.register(require('fastify-nuxtjs')).after(() => {
  fastify.nuxt('*');
});

Or import your generated routes.json from your .nuxt folder:

const nuxtRoutes = require('./.nuxt/routes.json');
const fastify = require('fastify')();

fastify.register(require('fastify-nuxtjs')).after(() => {
  nuxtRoutes.forEach((nuxtRoute) => {
    fastify.nuxt(nuxtRoute.path);
  });
});

Acknowledgements

Heavily inspired by fastify-nextjs

License

Licensed under MIT.