Skip to content

Commit 77bb102

Browse files
hristo-kanchevBrian Vaughn
authored and
Brian Vaughn
committed
[DevTools] [Profiler]: Save profile now working in Firefox (#16612)
* Added anchor dom element in order to successfully download profiling data. * Reworked downloadFile to accept a DOMElement in order for FF to successfully download profiling data. * Prettify downloadFile changes.
1 parent 92f094d commit 77bb102

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/react-devtools-shared/src/devtools/views/Profiler/ProfilingImportExportButtons.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function ProfilingImportExportButtons() {
2929
const {profilerStore} = store;
3030

3131
const inputRef = useRef<HTMLInputElement | null>(null);
32+
const downloadRef = useRef<HTMLAnchorElement | null>(null);
3233

3334
const {dispatch: modalDialogDispatch} = useContext(ModalDialogContext);
3435

@@ -38,7 +39,7 @@ export default function ProfilingImportExportButtons() {
3839
return;
3940
}
4041

41-
if (profilingData !== null) {
42+
if (profilingData !== null && downloadRef.current !== null) {
4243
const profilingDataExport = prepareProfilingDataExport(profilingData);
4344
const date = new Date();
4445
const dateString = date
@@ -54,6 +55,7 @@ export default function ProfilingImportExportButtons() {
5455
})
5556
.replace(/:/g, '-');
5657
downloadFile(
58+
downloadRef.current,
5759
`profiling-data.${dateString}.${timeString}.json`,
5860
JSON.stringify(profilingDataExport, null, 2),
5961
);
@@ -114,6 +116,7 @@ export default function ProfilingImportExportButtons() {
114116
onChange={handleFiles}
115117
tabIndex={-1}
116118
/>
119+
<a ref={downloadRef} className={styles.Input} />
117120
<Button
118121
disabled={isProfiling}
119122
onClick={uploadData}

packages/react-devtools-shared/src/devtools/views/utils.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ export function serializeHooksForCopy(hooks: HooksTree | null): string {
178178
// Without this, we would see a "Download failed: network error" failure.
179179
let downloadUrl = null;
180180

181-
export function downloadFile(filename: string, text: string): void {
181+
export function downloadFile(
182+
element: HTMLAnchorElement,
183+
filename: string,
184+
text: string,
185+
): void {
182186
const blob = new Blob([text], {type: 'text/plain;charset=utf-8'});
183187

184188
if (downloadUrl !== null) {
@@ -187,15 +191,10 @@ export function downloadFile(filename: string, text: string): void {
187191

188192
downloadUrl = URL.createObjectURL(blob);
189193

190-
const element = document.createElement('a');
191194
element.setAttribute('href', downloadUrl);
192195
element.setAttribute('download', filename);
193-
element.style.display = 'none';
194-
((document.body: any): HTMLBodyElement).appendChild(element);
195196

196197
element.click();
197-
198-
((document.body: any): HTMLBodyElement).removeChild(element);
199198
}
200199

201200
export function truncateText(text: string, maxLength: number): string {

0 commit comments

Comments
 (0)