Skip to content

Commit bc6184d

Browse files
authored
[devtools] Fix "View source" for sources with URLs that aren't normalized (facebook#32951)
1 parent ce578f9 commit bc6184d

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

packages/react-devtools-extensions/src/main/fetchFileWithCaching.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global chrome */
22

3-
import {normalizeUrl} from 'react-devtools-shared/src/utils';
3+
import {normalizeUrlIfValid} from 'react-devtools-shared/src/utils';
44
import {__DEBUG__} from 'react-devtools-shared/src/constants';
55

66
let debugIDCounter = 0;
@@ -117,7 +117,7 @@ async function fetchFileWithCaching(url: string): Promise<string> {
117117
chrome.devtools.inspectedWindow.getResources(r => resolve(r)),
118118
);
119119

120-
const normalizedReferenceURL = normalizeUrl(url);
120+
const normalizedReferenceURL = normalizeUrlIfValid(url);
121121
const resource = resources.find(r => r.url === normalizedReferenceURL);
122122

123123
if (resource != null) {

packages/react-devtools-extensions/src/main/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY,
1717
} from 'react-devtools-shared/src/constants';
1818
import {logEvent} from 'react-devtools-shared/src/Logger';
19+
import {normalizeUrlIfValid} from 'react-devtools-shared/src/utils';
1920

2021
import {
2122
setBrowserSelectionFromReact,
@@ -128,7 +129,11 @@ function createBridgeAndStore() {
128129
: source;
129130

130131
// We use 1-based line and column, Chrome expects them 0-based.
131-
chrome.devtools.panels.openResource(sourceURL, line - 1, column - 1);
132+
chrome.devtools.panels.openResource(
133+
normalizeUrlIfValid(sourceURL),
134+
line - 1,
135+
column - 1,
136+
);
132137
};
133138

134139
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.

packages/react-devtools-shared/src/symbolicateSource.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @flow
88
*/
99

10-
import {normalizeUrl} from 'react-devtools-shared/src/utils';
1110
import SourceMapConsumer from 'react-devtools-shared/src/hooks/SourceMapConsumer';
1211

1312
import type {Source} from 'react-devtools-shared/src/shared/types';
@@ -91,9 +90,8 @@ export async function symbolicateSource(
9190
try {
9291
// sourceMapURL = https://react.dev/script.js.map
9392
void new URL(possiblyURL); // test if it is a valid URL
94-
const normalizedURL = normalizeUrl(possiblyURL);
9593

96-
return {sourceURL: normalizedURL, line, column};
94+
return {sourceURL: possiblyURL, line, column};
9795
} catch (e) {
9896
// This is not valid URL
9997
if (

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,17 @@ export function backendToFrontendSerializedElementMapper(
996996
};
997997
}
998998

999-
// Chrome normalizes urls like webpack-internals:// but new URL don't, so cannot use new URL here.
1000-
export function normalizeUrl(url: string): string {
1001-
return url.replace('/./', '/');
999+
/**
1000+
* Should be used when treating url as a Chrome Resource URL.
1001+
*/
1002+
export function normalizeUrlIfValid(url: string): string {
1003+
try {
1004+
// TODO: Chrome will use the basepath to create a Resource URL.
1005+
return new URL(url).toString();
1006+
} catch {
1007+
// Giving up if it's not a valid URL without basepath
1008+
return url;
1009+
}
10021010
}
10031011

10041012
export function getIsReloadAndProfileSupported(): boolean {

0 commit comments

Comments
 (0)