forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink-elements.js
184 lines (161 loc) · 5.9 KB
/
link-elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @license Copyright 2018 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';
const LinkHeader = require('http-link-header');
const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js');
const {URL} = require('../../lib/url-shim.js');
const NetworkRecords = require('../../computed/network-records.js');
const NetworkAnalyzer = require('../../lib/dependency-graph/simulator/network-analyzer.js');
const pageFunctions = require('../../lib/page-functions.js');
const DevtoolsLog = require('./devtools-log.js');
/* globals HTMLLinkElement getNodeDetails */
/**
* @fileoverview
* This gatherer collects all the effect `link` elements, both in the page and declared in the
* headers of the main resource.
*/
/**
*
* @param {string} url
* @param {string} finalUrl
* @return {string|null}
*/
function normalizeUrlOrNull(url, finalUrl) {
try {
return new URL(url, finalUrl).href;
} catch (_) {
return null;
}
}
/**
* @param {string|undefined} value
* @return {LH.Artifacts.LinkElement['crossOrigin']}
*/
function getCrossoriginFromHeader(value) {
if (value === 'anonymous') return 'anonymous';
if (value === 'use-credentials') return 'use-credentials';
return null;
}
/**
* @return {LH.Artifacts['LinkElements']}
*/
/* c8 ignore start */
function getLinkElementsInDOM() {
/** @type {Array<HTMLOrSVGElement>} */
// @ts-expect-error - getElementsInDocument put into scope via stringification
const browserElements = getElementsInDocument('link'); // eslint-disable-line no-undef
/** @type {LH.Artifacts['LinkElements']} */
const linkElements = [];
for (const link of browserElements) {
// We're only interested in actual LinkElements, not `<link>` tagName elements inside SVGs.
// https://github.com/GoogleChrome/lighthouse/issues/9764
if (!(link instanceof HTMLLinkElement)) continue;
const hrefRaw = link.getAttribute('href') || '';
const source = link.closest('head') ? 'head' : 'body';
linkElements.push({
rel: link.rel,
href: link.href,
hreflang: link.hreflang,
as: link.as,
crossOrigin: link.crossOrigin,
hrefRaw,
source,
// @ts-expect-error - put into scope via stringification
node: getNodeDetails(link),
});
}
return linkElements;
}
/* c8 ignore stop */
class LinkElements extends FRGatherer {
constructor() {
super();
/**
* This needs to be in the constructor.
* https://github.com/GoogleChrome/lighthouse/issues/12134
* @type {LH.Gatherer.GathererMeta<'DevtoolsLog'>}
*/
this.meta = {
supportedModes: ['timespan', 'navigation'],
dependencies: {DevtoolsLog: DevtoolsLog.symbol},
};
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @return {Promise<LH.Artifacts['LinkElements']>}
*/
static getLinkElementsInDOM(context) {
// We'll use evaluateAsync because the `node.getAttribute` method doesn't actually normalize
// the values like access from JavaScript does.
return context.driver.executionContext.evaluate(getLinkElementsInDOM, {
args: [],
useIsolation: true,
deps: [
pageFunctions.getNodeDetailsString,
pageFunctions.getElementsInDocument,
],
});
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @param {LH.Artifacts.NetworkRequest[]} networkRecords
* @return {LH.Artifacts['LinkElements']}
*/
static getLinkElementsInHeaders(context, networkRecords) {
const finalUrl = context.url;
const mainDocument = NetworkAnalyzer.findMainDocument(networkRecords, finalUrl);
/** @type {LH.Artifacts['LinkElements']} */
const linkElements = [];
for (const header of mainDocument.responseHeaders) {
if (header.name.toLowerCase() !== 'link') continue;
for (const link of LinkHeader.parse(header.value).refs) {
linkElements.push({
rel: link.rel || '',
href: normalizeUrlOrNull(link.uri, finalUrl),
hrefRaw: link.uri || '',
hreflang: link.hreflang || '',
as: link.as || '',
crossOrigin: getCrossoriginFromHeader(link.crossorigin),
source: 'headers',
node: null,
});
}
}
return linkElements;
}
/**
* @param {LH.Gatherer.FRTransitionalContext} context
* @param {LH.Artifacts.NetworkRequest[]} networkRecords
* @return {Promise<LH.Artifacts['LinkElements']>}
*/
async _getArtifact(context, networkRecords) {
const fromDOM = await LinkElements.getLinkElementsInDOM(context);
const fromHeaders = LinkElements.getLinkElementsInHeaders(context, networkRecords);
const linkElements = fromDOM.concat(fromHeaders);
for (const link of linkElements) {
// Normalize the rel for easy consumption/filtering
link.rel = link.rel.toLowerCase();
}
return linkElements;
}
/**
* @param {LH.Gatherer.PassContext} context
* @param {LH.Gatherer.LoadData} loadData
* @return {Promise<LH.Artifacts['LinkElements']>}
*/
async afterPass(context, loadData) {
return this._getArtifact({...context, dependencies: {}}, loadData.networkRecords);
}
/**
* @param {LH.Gatherer.FRTransitionalContext<'DevtoolsLog'>} context
* @return {Promise<LH.Artifacts['LinkElements']>}
*/
async getArtifact(context) {
const records = await NetworkRecords.request(context.dependencies.DevtoolsLog, context);
return this._getArtifact(context, records);
}
}
module.exports = LinkElements;