Skip to content

Commit b312571

Browse files
author
Brian Vaughn
committed
Removed named hooks prefetch step since we are using cached network responses.
1 parent 407574e commit b312571

File tree

4 files changed

+38
-130
lines changed

4 files changed

+38
-130
lines changed

packages/react-devtools-inline/src/hookNames.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
import {
44
parseHookNames,
55
parseSourceAndMetadata,
6-
prefetchSourceFiles,
76
purgeCachedMetadata,
87
} from 'react-devtools-shared/src/hooks/parseHookNames';
98

10-
export {
11-
parseHookNames,
12-
parseSourceAndMetadata,
13-
prefetchSourceFiles,
14-
purgeCachedMetadata,
15-
};
9+
export {parseHookNames, parseSourceAndMetadata, purgeCachedMetadata};

packages/react-devtools-shared/src/devtools/views/Components/InspectedElementContext.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ export function InspectedElementContextController({children}: Props) {
116116
setParseHookNames(parseHookNamesByDefault || alreadyLoadedHookNames);
117117
}
118118

119-
const prefetchSourceFilesRef = useRef(null);
120119
const purgeCachedMetadataRef = useRef(null);
121120

122121
// Don't load a stale element from the backend; it wastes bridge bandwidth.
@@ -132,12 +131,10 @@ export function InspectedElementContextController({children}: Props) {
132131
if (hookNamesModule !== null) {
133132
const {
134133
parseHookNames: loadHookNamesFunction,
135-
prefetchSourceFiles,
136134
purgeCachedMetadata,
137135
} = hookNamesModule;
138136

139137
purgeCachedMetadataRef.current = purgeCachedMetadata;
140-
prefetchSourceFilesRef.current = prefetchSourceFiles;
141138

142139
if (
143140
inspectedElement !== null &&
@@ -185,11 +182,6 @@ export function InspectedElementContextController({children}: Props) {
185182
inspectedElementRef.current !== inspectedElement
186183
) {
187184
inspectedElementRef.current = inspectedElement;
188-
189-
const prefetchSourceFiles = prefetchSourceFilesRef.current;
190-
if (typeof prefetchSourceFiles === 'function') {
191-
prefetchSourceFiles(inspectedElement.hooks, fetchFileWithCaching);
192-
}
193185
}
194186
}, [inspectedElement]);
195187

packages/react-devtools-shared/src/hooks/parseHookNames/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@ import type {FetchFileWithCaching} from 'react-devtools-shared/src/devtools/view
1515
import {withAsyncPerformanceMark} from 'react-devtools-shared/src/PerformanceMarks';
1616
import WorkerizedParseSourceAndMetadata from './parseSourceAndMetadata.worker';
1717
import typeof * as ParseSourceAndMetadataModule from './parseSourceAndMetadata';
18-
import {
19-
flattenHooksList,
20-
loadSourceAndMetadata,
21-
prefetchSourceFiles,
22-
} from './loadSourceAndMetadata';
18+
import {flattenHooksList, loadSourceAndMetadata} from './loadSourceAndMetadata';
2319

2420
const workerizedParseHookNames: ParseSourceAndMetadataModule = WorkerizedParseSourceAndMetadata();
2521

26-
export {prefetchSourceFiles};
27-
2822
export function parseSourceAndMetadata(
2923
hooksList: Array<HooksNode>,
3024
locationKeyToHookSourceAndMetadata: Map<string, HookSourceAndMetadata>,

packages/react-devtools-shared/src/hooks/parseHookNames/loadSourceAndMetadata.js

Lines changed: 36 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
// This is the fastest option since our custom metadata file is much smaller than a full source map,
4646
// and there is no need to convert runtime code to the original source.
4747

48-
import LRU from 'lru-cache';
4948
import {__DEBUG__} from 'react-devtools-shared/src/constants';
5049
import {getHookSourceLocationKey} from 'react-devtools-shared/src/hookNamesCache';
5150
import {sourceMapIncludesSource} from '../SourceMapUtils';
@@ -55,7 +54,6 @@ import {
5554
withSyncPerformanceMark,
5655
} from 'react-devtools-shared/src/PerformanceMarks';
5756

58-
import type {LRUCache} from 'react-devtools-shared/src/types';
5957
import type {
6058
HooksNode,
6159
HookSource,
@@ -70,14 +68,6 @@ const FETCH_OPTIONS = {cache: 'force-cache'};
7068

7169
const MAX_SOURCE_LENGTH = 100_000_000;
7270

73-
// Fetch requests originated from an extension might not have origin headers
74-
// which may prevent subsequent requests from using cached responses
75-
// if the server returns a Vary: 'Origin' header
76-
// so this cache will temporarily store pre-fetches sources in memory.
77-
const prefetchedSources: LRUCache<string, string> = new LRU({
78-
max: 15,
79-
});
80-
8171
export type HookSourceAndMetadata = {|
8272
// Generated by react-debug-tools.
8373
hookSource: HookSource,
@@ -462,109 +452,47 @@ function loadSourceFiles(
462452
locationKeyToHookSourceAndMetadata.forEach(hookSourceAndMetadata => {
463453
const {runtimeSourceURL} = hookSourceAndMetadata;
464454

465-
const prefetchedSourceCode = prefetchedSources.get(runtimeSourceURL);
466-
if (prefetchedSourceCode != null) {
467-
hookSourceAndMetadata.runtimeSourceCode = prefetchedSourceCode;
468-
} else {
469-
let fetchFileFunction = fetchFile;
470-
if (fetchFileWithCaching != null) {
471-
// If a helper function has been injected to fetch with caching,
472-
// use it to fetch the (already loaded) source file.
473-
fetchFileFunction = url => {
474-
return withAsyncPerformanceMark(
475-
`fetchFileWithCaching("${url}")`,
476-
() => {
477-
return ((fetchFileWithCaching: any): FetchFileWithCaching)(url);
478-
},
479-
);
480-
};
481-
}
455+
let fetchFileFunction = fetchFile;
456+
if (fetchFileWithCaching != null) {
457+
// If a helper function has been injected to fetch with caching,
458+
// use it to fetch the (already loaded) source file.
459+
fetchFileFunction = url => {
460+
return withAsyncPerformanceMark(
461+
`fetchFileWithCaching("${url}")`,
462+
() => {
463+
return ((fetchFileWithCaching: any): FetchFileWithCaching)(url);
464+
},
465+
);
466+
};
467+
}
482468

483-
const fetchPromise =
484-
dedupedFetchPromises.get(runtimeSourceURL) ||
485-
fetchFileFunction(runtimeSourceURL).then(runtimeSourceCode => {
486-
// TODO (named hooks) Re-think this; the main case where it matters is when there's no source-maps,
487-
// because then we need to parse the full source file as an AST.
488-
if (runtimeSourceCode.length > MAX_SOURCE_LENGTH) {
489-
throw Error('Source code too large to parse');
490-
}
469+
const fetchPromise =
470+
dedupedFetchPromises.get(runtimeSourceURL) ||
471+
fetchFileFunction(runtimeSourceURL).then(runtimeSourceCode => {
472+
// TODO (named hooks) Re-think this; the main case where it matters is when there's no source-maps,
473+
// because then we need to parse the full source file as an AST.
474+
if (runtimeSourceCode.length > MAX_SOURCE_LENGTH) {
475+
throw Error('Source code too large to parse');
476+
}
491477

492-
if (__DEBUG__) {
493-
console.groupCollapsed(
494-
`loadSourceFiles() runtimeSourceURL "${runtimeSourceURL}"`,
495-
);
496-
console.log(runtimeSourceCode);
497-
console.groupEnd();
498-
}
478+
if (__DEBUG__) {
479+
console.groupCollapsed(
480+
`loadSourceFiles() runtimeSourceURL "${runtimeSourceURL}"`,
481+
);
482+
console.log(runtimeSourceCode);
483+
console.groupEnd();
484+
}
499485

500-
return runtimeSourceCode;
501-
});
502-
dedupedFetchPromises.set(runtimeSourceURL, fetchPromise);
486+
return runtimeSourceCode;
487+
});
488+
dedupedFetchPromises.set(runtimeSourceURL, fetchPromise);
503489

504-
setterPromises.push(
505-
fetchPromise.then(runtimeSourceCode => {
506-
hookSourceAndMetadata.runtimeSourceCode = runtimeSourceCode;
507-
}),
508-
);
509-
}
490+
setterPromises.push(
491+
fetchPromise.then(runtimeSourceCode => {
492+
hookSourceAndMetadata.runtimeSourceCode = runtimeSourceCode;
493+
}),
494+
);
510495
});
511496

512497
return Promise.all(setterPromises);
513498
}
514-
515-
export function prefetchSourceFiles(
516-
hooksTree: HooksTree,
517-
fetchFileWithCaching: FetchFileWithCaching | null,
518-
): void {
519-
// Deduplicate fetches, since there can be multiple location keys per source map.
520-
const dedupedFetchPromises = new Set();
521-
522-
let fetchFileFunction = null;
523-
if (fetchFileWithCaching != null) {
524-
// If a helper function has been injected to fetch with caching,
525-
// use it to fetch the (already loaded) source file.
526-
fetchFileFunction = url => {
527-
return withAsyncPerformanceMark(
528-
`[pre] fetchFileWithCaching("${url}")`,
529-
() => {
530-
return ((fetchFileWithCaching: any): FetchFileWithCaching)(url);
531-
},
532-
);
533-
};
534-
} else {
535-
fetchFileFunction = url => fetchFile(url, '[pre] fetchFile');
536-
}
537-
538-
const hooksQueue = Array.from(hooksTree);
539-
540-
for (let i = 0; i < hooksQueue.length; i++) {
541-
const hook = hooksQueue.pop();
542-
if (isUnnamedBuiltInHook(hook)) {
543-
continue;
544-
}
545-
546-
const hookSource = hook.hookSource;
547-
if (hookSource == null) {
548-
continue;
549-
}
550-
551-
const runtimeSourceURL = ((hookSource.fileName: any): string);
552-
553-
if (prefetchedSources.has(runtimeSourceURL)) {
554-
// If we've already fetched this source, skip it.
555-
continue;
556-
}
557-
558-
if (!dedupedFetchPromises.has(runtimeSourceURL)) {
559-
dedupedFetchPromises.add(runtimeSourceURL);
560-
561-
fetchFileFunction(runtimeSourceURL).then(text => {
562-
prefetchedSources.set(runtimeSourceURL, text);
563-
});
564-
}
565-
566-
if (hook.subHooks.length > 0) {
567-
hooksQueue.push(...hook.subHooks);
568-
}
569-
}
570-
}

0 commit comments

Comments
 (0)