Skip to content

Commit 74690f1

Browse files
paullewisbrendankenny
authored andcommitted
Moves from XHR to DevTools Protocol for manifest retrieval (#600)
* Moves from XHR to DevTools Protocol for manifest
1 parent 3f3e5c2 commit 74690f1

File tree

2 files changed

+19
-57
lines changed

2 files changed

+19
-57
lines changed

lighthouse-core/gather/gatherers/manifest.js

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,6 @@
1919
const Gatherer = require('./gatherer');
2020
const manifestParser = require('../../lib/manifest-parser');
2121

22-
/* global document, XMLHttpRequest, __returnResults */
23-
24-
/* istanbul ignore next */
25-
function getManifestContent() {
26-
function post(response) {
27-
// __returnResults is magically inserted by driver.evaluateAsync
28-
__returnResults(response);
29-
}
30-
31-
const manifestNode = document.querySelector('link[rel=manifest]');
32-
if (!manifestNode) {
33-
return post({error: 'No <link rel="manifest"> found in DOM.'});
34-
}
35-
36-
const manifestURL = manifestNode.href;
37-
if (!manifestURL) {
38-
return post({error: 'No href found on <link rel="manifest">.'});
39-
}
40-
41-
const req = new XMLHttpRequest();
42-
req.open('GET', manifestURL);
43-
req.onload = function() {
44-
if (req.status !== 200) {
45-
return post({
46-
error: `Unable to fetch manifest at \
47-
${manifestURL}: ${req.status} - ${req.statusText}`
48-
});
49-
}
50-
51-
post({manifestContent: req.response});
52-
};
53-
req.send();
54-
}
55-
5622
class Manifest extends Gatherer {
5723

5824
static _errorManifest(errorString) {
@@ -70,23 +36,18 @@ class Manifest extends Gatherer {
7036
* potentially lead to a different asset. Using the original manifest
7137
* resource is tracked in issue #83
7238
*/
73-
return driver.evaluateAsync(`(${getManifestContent.toString()}())`)
74-
75-
.then(returnedValue => {
76-
if (!returnedValue) {
39+
return driver.sendCommand('Page.getAppManifest')
40+
.then(response => {
41+
if (response.errors.length) {
42+
this.artifact = Manifest._errorManifest(response.errors.join(', '));
43+
return;
44+
}
45+
46+
this.artifact = manifestParser(response.data);
47+
}, _ => {
7748
this.artifact = Manifest._errorManifest('Unable to retrieve manifest');
7849
return;
79-
}
80-
81-
if (returnedValue.error) {
82-
this.artifact = Manifest._errorManifest(returnedValue.error);
83-
} else {
84-
this.artifact = manifestParser(returnedValue.manifestContent);
85-
}
86-
}, _ => {
87-
this.artifact = Manifest._errorManifest('Unable to retrieve manifest');
88-
return;
89-
});
50+
});
9051
}
9152
}
9253

lighthouse-core/test/gather/gatherers/manifest.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('Manifest gatherer', () => {
3434
it('returns an artifact', () => {
3535
return manifestGather.afterPass({
3636
driver: {
37-
evaluateAsync() {
38-
return Promise.resolve('');
37+
sendCommand() {
38+
return Promise.resolve({data: '', errors: [], url: 'https://example.com/manifest.json'});
3939
}
4040
}
4141
}).then(_ => {
@@ -46,7 +46,7 @@ describe('Manifest gatherer', () => {
4646
it('handles driver failure', () => {
4747
return manifestGather.afterPass({
4848
driver: {
49-
evaluateAsync() {
49+
sendCommand() {
5050
return Promise.reject('such a fail');
5151
}
5252
}
@@ -61,9 +61,9 @@ describe('Manifest gatherer', () => {
6161
const error = 'There was an error.';
6262
return manifestGather.afterPass({
6363
driver: {
64-
evaluateAsync() {
64+
sendCommand() {
6565
return Promise.resolve({
66-
error
66+
errors: [error]
6767
});
6868
}
6969
}
@@ -73,14 +73,15 @@ describe('Manifest gatherer', () => {
7373
});
7474

7575
it('creates a manifest object for valid manifest content', () => {
76-
const manifestContent = JSON.stringify({
76+
const data = JSON.stringify({
7777
name: 'App'
7878
});
7979
return manifestGather.afterPass({
8080
driver: {
81-
evaluateAsync() {
81+
sendCommand() {
8282
return Promise.resolve({
83-
manifestContent
83+
errors: [],
84+
data
8485
});
8586
}
8687
}

0 commit comments

Comments
 (0)