@@ -15,14 +15,23 @@ export function getRestartDevServerMiddleware({
15
15
telemetry,
16
16
turbopackProject,
17
17
} : RestartDevServerMiddlewareConfig ) {
18
- return async function (
18
+ /**
19
+ * Some random value between 1 and Number.MAX_SAFE_INTEGER (inclusive). The same value is returned
20
+ * on every call to `__nextjs_server_status` until the server is restarted.
21
+ *
22
+ * Can be used to determine if two server status responses are from the same process or a
23
+ * different (restarted) process.
24
+ */
25
+ const executionId : number =
26
+ Math . floor ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) + 1
27
+
28
+ async function handleRestartRequest (
19
29
req : IncomingMessage ,
20
30
res : ServerResponse ,
21
- next : ( ) => void
22
- ) : Promise < void > {
23
- const { pathname, searchParams } = new URL ( `http://n${ req . url } ` )
24
- if ( pathname !== '/__nextjs_restart_dev' || req . method !== 'POST' ) {
25
- return next ( )
31
+ searchParams : URLSearchParams
32
+ ) {
33
+ if ( req . method !== 'POST' ) {
34
+ return middlewareResponse . methodNotAllowed ( res )
26
35
}
27
36
28
37
const invalidatePersistentCache = searchParams . has (
@@ -48,4 +57,31 @@ export function getRestartDevServerMiddleware({
48
57
49
58
return middlewareResponse . noContent ( res )
50
59
}
60
+
61
+ async function handleServerStatus ( req : IncomingMessage , res : ServerResponse ) {
62
+ if ( req . method !== 'GET' ) {
63
+ return middlewareResponse . methodNotAllowed ( res )
64
+ }
65
+
66
+ return middlewareResponse . json ( res , {
67
+ executionId,
68
+ } )
69
+ }
70
+
71
+ return async function (
72
+ req : IncomingMessage ,
73
+ res : ServerResponse ,
74
+ next : ( ) => void
75
+ ) : Promise < void > {
76
+ const { pathname, searchParams } = new URL ( `http://n${ req . url } ` )
77
+
78
+ switch ( pathname ) {
79
+ case '/__nextjs_restart_dev' :
80
+ return await handleRestartRequest ( req , res , searchParams )
81
+ case '/__nextjs_server_status' :
82
+ return await handleServerStatus ( req , res )
83
+ default :
84
+ return next ( )
85
+ }
86
+ }
51
87
}
0 commit comments