https://github.com/applicazza/fastify-nextjs
Serve Next.js requests via Fastify
https://github.com/applicazza/fastify-nextjs
fastify nextjs
Last synced: 2 months ago
JSON representation
Serve Next.js requests via Fastify
- Host: GitHub
- URL: https://github.com/applicazza/fastify-nextjs
- Owner: applicazza
- License: mit
- Created: 2021-06-21T20:52:15.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-11-12T03:05:52.000Z (over 1 year ago)
- Last Synced: 2025-03-02T11:51:21.812Z (2 months ago)
- Topics: fastify, nextjs
- Language: TypeScript
- Homepage:
- Size: 1.03 MB
- Stars: 13
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# @applicazza/fastify-nextjs
`@applicazza/fastify-nextjs` is a plugin for serving [Next.js](https://nextjs.org) requests
via [Fastify](https://github.com/fastify/fastify).[](https://www.npmjs.com/package/@applicazza/fastify-nextjs) [](https://codecov.io/gh/applicazza/fastify-nextjs)
**This project is yet to reach stable state. You may help by contributing and testing it.**
## Why
Original [fastify-nextjs](https://github.com/fastify/fastify-nextjs) doesn't pass response through Fastify's pipeline thus hooks that you may need won't work. This package is using JavaScript's [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) to intercept calls to NodeJs [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse) and pass it to Fastify.
## Usage
### Add dependencies
```shell
yarn add @applicazza/fastify-nextjs
yarn add fastify-static
```### Disable compression in Next.js (next.config.js)
```js
module.exports = {
compress: false,
};
```### Default example
```ts
import createFastify from 'fastify';
import fastifyNextJs from '@applicazza/fastify-nextjs';const dev = process.env.NODE_ENV !== 'production';
const fastify = createFastify();
fastify.register(fastifyNextJs, {
dev,
});await fastify.after();
fastify.passNextJsRequests();
await fastify.listen(0);
```### Extended example
```ts
import createFastify from 'fastify';
import fastifyNextJs from '@applicazza/fastify-nextjs';const dev = process.env.NODE_ENV !== 'production';
const fastify = createFastify();
fastify.register(fastifyNextJs, {
dev,
});await fastify.after();
fastify.passNextJsDataRequests();
fastify.passNextJsImageRequests();
if (dev) {
fastify.passNextJsDevRequests();
} else {
fastify.passNextJsStaticRequests();
}
fastify.passNextJsPageRequests();await fastify.listen(0);
```### Plugin accepts following options:
```ts
interface FastifyNextJsOptions {
basePath?: string;
dev?: boolean;
dir?: string;
}
```### Plugin augments fastify instance with following properties and methods:
```ts
interface FastifyNextJsDecoratorArguments {
logLevel?: LogLevel;
}interface FastifyInstance {
nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextServer: NextServer;
passNextJsRequests: (args?: FastifyNextJsDecoratorArguments) => void;
passNextJsDataRequests: (args?: FastifyNextJsDecoratorArguments) => void;
passNextJsDevRequests: (args?: FastifyNextJsDecoratorArguments) => void;
passNextJsImageRequests: (args?: FastifyNextJsDecoratorArguments) => void;
passNextJsPageRequests: (args?: FastifyNextJsDecoratorArguments) => void;
passNextJsStaticRequests: (args?: FastifyNextJsDecoratorArguments) => void;
}
```## Building from source
```shell
yarn build
```## Testing
```shell
yarn test
```## License
Licensed under [MIT](./LICENSE.md).