From 5b54bc8f9ba2e5ed908e2918a565c86da3756e0d Mon Sep 17 00:00:00 2001 From: gwer Date: Wed, 28 Jun 2023 19:55:59 +0300 Subject: [PATCH 1/3] Fix broken request handler --- packages/next/src/server/next.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index 97023cd0349f7..69c6b5ab8404c 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -319,13 +319,17 @@ function createServer(options: NextServerOptions): NextServer { case 'getRequestHandler': { return () => { let handler: RequestHandler - return async (req: IncomingMessage, res: ServerResponse) => { + return async ( + req: IncomingMessage, + res: ServerResponse, + parsedUrl?: UrlWithParsedQuery + ) => { if (shouldUseStandaloneMode) { const standaloneHandler = await handlerPromise return standaloneHandler(req, res) } handler = handler || server.getRequestHandler() - return handler(req, res) + return handler(req, res, parsedUrl) } } } From c80ba771f3061927feef002dc76e4b84b1e128cb Mon Sep 17 00:00:00 2001 From: gwer Date: Wed, 26 Jul 2023 21:28:29 +0300 Subject: [PATCH 2/3] Add tests --- test/integration/custom-server/server.js | 5 +++++ test/integration/custom-server/test/index.test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/test/integration/custom-server/server.js b/test/integration/custom-server/server.js index 7e9f15df9303c..521b73f89d846 100644 --- a/test/integration/custom-server/server.js +++ b/test/integration/custom-server/server.js @@ -7,6 +7,7 @@ if (process.env.POLYFILL_FETCH) { const { readFileSync } = require('fs') const next = require('next') const { join } = require('path') +const { parse } = require('url') const dev = process.env.NODE_ENV !== 'production' const dir = __dirname @@ -64,6 +65,10 @@ app.prepare().then(() => { } } + if (/custom-url-with-request-handler/.test(req.url)) { + return handleNextRequests(req, res, parse('/dashboard', true)) + } + handleNextRequests(req, res) }) diff --git a/test/integration/custom-server/test/index.test.js b/test/integration/custom-server/test/index.test.js index 698513366f4d1..b7c8466632047 100644 --- a/test/integration/custom-server/test/index.test.js +++ b/test/integration/custom-server/test/index.test.js @@ -121,6 +121,18 @@ describe.each([ expect(html).toMatch(/made it to dashboard/) }) + it('should handle custom urls with requests handler', async () => { + const html = await renderViaHTTP( + nextUrl, + '/custom-url-with-request-handler', + undefined, + { + agent, + } + ) + expect(html).toMatch(/made it to dashboard/) + }) + it('should contain customServer in NEXT_DATA', async () => { const html = await renderViaHTTP(nextUrl, '/', undefined, { agent }) const $ = cheerio.load(html) From d81d68305f32ca6260a9e63e1b704176a31db2b3 Mon Sep 17 00:00:00 2001 From: gwer Date: Thu, 27 Jul 2023 12:33:04 +0300 Subject: [PATCH 3/3] Fix lint --- packages/next/src/server/next.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index 50b5343798710..fabcea980ecad 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -346,16 +346,20 @@ function createServer(options: NextServerOptions): NextServer { ) => { if (shouldUseStandaloneMode) { setupWebSocketHandler(options.httpServer, req) - const parsedUrl = url.parse( + const proxyParsedUrl = url.parse( `http://127.0.0.1:${serverPort}${req.url}`, true ) if ((req?.socket as TLSSocket)?.encrypted) { req.headers['x-forwarded-proto'] = 'https' } - addRequestMeta(req, '__NEXT_INIT_QUERY', parsedUrl.query) + addRequestMeta( + req, + '__NEXT_INIT_QUERY', + proxyParsedUrl.query + ) - await proxyRequest(req, res, parsedUrl, undefined, req) + await proxyRequest(req, res, proxyParsedUrl, undefined, req) return } handler = handler || server.getRequestHandler()