Skip to content

Commit

Permalink
Add passNextJsImageRequests method and use fastify.route instead …
Browse files Browse the repository at this point in the history
…of `fastify.all`
  • Loading branch information
applicazza committed Jun 24, 2021
1 parent 697a2ad commit 8f8650c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fastify.register(fastifyNextJs, {

await fastify.after();

fastify.passNextJsDataRequests();
fastify.passNextJsDataRequests();
if (dev) {
fastify.passNextJsDevRequests();
Expand Down Expand Up @@ -93,6 +94,7 @@ interface FastifyInstance {
nextJsRawRequestHandler: (request: FastifyRequest, reply: FastifyReply) => void;
nextServer: NextServer;
passNextJsRequests: () => void;
passNextJsImageRequests: () => void;
passNextJsDataRequests: () => void;
passNextJsDevRequests: () => void;
passNextJsPageRequests: () => 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.5"
"version": "0.0.6"
}
41 changes: 37 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module 'fastify' {
passNextJsRequests: () => void;
passNextJsDataRequests: () => void;
passNextJsDevRequests: () => void;
passNextJsImageRequests: () => void;
passNextJsPageRequests: () => void;
passNextJsStaticRequests: () => void;
}
Expand Down Expand Up @@ -51,6 +52,7 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,

const passNextJsRequestsDecorator = () => {
fastify.passNextJsDataRequests();
fastify.passNextJsImageRequests();

if (dev) {
fastify.passNextJsDevRequests();
Expand All @@ -63,11 +65,32 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
};

const passNextJsDataRequestsDecorator = () => {
fastify.get(`${basePath}/_next/data/*`, nextJsProxyRequestHandler);
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/_next/data/*`,
handler: nextJsProxyRequestHandler,
});
};

const passNextJsDevRequestsDecorator = () => {
fastify.all(`${basePath}/_next/*`, nextJsRawRequestHandler);
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/_next/static/*`,
handler: nextJsRawRequestHandler,
});
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/_next/webpack-hmr`,
handler: nextJsRawRequestHandler,
});
};

const passNextJsImageRequestsDecorator = () => {
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/_next/image`,
handler: nextJsRawRequestHandler,
});
};

const passNextJsStaticRequestsDecorator = () => {
Expand All @@ -80,13 +103,23 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,

const passNextJsPageRequestsDecorator = () => {
if (basePath) {
fastify.all(`${basePath}`, nextJsProxyRequestHandler);
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}`,
handler: nextJsProxyRequestHandler,
});
}
fastify.all(`${basePath}/*`, nextJsProxyRequestHandler);

fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/*`,
handler: nextJsProxyRequestHandler,
});
};
fastify.decorate('passNextJsRequests', passNextJsRequestsDecorator);
fastify.decorate('passNextJsDataRequests', passNextJsDataRequestsDecorator);
fastify.decorate('passNextJsDevRequests', passNextJsDevRequestsDecorator);
fastify.decorate('passNextJsImageRequests', passNextJsImageRequestsDecorator);
fastify.decorate('passNextJsStaticRequests', passNextJsStaticRequestsDecorator);
fastify.decorate('passNextJsPageRequests', passNextJsPageRequestsDecorator);
fastify.decorate('nextServer', nextServer);
Expand Down

0 comments on commit 8f8650c

Please sign in to comment.