Send link relations in documentLoaded message#7142
Conversation
src/service/document-info-impl.js
Outdated
| } | ||
| const rel = n.getAttribute('rel'); | ||
| const href = n.getAttribute('href'); | ||
| if (!rel || !href || rel == 'prefetch' || rel == 'preload' || |
src/service/document-info-impl.js
Outdated
| continue; | ||
| } | ||
| const rel = n.getAttribute('rel'); | ||
| const href = n.getAttribute('href'); |
There was a problem hiding this comment.
If you read .href instead of using getAttribute you are getting a full resolved URL and can skip the expensive parseUrl below.
src/service/document-info-impl.js
Outdated
| } | ||
| const pageViewId = getPageViewId(ampdoc.win); | ||
|
|
||
| const doc = ampdoc.win.document; |
There was a problem hiding this comment.
Something like getGlobalDoc ? Why do we need factoring?
src/service/document-info-impl.js
Outdated
| rel == 'preconnect') { | ||
| continue; | ||
| } | ||
| linkRels[rel] = parseUrl(href).href; |
There was a problem hiding this comment.
I think we need to upgrade to an array, if there are more than 1 value for the same rel
There was a problem hiding this comment.
Yeah, I will make linkRels type of !Object<string, !Array<string>>
src/service/document-info-impl.js
Outdated
| } | ||
| const pageViewId = getPageViewId(ampdoc.win); | ||
|
|
||
| const doc = ampdoc.win.document; |
There was a problem hiding this comment.
I meant to factor all the following code into a function like getLinkRels
src/service/document-info-impl.js
Outdated
|
|
||
| const doc = ampdoc.win.document; | ||
| if (doc.head) { | ||
| for (let n = doc.head.firstElementChild; n; n = n.nextElementSibling) { |
There was a problem hiding this comment.
A querySelectorAll might be more appropriate.
| @@ -73,13 +78,31 @@ export class DocInfo { | |||
| : sourceUrl; | |||
| } | |||
There was a problem hiding this comment.
The canonicalUrl in docInfo could be rootNode.AMP.canonicalUrl or parseUrl(rootNode.querySelector('link[rel=canonical]').href).href or sourceUrl. Shall I make the linkRels['canonical'] the same logic?
There was a problem hiding this comment.
I'll keep linkRels['canonical'] to be the value in the link tag
src/service/document-info-impl.js
Outdated
| const links = doc.head.querySelectorAll('link'); | ||
| for (let i = 0; i < links.length; i++) { | ||
| const link = links[i]; | ||
| const rel = link.getAttribute('rel'); |
There was a problem hiding this comment.
need to handle the case where the rel has multiple mappings to the same href.
<link rel="sharelink canonical">
split(/\s+/)
|
@cramforce PTAL |
src/service/document-info-impl.js
Outdated
| function getLinkRels(doc) { | ||
| const linkRels = map(); | ||
| if (doc.head) { | ||
| const links = doc.head.querySelectorAll('link'); |
There was a problem hiding this comment.
You can narrow down further by using link[rel].
src/service/document-info-impl.js
Outdated
| if (!isArray(linkRels[rel])) { | ||
| const firstHref = linkRels[rel]; | ||
| linkRels[rel] = []; | ||
| linkRels[rel].push(firstHref); |
There was a problem hiding this comment.
Nit: we can combine the above statements:
linkRels[rel] = [linkRels[rel]];There was a problem hiding this comment.
thanks for simplifying this~ done
src/service/document-info-impl.js
Outdated
| } | ||
|
|
||
| rels.split(/\s+/).forEach(rel => { | ||
| if (rel == 'prefetch' || rel == 'preload' || rel == 'preconnect' || |
There was a problem hiding this comment.
Should we hoist this to a const array, and do an #indexOf search?
src/service/document-info-impl.js
Outdated
| import {map} from '../utils/object'; | ||
| import {isArray} from '../types'; | ||
|
|
||
| /** @private @const {Array<string>} */ |
src/service/document-info-impl.js
Outdated
| if (rel == 'prefetch' || rel == 'preload' || rel == 'preconnect' || | ||
| rel == 'dns-prefetch') { | ||
| return; | ||
| for (let i = 0; i < filteredLinkRels.length; i++) { |
There was a problem hiding this comment.
No need for this for loop, just the body.
src/service/document-info-impl.js
Outdated
| } | ||
|
|
||
| rels.split(/\s+/).forEach(rel => { | ||
| for (let i = 0; i < filteredLinkRels.length; i++) { |
There was a problem hiding this comment.
Check if rel is truthy. This may produce empty strings if there is leading or trailing whitespace in a value.
There was a problem hiding this comment.
Going to adopt what @jridgewell suggested which will avoid the truth check
if (filteredLinkRels.indexOf(rel) != -1) {
return;
}
src/service/document-info-impl.js
Outdated
| } | ||
| } | ||
|
|
||
| if (!linkRels[rel]) { |
There was a problem hiding this comment.
You can assign the existing value to a variable and then avoid a few lookups of it further down.
src/service/document-info-impl.js
Outdated
| if (!isArray(linkRels[rel])) { | ||
| linkRels[rel] = [linkRels[rel]]; | ||
| if (!isArray(value)) { | ||
| linkRels[rel] = [value]; |
There was a problem hiding this comment.
If we want to get super nit picky:
let value = linkRels[rel];
if (!isArray) {
value = linkRels[rel] = [value];
}
value.push(href);
src/service/document-info-impl.js
Outdated
|
|
||
| if (!linkRels[rel]) { | ||
| const value = linkRels[rel]; | ||
| if (!value) { |
There was a problem hiding this comment.
Nit: Please flip the conditionals.
Fixes #7086 and #7058