Skip to content

Commit

Permalink
Add nextJsProxyRequestHandler and nextJsRawRequestHandler decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
applicazza committed Jun 23, 2021
1 parent 0e272dc commit 6acb083
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Original [fastify-nextjs](https://github.com/fastify/fastify-nextjs) doesn't pas

## Usage

Add dependencies
####Add dependencies

```shell
yarn add @applicazza/fastify-nextjs
yarn add fastify-static
```

Disable compression in Next.js (next.config.js)
####Disable compression in Next.js (next.config.js)

```js
module.exports = {
Expand Down Expand Up @@ -89,6 +89,8 @@ Plugin augments fastify instance with following properties and methods:

```ts
interface FastifyInstance {
nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextServer: NextServer;
passNextJsRequests: () => void;
passNextJsDataRequests: () => void;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
"test": "jest --coverage"
},
"types": "dist/index.d.ts",
"version": "0.0.4"
"version": "0.0.5"
}
18 changes: 11 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ declare module 'fastify' {
// eslint-disable-next-line no-unused-vars
// noinspection JSUnusedGlobalSymbols
interface FastifyInstance {
nextJsProxyRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextServer: NextServer;
passNextJsRequests: () => void;
passNextJsDataRequests: () => void;
Expand Down Expand Up @@ -61,11 +63,11 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
};

const passNextJsDataRequestsDecorator = () => {
fastify.get(`${basePath}/_next/data/*`, nextProxyRequestHandler);
fastify.get(`${basePath}/_next/data/*`, nextJsProxyRequestHandler);
};

const passNextJsDevRequestsDecorator = () => {
fastify.all(`${basePath}/_next/*`, nextRawRequestHandler);
fastify.all(`${basePath}/_next/*`, nextJsRawRequestHandler);
};

const passNextJsStaticRequestsDecorator = () => {
Expand All @@ -78,26 +80,28 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,

const passNextJsPageRequestsDecorator = () => {
if (basePath) {
fastify.all(`${basePath}`, nextProxyRequestHandler);
fastify.all(`${basePath}`, nextJsProxyRequestHandler);
}
fastify.all(`${basePath}/*`, nextProxyRequestHandler);
fastify.all(`${basePath}/*`, nextJsProxyRequestHandler);
};

fastify.decorate('passNextJsRequests', passNextJsRequestsDecorator);
fastify.decorate('passNextJsDataRequests', passNextJsDataRequestsDecorator);
fastify.decorate('passNextJsDevRequests', passNextJsDevRequestsDecorator);
fastify.decorate('passNextJsStaticRequests', passNextJsStaticRequestsDecorator);
fastify.decorate('passNextJsPageRequests', passNextJsPageRequestsDecorator);
fastify.decorate('nextServer', nextServer);

const nextProxyRequestHandler = function (request: FastifyRequest, reply: FastifyReply) {
const nextJsProxyRequestHandler = function (request: FastifyRequest, reply: FastifyReply) {
nextRequestHandler(proxyFastifyRawRequest(request), proxyFastifyRawReply(reply));
};

const nextRawRequestHandler = function (request: FastifyRequest, reply: FastifyReply) {
const nextJsRawRequestHandler = function (request: FastifyRequest, reply: FastifyReply) {
nextRequestHandler(request.raw, reply.raw);
};

fastify.decorate('nextJsProxyRequestHandler', nextJsProxyRequestHandler);
fastify.decorate('nextJsRawRequestHandler', nextJsRawRequestHandler);

fastify.addHook('onClose', function () {
return nextServer.close();
});
Expand Down

0 comments on commit 6acb083

Please sign in to comment.