diff --git a/package.json b/package.json index f2f76b5..9b8abd3 100644 --- a/package.json +++ b/package.json @@ -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", @@ -52,5 +52,5 @@ "test": "jest --coverage" }, "types": "dist/index.d.ts", - "version": "0.0.6" + "version": "0.1.0" } diff --git a/src/index.ts b/src/index.ts index 266868a..0df2847 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,7 +48,13 @@ const fastifyNextJs: FastifyPluginAsync = 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(); @@ -61,85 +67,89 @@ const fastifyNextJs: FastifyPluginAsync = 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) => { @@ -205,4 +215,5 @@ const proxyFastifyRawReply = (reply: FastifyReply) => { export default fastifyPlugin(fastifyNextJs, { fastify: '3.x', + name: '@applicazza/fastify-nextjs' }); diff --git a/yarn.lock b/yarn.lock index 3a2deb8..df5b44a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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