Unverified Commit 629c9db8 authored by Vladlen Grachev's avatar Vladlen Grachev Committed by GitHub

Fix broken request handler (#51939)

Hi!

In previous versions (13.4.2 and earlier) in the custom server I could
do the following:
```
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;

if (pathname === '/c') {
  await handle(req, res, parse('/b', true));
} else {
  await handle(req, res, parsedUrl);
}
```

Of course, you can replace `handle` with `app.render` in the attached
example, but in practice it is convenient to put the definition of
`parsedUrl` into middleware and substitute the necessary `parsedUrl` in
exceptional situations.

This was broken in #49805.

I'm not sure if this use of request handler is expected, but it has
always worked and people have used it (probably due to lack of
documentation about the difference between `app.render` and
`app.getRequestHandler()`). So the change looks breaking, and I don't
think it should appear in minor versions.

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent ff5338ce
...@@ -339,23 +339,31 @@ function createServer(options: NextServerOptions): NextServer { ...@@ -339,23 +339,31 @@ function createServer(options: NextServerOptions): NextServer {
case 'getRequestHandler': { case 'getRequestHandler': {
return () => { return () => {
let handler: RequestHandler let handler: RequestHandler
return async (req: IncomingMessage, res: ServerResponse) => { return async (
req: IncomingMessage,
res: ServerResponse,
parsedUrl?: UrlWithParsedQuery
) => {
if (shouldUseStandaloneMode) { if (shouldUseStandaloneMode) {
setupWebSocketHandler(options.httpServer, req) setupWebSocketHandler(options.httpServer, req)
const parsedUrl = url.parse( const proxyParsedUrl = url.parse(
`http://127.0.0.1:${serverPort}${req.url}`, `http://127.0.0.1:${serverPort}${req.url}`,
true true
) )
if ((req?.socket as TLSSocket)?.encrypted) { if ((req?.socket as TLSSocket)?.encrypted) {
req.headers['x-forwarded-proto'] = 'https' 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 return
} }
handler = handler || server.getRequestHandler() handler = handler || server.getRequestHandler()
return handler(req, res) return handler(req, res, parsedUrl)
} }
} }
} }
......
...@@ -7,6 +7,7 @@ if (process.env.POLYFILL_FETCH) { ...@@ -7,6 +7,7 @@ if (process.env.POLYFILL_FETCH) {
const { readFileSync } = require('fs') const { readFileSync } = require('fs')
const next = require('next') const next = require('next')
const { join } = require('path') const { join } = require('path')
const { parse } = require('url')
const dev = process.env.NODE_ENV !== 'production' const dev = process.env.NODE_ENV !== 'production'
const dir = __dirname const dir = __dirname
...@@ -68,6 +69,10 @@ app.prepare().then(() => { ...@@ -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) handleNextRequests(req, res)
}) })
......
...@@ -123,6 +123,18 @@ describe.skip.each([ ...@@ -123,6 +123,18 @@ describe.skip.each([
expect(html).toMatch(/made it to dashboard/) 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 () => { it('should contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(nextUrl, '/', undefined, { agent }) const html = await renderViaHTTP(nextUrl, '/', undefined, { agent })
const $ = cheerio.load(html) const $ = cheerio.load(html)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment