feat: Add pretty print toggle for request and response bodies in network details#39682
Open
cpAdm wants to merge 1 commit intomicrosoft:mainfrom
Open
feat: Add pretty print toggle for request and response bodies in network details#39682cpAdm wants to merge 1 commit intomicrosoft:mainfrom
cpAdm wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a user-facing “Pretty print” toggle in Trace Viewer’s Network details to switch between raw and formatted request/response bodies, with persisted settings and an error indicator when formatting fails.
Changes:
- Introduces pretty-print toggles for request payload (header button) and response body (bottom toolbar) with localStorage-backed persistence.
- Refactors the existing “error dot” indicator into a reusable
ToolbarButtonerrorBadgeprop and applies it to the UI mode output toggle. - Updates UI-mode tests to assert copying uses the original (unformatted) request body and validates the new request-body toggle behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/playwright-test/ui-mode-test-network-tab.spec.ts | Extends request-body formatting tests and adjusts copy-request expectations to use raw body. |
| packages/web/src/components/toolbarButton.tsx | Adds errorBadge support to show an error indicator on toolbar buttons. |
| packages/web/src/components/toolbarButton.css | Styles the new toolbar-button error badge and makes buttons positioning-relative. |
| packages/web/src/components/toolbar.css | Updates shadow styling selector to exclude .no-shadow toolbars via :not(...). |
| packages/trace-viewer/src/ui/uiModeView.tsx | Switches output error indicator to the new ToolbarButton errorBadge prop. |
| packages/trace-viewer/src/ui/networkResourceDetails.tsx | Adds pretty-print toggles for request/response bodies and centralizes formatting logic in useFormattedBody. |
| packages/trace-viewer/src/ui/networkResourceDetails.css | Adds response bottom toolbar styling and response-body sizing rule. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+191
to
+193
| // Untoggle pretty print to see original request body | ||
| await payloadPanel.getByRole('button', { name: 'Pretty print', exact: true }).click(); | ||
| await expect(payloadPanel.locator('.CodeMirror-code .CodeMirror-line')).toHaveText([ |
Comment on lines
+324
to
+332
| function formatBody(body: string, contentType?: string): string { | ||
| if (!contentType) | ||
| return body; | ||
|
|
||
| const bodyStr = body; | ||
| if (bodyStr === '') | ||
| return '<Empty>'; | ||
| if (isJsonMimeType(contentType)) | ||
| return JSON.stringify(JSON.parse(body), null, 2); | ||
|
|
||
| if (isJsonMimeType(contentType)) { | ||
| try { | ||
| return JSON.stringify(JSON.parse(bodyStr), null, 2); | ||
| } catch (err) { | ||
| return bodyStr; | ||
| } | ||
| } | ||
|
|
||
| if (isXmlMimeType(contentType)) { | ||
| try { | ||
| return formatXml(bodyStr); | ||
| } catch { | ||
| return bodyStr; | ||
| } | ||
| } | ||
| if (isXmlMimeType(contentType)) | ||
| return formatXml(body); |
Comment on lines
+78
to
+80
| .network-response-body { | ||
| height: calc(100% - 31px /* Height of bottom toolbar */); | ||
| } |
Comment on lines
+235
to
+248
| const [showFormattedResponse, setShowFormattedResponse] = useSetting('trace-viewer-network-details-show-formatted-response', true); | ||
| const formatResult = useFormattedBody(responseBody, showFormattedResponse); | ||
|
|
||
| return <div className='vbox network-request-details-tab'> | ||
| {!resource.response.content._sha1 && <div>Response body is not available for this request.</div>} | ||
| {responseBody && responseBody.font && <FontPreview font={responseBody.font} />} | ||
| {responseBody && responseBody.dataUrl && <div><img draggable='false' src={responseBody.dataUrl} /></div>} | ||
| {responseBody && responseBody.text && <CodeMirrorWrapper text={responseBody.text} mimeType={responseBody.mimeType} readOnly lineNumbers={true}/>} | ||
| {responseBody && responseBody.text !== undefined && <div className='network-response-body'> | ||
| <CodeMirrorWrapper text={formatResult.text} mimeType={responseBody.mimeType} readOnly lineNumbers={true}/> | ||
| <Toolbar noShadow={true} noMinHeight={true} className='network-response-toolbar'> | ||
| <div style={{ margin: 'auto' }}></div> | ||
| <FormatToggleButton toggled={showFormattedResponse} error={formatResult.error} onToggle={() => setShowFormattedResponse(!showFormattedResponse)} /> | ||
| </Toolbar> | ||
| </div>} |
Comment on lines
58
to
63
| aria-label={ariaLabel || title} | ||
| > | ||
| {icon && <span className={`codicon codicon-${icon}`} style={children ? { marginRight: 5 } : {}}></span>} | ||
| {children} | ||
| {errorBadge && <span className='toolbar-button-error-badge' title={errorBadge}></span>} | ||
| </button>; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow up on #39405,
uiModeView.tsx(extracted toToolbarButton)Loading...which in most cases just causing flickering anyway (since most bodies are fetched very quickly)<Empty>, but to line up with e.g. Chrome Devtools, we now just show an empty editor to avoid confusionIf formatting fails:

Example test to showcase this new feature
If we want, we can extract
useFormattedBodyand its helper functions to a separate file as suggested here?Closes: #37963