-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: resolve redirected script records #13751
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
</head> | ||
<body> | ||
<h1>I have a script that redirects</h1> | ||
<script src="/simple-script.js?redirect=%2Funused-javascript.js%3Fgzip%3D1"></script> | ||
<script> | ||
Array.prototype.findIndex = function() {}; | ||
</script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
/** @type {LH.Config.Json} */ | ||
const config = { | ||
extends: 'lighthouse:default', | ||
settings: { | ||
onlyAudits: [ | ||
'legacy-javascript', | ||
'unused-javascript', | ||
], | ||
}, | ||
}; | ||
|
||
/** | ||
* @type {Smokehouse.ExpectedRunnerResult} | ||
*/ | ||
const expectations = { | ||
lhr: { | ||
runWarnings: [ | ||
/The page may not be loading as expected/, | ||
], | ||
requestedUrl: 'http://localhost:10200/online-only.html?redirect=/redirects-scripts.html', | ||
finalUrl: 'http://localhost:10200/redirects-scripts.html', | ||
audits: { | ||
'unused-javascript': { | ||
details: { | ||
items: [ | ||
{ | ||
// A sourced script that redirects will use the value of the `src` attribute as it's script URL. | ||
// This check ensures that we resolve the redirect and use the final redirect network request to compute savings. | ||
// We can distinguish using totalBytes because the final request is compressed while the redirect request is not. | ||
url: 'http://localhost:10200/simple-script.js?redirect=%2Funused-javascript.js%3Fgzip%3D1', | ||
totalBytes: '285000 +/- 2000', | ||
}, | ||
], | ||
}, | ||
}, | ||
'legacy-javascript': { | ||
details: { | ||
items: [ | ||
{ | ||
// An inline script that in a document that redirects will take the destination URL as it's script URL. | ||
url: 'http://localhost:10200/redirects-scripts.html', | ||
subItems: { | ||
items: [ | ||
{signal: 'Array.prototype.findIndex'}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default { | ||
id: 'redirects-scripts', | ||
expectations, | ||
config, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ const ByteEfficiencyAudit = require('./byte-efficiency-audit.js'); | |
const JsBundles = require('../../computed/js-bundles.js'); | ||
const i18n = require('../../lib/i18n/i18n.js'); | ||
const thirdPartyWeb = require('../../lib/third-party-web.js'); | ||
const NetworkAnalyzer = require('../../lib/dependency-graph/simulator/network-analyzer.js'); | ||
const {getRequestForScript} = require('../../lib/script-helpers.js'); | ||
|
||
const UIStrings = { | ||
/** Title of a Lighthouse audit that tells the user about legacy polyfills and transforms used on the page. This is displayed in a list of audit titles that Lighthouse generates. */ | ||
|
@@ -372,11 +372,8 @@ class LegacyJavascript extends ByteEfficiencyAudit { | |
let transferRatio = transferRatioByUrl.get(url); | ||
if (transferRatio !== undefined) return transferRatio; | ||
|
||
const mainDocumentRecord = NetworkAnalyzer.findOptionalMainDocument(networkRecords); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using I think it makes more sense to just get the main document record using the script URL like any other script in this case. For timespan/snapshot, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup! and this would very much be a bug for inline scripts in iframes. not sure why we (Read: I) did it this way 🤔 |
||
const script = artifacts.Scripts.find(script => script.url === url); | ||
const networkRecord = url === artifacts.URL.finalUrl ? | ||
mainDocumentRecord : | ||
networkRecords.find(n => n.url === script?.url); | ||
const networkRecord = getRequestForScript(networkRecords, script); | ||
|
||
if (!script || script.content === null) { | ||
// Can't find content, so just use 1. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @license Copyright 2022 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @param {LH.Artifacts.NetworkRequest[]} networkRecords | ||
* @param {LH.Artifacts.Script|undefined} script | ||
* @return {LH.Artifacts.NetworkRequest|undefined} | ||
*/ | ||
function getRequestForScript(networkRecords, script) { | ||
if (!script) return; | ||
let networkRequest = networkRecords.find(request => request.url === script.url); | ||
while (networkRequest?.redirectDestination) { | ||
networkRequest = networkRequest.redirectDestination; | ||
} | ||
return networkRequest; | ||
} | ||
|
||
module.exports = {getRequestForScript}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could
const url
be removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still used in multiple places, I think we can leave it.