Skip to content

Commit

Permalink
Use fastify.register with prefix for routing
Browse files Browse the repository at this point in the history
  • Loading branch information
applicazza committed Jun 27, 2021
1 parent b36bf8c commit 3b227b1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 51 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@typescript-eslint/parser": "^4.27.0",
"axios": "^0.21.1",
"esbuild": "^0.12.9",
"esbuild-node-externals": "^1.2.0",
"esbuild-node-externals": "^1.3.0",
"eslint": "^7.29.0",
"fastify": "^3.18.0",
"fastify-plugin": "^3.0.0",
Expand Down Expand Up @@ -52,5 +52,5 @@
"test": "jest --coverage"
},
"types": "dist/index.d.ts",
"version": "0.0.6"
"version": "0.1.0"
}
97 changes: 54 additions & 43 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
dev,
});

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

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

const passNextJsRequestsDecorator = () => {
fastify.passNextJsDataRequests();
Expand All @@ -61,85 +67,89 @@ const fastifyNextJs: FastifyPluginAsync<FastifyNextJsOptions> = async (fastify,
}

fastify.passNextJsPageRequests();

};

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

const passNextJsDevRequestsDecorator = () => {
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,
fastify.register((fastify, _, done) => {
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: '/static/*',
handler: nextJsRawRequestHandler
});
fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: '/webpack-hmr',
handler: nextJsRawRequestHandler
});
done();
}, {
prefix: `${basePath}/_next`
});
};

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

const passNextJsStaticRequestsDecorator = () => {
fastify.register(fastifyStatic, {
prefix: '${basePath}/_next/static/',
prefix: `${basePath}/_next/static/`,
root: `${process.cwd()}/.next/static`,
decorateReply: false,
});
};

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

fastify.route({
method: ['GET', 'HEAD', 'OPTIONS'],
url: `${basePath}/*`,
handler: nextJsProxyRequestHandler,
done();
}, {
prefix: basePath || '/'
});
};
fastify.decorate('passNextJsRequests', passNextJsRequestsDecorator);

fastify.decorate('nextJsProxyRequestHandler', nextJsProxyRequestHandler);
fastify.decorate('nextJsRawRequestHandler', nextJsRawRequestHandler);
fastify.decorate('nextServer', nextServer);
fastify.decorate('passNextJsDataRequests', passNextJsDataRequestsDecorator);
fastify.decorate('passNextJsDevRequests', passNextJsDevRequestsDecorator);
fastify.decorate('passNextJsImageRequests', passNextJsImageRequestsDecorator);
fastify.decorate('passNextJsStaticRequests', passNextJsStaticRequestsDecorator);
fastify.decorate('passNextJsPageRequests', passNextJsPageRequestsDecorator);
fastify.decorate('nextServer', nextServer);

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

const nextJsRawRequestHandler = function (request: FastifyRequest, reply: FastifyReply) {
nextRequestHandler(request.raw, reply.raw);
};
fastify.decorate('passNextJsRequests', passNextJsRequestsDecorator);
fastify.decorate('passNextJsStaticRequests', passNextJsStaticRequestsDecorator);

fastify.decorate('nextJsProxyRequestHandler', nextJsProxyRequestHandler);
fastify.decorate('nextJsRawRequestHandler', nextJsRawRequestHandler);
await nextServer.prepare();

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

await nextServer.prepare();
};

const proxyFastifyRawRequest = (request: FastifyRequest) => {
Expand Down Expand Up @@ -205,4 +215,5 @@ const proxyFastifyRawReply = (reply: FastifyReply) => {

export default fastifyPlugin(fastifyNextJs, {
fastify: '3.x',
name: '@applicazza/fastify-nextjs'
});
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ __metadata:
"@typescript-eslint/parser": ^4.27.0
axios: ^0.21.1
esbuild: ^0.12.9
esbuild-node-externals: ^1.2.0
esbuild-node-externals: ^1.3.0
eslint: ^7.29.0
fastify: ^3.18.0
fastify-plugin: ^3.0.0
Expand Down Expand Up @@ -2573,15 +2573,15 @@ __metadata:
languageName: node
linkType: hard

"esbuild-node-externals@npm:^1.2.0":
version: 1.2.0
resolution: "esbuild-node-externals@npm:1.2.0"
"esbuild-node-externals@npm:^1.3.0":
version: 1.3.0
resolution: "esbuild-node-externals@npm:1.3.0"
dependencies:
find-up: 5.0.0
tslib: 2.1.0
peerDependencies:
esbuild: ^0.11.0
checksum: c5c6681b83d13486ae5f469f7cae5de9252ab26912002223958f2eed2641a6b20ff3c8a3dbc1bff27c271489004e66f4529eaa08e3e29c429b901fea0d610aa5
esbuild: ^0.12.0
checksum: 03a10a2807906c243c38935f758b3db47e4b2257d58101bdd6f027425edc9b522820f602f27a1fec89e6b2aa3672eb44cbf04b3be4bdcc10346e383b00a901d3
languageName: node
linkType: hard

Expand Down

0 comments on commit 3b227b1

Please sign in to comment.