-
Notifications
You must be signed in to change notification settings - Fork 28.5k
[devtools] Add a very minimal API for restarting the dev server #79265
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ages/next/src/client/components/react-dev-overlay/server/restart-dev-server-middleware.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ServerResponse, IncomingMessage } from 'http' | ||
import type { Telemetry } from '../../../../telemetry/storage' | ||
import { RESTART_EXIT_CODE } from '../../../../server/lib/utils' | ||
import { middlewareResponse } from './middleware-response' | ||
|
||
const EVENT_DEV_OVERLAY_RESTART_SERVER = 'DEV_OVERLAY_RESTART_SERVER' | ||
|
||
export function getRestartDevServerMiddleware(telemetry: Telemetry) { | ||
return async function ( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
next: () => void | ||
): Promise<void> { | ||
const { pathname } = new URL(`http://n${req.url}`) | ||
if (pathname !== '/__nextjs_restart_dev' || req.method !== 'POST') { | ||
return next() | ||
} | ||
|
||
telemetry.record({ | ||
eventName: EVENT_DEV_OVERLAY_RESTART_SERVER, | ||
payload: {}, | ||
}) | ||
|
||
// TODO: Use flushDetached | ||
await telemetry.flush() | ||
|
||
// do this async to try to give the response a chance to send | ||
// it's not really important if it doesn't though | ||
setTimeout(() => { | ||
process.exit(RESTART_EXIT_CODE) | ||
}, 0) | ||
|
||
return middlewareResponse.noContent(res) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's flush detached? Sounds like we do want to ensure everything is flushed before we exit and detached makes it sound like we wouldn't care if it doesn't flush?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Telemetry is best-effort. We shouldn't generate an error if we fail to submit it. However, this is different than "we wouldn't care if it doesn't flush".
Normally when the
next dev
server exits, we don't want to block on submitting telemetry (the network request can be slow), so we write telemetry to a file and spawn a detached process that we can continue submitting telemetry in the background and exit the main process quickly.This sort of thing is a common trick for CLIs that have slow exits, e.g.: https://github.com/rui314/mold/blob/main/docs/design.md#details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So flush detached actually means it's more likely that we submit telemetry. Thanks for clarifying.