Skip to content

Commit

Permalink
misc(viewer): support legacy 2.x reports in viewer (#5204)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed May 16, 2018
1 parent 34324f8 commit b19a7b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
31 changes: 22 additions & 9 deletions lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* the report.
*/

const VIEWER_ORIGIN = 'https://googlechrome.github.io';

/* globals self URL Blob CustomEvent getFilenamePrefix window */

class ReportUIFeatures {
Expand Down Expand Up @@ -318,7 +320,8 @@ class ReportUIFeatures {
break;
}
case 'open-viewer': {
this.sendJsonReport();
const viewerPath = '/lighthouse/viewer/';
ReportUIFeatures.openTabAndSendJsonReport(this.json, viewerPath);
break;
}
case 'save-gist': {
Expand All @@ -344,30 +347,40 @@ class ReportUIFeatures {
/**
* Opens a new tab to the online viewer and sends the local page's JSON results
* to the online viewer using postMessage.
* @param {!ReportRenderer.ReportJSON} reportJson
* @param {string} viewerPath
* @suppress {reportUnknownTypes}
* @protected
*/
sendJsonReport() {
const VIEWER_ORIGIN = 'https://googlechrome.github.io';
const VIEWER_URL = `${VIEWER_ORIGIN}/lighthouse/viewer/`;

static openTabAndSendJsonReport(reportJson, viewerPath) {
let resolve;
const p = new Promise(res => resolve = res);
// Chrome doesn't allow us to immediately postMessage to a popup right
// after it's created. Normally, we could also listen for the popup window's
// load event, however it is cross-domain and won't fire. Instead, listen
// for a message from the target app saying "I'm open".
const json = this.json;
const json = reportJson;
window.addEventListener('message', function msgHandler(/** @type {!Event} */ e) {
const messageEvent = /** @type {!MessageEvent<{opened: boolean}>} */ (e);
const messageEvent = /** @type {!MessageEvent<{opened: boolean, rendered: boolean}>} */ (e);
if (messageEvent.origin !== VIEWER_ORIGIN) {
return;
}

// Most recent deployment
if (messageEvent.data.opened) {
popup.postMessage({lhresults: json}, VIEWER_ORIGIN);
}
if (messageEvent.data.rendered) {
window.removeEventListener('message', msgHandler);
resolve(popup);
}
});

const popup = /** @type {!Window} */ (window.open(VIEWER_URL, '_blank'));
// The popup's window.name is keyed by version+url+fetchTime, so we reuse/select tabs correctly
const fetchTime = json.fetchTime || json.generatedTime;
const windowName = `${json.lighthouseVersion}-${json.requestedUrl}-${fetchTime}`;
const popup = /** @type {!Window} */ (window.open(`${VIEWER_ORIGIN}${viewerPath}`, windowName));

return p;
}

/**
Expand Down
27 changes: 26 additions & 1 deletion lighthouse-viewer/app/src/lighthouse-report-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class LighthouseReportViewer {
_replaceReportHtml(json) {
this._validateReportJson(json);

if (!json.lighthouseVersion.startsWith('3')) {
this._loadInLegacyViewerVersion(json);
return;
}

const dom = new DOM(document);
const renderer = new ReportRenderer(dom);

Expand Down Expand Up @@ -164,12 +169,28 @@ class LighthouseReportViewer {
} catch (e) {
throw new Error('Could not parse JSON file.');
}

this._reportIsFromGist = false;
this._replaceReportHtml(json);
}).catch(err => logger.error(err.message));
}

/**
* Opens new tab with compatible report viewer version
* @param {!ReportRenderer.ReportJSON} reportJson
* @private
*/
_loadInLegacyViewerVersion(json) {
const warnMsg = `Version mismatch between viewer and JSON.
Opening new tab with compatible viewer.`;
const viewerPath = '/lighthouse/viewer2x/';

logger.log(warnMsg, false);
ViewerUIFeatures.openTabAndSendJsonReport(json, viewerPath).then(_ => {
window.close();
logger.log(`${warnMsg} You can close this tab.`, false);
});
}

/**
* Reads a file and returns its content as a string.
* @param {!File} file
Expand Down Expand Up @@ -296,6 +317,10 @@ class LighthouseReportViewer {
if (e.source === self.opener && e.data.lhresults) {
this._reportIsFromGist = false;
this._replaceReportHtml(e.data.lhresults);

if (self.opener && !self.opener.closed) {
self.opener.postMessage({rendered: true}, '*');
}
if (window.ga) {
window.ga('send', 'event', 'report', 'open in viewer');
}
Expand Down
7 changes: 0 additions & 7 deletions lighthouse-viewer/app/src/viewer-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ class ViewerUIFeatures extends ReportUIFeatures {
return ReportGenerator.generateReportHtml(this.json);
}

/**
* @override
*/
sendJsonReport() {
throw new Error('Cannot send JSON to Viewer from Viewer.');
}

/**
* @override
*/
Expand Down

0 comments on commit b19a7b5

Please sign in to comment.