Skip to content

[devtools] Add an endpoint to poll for server status #80005

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

Merged
merged 1 commit into from
Jun 2, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ export function getRestartDevServerMiddleware({
telemetry,
turbopackProject,
}: RestartDevServerMiddlewareConfig) {
return async function (
/**
* Some random value between 1 and Number.MAX_SAFE_INTEGER (inclusive). The same value is returned
* on every call to `__nextjs_server_status` until the server is restarted.
*
* Can be used to determine if two server status responses are from the same process or a
* different (restarted) process.
*/
const executionId: number =
Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1

async function handleRestartRequest(
req: IncomingMessage,
res: ServerResponse,
next: () => void
): Promise<void> {
const { pathname, searchParams } = new URL(`http://n${req.url}`)
if (pathname !== '/__nextjs_restart_dev' || req.method !== 'POST') {
return next()
searchParams: URLSearchParams
) {
if (req.method !== 'POST') {
return middlewareResponse.methodNotAllowed(res)
}

const invalidatePersistentCache = searchParams.has(
Expand All @@ -48,4 +57,31 @@ export function getRestartDevServerMiddleware({

return middlewareResponse.noContent(res)
}

async function handleServerStatus(req: IncomingMessage, res: ServerResponse) {
if (req.method !== 'GET') {
return middlewareResponse.methodNotAllowed(res)
}

return middlewareResponse.json(res, {
executionId,
})
}

return async function (
req: IncomingMessage,
res: ServerResponse,
next: () => void
): Promise<void> {
const { pathname, searchParams } = new URL(`http://n${req.url}`)

switch (pathname) {
case '/__nextjs_restart_dev':
return await handleRestartRequest(req, res, searchParams)
case '/__nextjs_server_status':
return await handleServerStatus(req, res)
default:
return next()
}
}
}
Loading