Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken request handler #51939

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/custom-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})

Expand Down
12 changes: 12 additions & 0 deletions test/integration/custom-server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading