{"id":13456322,"url":"https://github.com/fastify/fastify-nextjs","last_synced_at":"2025-05-14T08:05:47.509Z","repository":{"id":37743051,"uuid":"92283780","full_name":"fastify/fastify-nextjs","owner":"fastify","description":"React server side rendering support for Fastify with Next","archived":false,"fork":false,"pushed_at":"2025-05-08T16:40:47.000Z","size":2610,"stargazers_count":545,"open_issues_count":16,"forks_count":61,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-05-08T17:44:27.758Z","etag":null,"topics":["fastify","fastify-plugin","next","react","ssr"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/nextjs","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2017-05-24T11:12:42.000Z","updated_at":"2025-05-08T16:40:44.000Z","dependencies_parsed_at":"2023-10-02T03:40:39.980Z","dependency_job_id":"c89ea19a-d3b0-4ce7-9062-484d2bb05e4a","html_url":"https://github.com/fastify/fastify-nextjs","commit_stats":{"total_commits":814,"total_committers":56,"mean_commits":"14.535714285714286","dds":0.2665847665847666,"last_synced_commit":"3e4444b6f4bb007362e02ce0ef1989b63747d8a6"},"previous_names":["fastify/fastify-react"],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-nextjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-nextjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-nextjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-nextjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-nextjs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101615,"owners_count":22014909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin","next","react","ssr"],"created_at":"2024-07-31T08:01:19.859Z","updated_at":"2025-05-14T08:05:42.500Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/nextjs\n\n[![CI](https://github.com/fastify/fastify-nextjs/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-nextjs/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/nextjs.svg?style=flat)](https://www.npmjs.com/package/@fastify/nextjs)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)\n\nReact server-side rendering support for Fastify with [Next.js](https://nextjs.org/docs/advanced-features/custom-server) framework. This library is for letting an existing Fastify server utilize NextJS, not for replacing NextJS' internal webserver with Fastify.\n\n## Install\n```\nnpm i @fastify/nextjs next react react-dom\n```\n\n## Usage\nSince 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.\nThe plugin will expose the `next` API in Fastify that will handle the rendering for you.\n```js\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('@fastify/nextjs'))\n  .after(() =\u003e {\n    fastify.next('/hello')\n  })\n\nfastify.listen({ port: 3000 }, err =\u003e {\n  if (err) throw err\n  console.log('Server listening on http://localhost:3000')\n})\n```\n\nAll your server rendered pages must be saved in the folder `pages`, as you can see in the [Next.js documentation](https://nextjs.org/docs/advanced-features/custom-server).\n```js\n// /pages/hello.js\nexport default () =\u003e \u003cdiv\u003ehello world\u003c/div\u003e\n```\nIf you need to pass [custom options](https://nextjs.org/docs/advanced-features/custom-server) to `next` just pass them to register as second parameter.\n```js\nfastify.register(require('@fastify/nextjs'), { dev: true })\n```\n\nIf you need to handle the render part yourself, just pass a callback to `next`:\n```js\nfastify.next('/hello', (app, req, reply) =\u003e {\n  // your code\n  // `app` is the Next instance\n  app.render(req.raw, reply.raw, '/hello', req.query, {})\n})\n```\n\nIf you need to render with Next.js from within a custom handler, use `reply.nextRender`\n\n```js\napp.setErrorHandler((err, req, reply) =\u003e {\n  return reply.nextRender('/a')\n})\n```\n\nIf you need to render a Next.js error page, use `reply.nextRenderError`\n\n```js\napp.setErrorHandler((err, req, reply) =\u003e {\n  return reply.status(err.statusCode || 500).nextRenderError(err)\n})\n```\n\nIf you need to handle HEAD routes, you can define the HTTP method:\n```js\nfastify.next('/api/*', { method: 'GET' });\nfastify.next('/api/*', { method: 'HEAD' });\n```\n\n### Assets serving\n\nBy default plugin handle route `${basePath}/_next/*` and forward to Next.js.\n\nIf you have custom preprocessing for `_next/*` requests, you can prevent this this handling with `noServeAssets: true` property for plugin options:\n\n```js\nfastify\n  .register(require('@fastify/nextjs'), {\n    noServeAssets: true\n  })\n  .after(() =\u003e {\n    fastify.next(`${process.env.BASE_PATH || ''}/_next/*`, (app, req, reply) =\u003e {\n      // your code\n      app.getRequestHandler()(req.raw, reply.raw).then(() =\u003e {\n        reply.hijack()\n      })\n    })\n  })\n```\n\n### under-pressure\n\nThe plugin includes [under-pressure](https://github.com/fastify/under-pressure), which can be configured by providing an `underPressure` property to the plugin options.\n\nUsing `under-pressure` allows implementing a circuit breaker that returns an error when the health metrics are not respected.\nBecause 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.\n\nThe available options are the same as those accepted by `under-pressure`.\n\nFor example:\n\n```js\nfastify.register(require('@fastify/nextjs'), {\n  underPressure: {\n    exposeStatusRoute: true\n  }\n})\n```\n\n- `underPressure` - `bool|object`\n\n  - (default) when false, `under-pressure` is not registered\n  - when true, `under-pressure` is registered with default options\n  - when it is an object, `under-pressure` is registered with the provided options\n\n## Custom properties on the request object\nIf you want to share custom objects (for example other fastify plugin instances - e.g. @fastify/redis) across the server/client with each page request, you can use the `onRequest` hook to add it to the request object.\nHere is an example on how to do it:\n\n```js\nconst Fastify = require('fastify')\nconst FastifyRedis = require('@fastify/redis')\nconst FastifyNextJS = require('@fastify/nextjs')\n\nconst fastify = Fastify()\nfastify.register(FastifyRedis, { host: '127.0.0.1' })\nfastify.register(FastifyNextJS)\n\nfastify.register(function(instance) {\n  // for performance reasons we do not want it to run on every request\n  // only the nextjs one should run\n  instance.addHook('onRequest', function(request, reply, done) {\n    // define a custom property on the request\n    request.raw.customProperty = { hello: \"world\" }\n    // OR make the instance of @fastify/redis available in the request\n    request.raw.redisInstance = instance.redis\n    done()\n  })\n\n  instance.next('/', function(app, request, reply) {\n    // your custom property containing the object will be available here\n    // request.raw.customProperty\n    // OR the redis instance\n    // request.raw.redisInstance\n    app.render(request.raw, reply.raw, '/hello', request.query, {})\n  })\n}, { prefix: '/hello' })\n```\nIn the example above we made the `customProperty` and `redisInstance` accessible in every request that is made to the server. On the client side it can be accessed like in this example:\n```js\nconst CustomPropPage = ({ cp, ri }) =\u003e \u003cdiv\u003ecustom property value: {cp} | redis instance: {ri}\u003c/div\u003e;\n\nexport default CustomPropPage;\n\nexport const getServerSideProps = async function (ctx) {\n  return {\n    props: {\n      cp: ctx.req.customProperty,\n      ri: ctx.req.redisInstance,\n    }\n  };\n};\n```\n\n## Plugin Timeout and Next.js development mode\nThe default timeout for plugins in Fastify is 10000ms, which can be a problem for huge Next.js Projects where the initial build time is higher than that.\nUsually, you will get an error like this:\n```\nError: ERR_AVVIO_PLUGIN_TIMEOUT: plugin did not start in time: /app/node_modules/@fastify/nextjs/index.js. You may have forgotten to call 'done' function or to resolve a Promise\n```\n\nThe workaround or fix is to increase the plugin timeout:\n```js\nconst isDev = process.env.NODE_ENV !== 'production';\nconst fastify = Fastify({ pluginTimeout: isDev ? 120_000 : undefined });\n```\n\n## Development\nCI currently runs npm@6 so when upgrading packages, please use this version.\n\n## Acknowledgements\n\nThis project is kindly sponsored by:\n- [NearForm](https://nearform.com)\n- [LetzDoIt](https://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-nextjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-nextjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-nextjs/lists"}