diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index bb88700717836..fabcea980ecad 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -339,23 +339,31 @@ 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) { 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() - return handler(req, res) + return handler(req, res, parsedUrl) } } } diff --git a/test/integration/custom-server/server.js b/test/integration/custom-server/server.js index 41520a7bd7d1b..d1764616043e1 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 @@ -68,6 +69,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 57d00159cd86e..e94e0a33e07d1 100644 --- a/test/integration/custom-server/test/index.test.js +++ b/test/integration/custom-server/test/index.test.js @@ -123,6 +123,18 @@ describe.skip.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)