[go: nahoru, domu]

Skip to content

Commit

Permalink
findLastDocumentForUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Aug 9, 2023
1 parent 6cf368b commit 86b9602
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

/** @type {LH.Config.Json} */
/** @type {LH.Config} */
const config = {
extends: 'lighthouse:default',
settings: {
Expand Down
10 changes: 3 additions & 7 deletions core/computed/main-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {makeComputedArtifact} from './computed-artifact.js';
import {NetworkRecords} from './network-records.js';
import UrlUtils from '../lib/url-utils.js';
import {NetworkAnalyzer} from '../lib/dependency-graph/simulator/network-analyzer.js';

/**
* @fileoverview This artifact identifies the main resource on the page. Current solution assumes
Expand All @@ -21,18 +21,14 @@ class MainResource {
static async compute_(data, context) {
const {mainDocumentUrl} = data.URL;
if (!mainDocumentUrl) throw new Error('mainDocumentUrl must exist to get the main resource');
const requests = await NetworkRecords.request(data.devtoolsLog, context);
const records = await NetworkRecords.request(data.devtoolsLog, context);

const mainResourceRequests = requests.filter(request =>
request.resourceType === 'Document' &&
UrlUtils.equalWithExcludedFragments(request.url, mainDocumentUrl)
);
// We could have more than one record matching the main doucment url,
// if the page did `location.reload()`. Since `mainDocumentUrl` refers to the _last_
// document request, we should return the last candidate here. Besides, the browser
// would have evicted the first request by the time `MainDocumentRequest` (a consumer
// of this computed artifact) attempts to fetch the contents, resulting in a protocol error.
const mainResource = mainResourceRequests[mainResourceRequests.length - 1];
const mainResource = NetworkAnalyzer.findLastDocumentForUrl(records, mainDocumentUrl);
if (!mainResource) {
throw new Error('Unable to identify the main resource');
}
Expand Down
17 changes: 16 additions & 1 deletion core/lib/dependency-graph/simulator/network-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,24 @@ class NetworkAnalyzer {
);
}

/**
* @param {Array<LH.Artifacts.NetworkRequest>} records
* @param {string} resourceUrl
* @return {LH.Artifacts.NetworkRequest|undefined}
*/
static findLastDocumentForUrl(records, resourceUrl) {
// equalWithExcludedFragments is expensive, so check that the resourceUrl starts with the request url first
const matchingRequests = records.filter(request =>
request.resourceType === 'Document' &&
resourceUrl.startsWith(request.url) &&
UrlUtils.equalWithExcludedFragments(request.url, resourceUrl)
);
return matchingRequests[matchingRequests.length - 1];
}

/**
* Resolves redirect chain given a main document.
* See: {@link NetworkAnalyzer.findResourceForUrl}) for how to retrieve main document.
* See: {@link NetworkAnalyzer.findLastDocumentForUrl}) for how to retrieve main document.
*
* @param {LH.Artifacts.NetworkRequest} request
* @return {LH.Artifacts.NetworkRequest}
Expand Down

0 comments on commit 86b9602

Please sign in to comment.