Skip to content

Commit

Permalink
misc(treemap): elide origin from url if same as requestedUrl (#12598)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Jun 2, 2021
1 parent dbe7884 commit 98e1d81
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lighthouse-treemap/app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,22 @@ class TreemapViewer {
/** @type {WeakMap<LH.Treemap.Node, LH.Treemap.NodePath>} */
this.nodeToPathMap = new WeakMap();

this.documentUrl = options.lhr.requestedUrl;
this.documentUrl = new URL(options.lhr.requestedUrl);
this.el = el;
this.getHueForD1NodeName = TreemapUtil.stableHasher(TreemapUtil.COLOR_HUES);

// These depth one node uses the network URL for the name, but we want
// to elide common parts of the URL so text fits better in the UI.
for (const node of this.depthOneNodesByGroup.scripts) {
try {
const url = new URL(node.name);
node.name = TreemapUtil.elideSameOrigin(url, this.documentUrl);
if (url.href === this.documentUrl.href) {
node.name += ' (inline)';
}
} catch {}
}

/* eslint-disable no-unused-expressions */
/** @type {LH.Treemap.Node} */
this.currentTreemapRoot;
Expand All @@ -94,8 +106,8 @@ class TreemapViewer {

createHeader() {
const urlEl = TreemapUtil.find('a.lh-header--url');
urlEl.textContent = this.documentUrl;
urlEl.href = this.documentUrl;
urlEl.textContent = this.documentUrl.toString();
urlEl.href = this.documentUrl.toString();

this.createBundleSelector();
}
Expand Down Expand Up @@ -215,7 +227,7 @@ class TreemapViewer {
wrapNodesInNewRootNode(nodes) {
const children = [...nodes];
return {
name: this.documentUrl,
name: this.documentUrl.toString(),
resourceBytes: children.reduce((acc, cur) => cur.resourceBytes + acc, 0),
unusedBytes: children.reduce((acc, cur) => (cur.unusedBytes || 0) + acc, 0),
children,
Expand Down Expand Up @@ -432,11 +444,6 @@ class TreemapViewer {
} else {
name = path.join('/');
}

// Elide the document URL.
if (name.startsWith(this.currentTreemapRoot.name)) {
name = name.replace(this.currentTreemapRoot.name, '//');
}
}

data.push({
Expand Down Expand Up @@ -468,7 +475,7 @@ class TreemapViewer {
const dataRow = cell.getRow().getData();
if (!dataRow.bundleNode) return '';

return `${dataRow.bundleNode.name} (bundle) ${dataRow.name}`;
return `${dataRow.bundleNode.name} ${dataRow.name}`;
};

/** @param {Tabulator.CellComponent} cell */
Expand Down
9 changes: 9 additions & 0 deletions lighthouse-treemap/app/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class TreemapUtil {
return string.slice(0, length - 1) + '…';
}

/**
* @param {URL} url
* @param {URL} fromRelativeUrl
*/
static elideSameOrigin(url, fromRelativeUrl) {
if (url.origin !== fromRelativeUrl.origin) return url.toString();
return url.toString().replace(fromRelativeUrl.origin, '');
}

/**
* @template {string} T
* @param {T} name
Expand Down

0 comments on commit 98e1d81

Please sign in to comment.