Skip to content

Send link relations in documentLoaded message#7142

Merged
muxin merged 7 commits intoampproject:masterfrom
muxin:link-rels
Jan 26, 2017
Merged

Send link relations in documentLoaded message#7142
muxin merged 7 commits intoampproject:masterfrom
muxin:link-rels

Conversation

@muxin
Copy link
Copy Markdown
Contributor

@muxin muxin commented Jan 21, 2017

Fixes #7086 and #7058

}
const rel = n.getAttribute('rel');
const href = n.getAttribute('href');
if (!rel || !href || rel == 'prefetch' || rel == 'preload' ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also dns-prefetch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

continue;
}
const rel = n.getAttribute('rel');
const href = n.getAttribute('href');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you read .href instead of using getAttribute you are getting a full resolved URL and can skip the expensive parseUrl below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
const pageViewId = getPageViewId(ampdoc.win);

const doc = ampdoc.win.document;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor this into a function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like getGlobalDoc ? Why do we need factoring?

rel == 'preconnect') {
continue;
}
linkRels[rel] = parseUrl(href).href;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to upgrade to an array, if there are more than 1 value for the same rel

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will make linkRels type of !Object<string, !Array<string>>

}
const pageViewId = getPageViewId(ampdoc.win);

const doc = ampdoc.win.document;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to factor all the following code into a function like getLinkRels

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK got it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factored


const doc = ampdoc.win.document;
if (doc.head) {
for (let n = doc.head.firstElementChild; n; n = n.nextElementSibling) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A querySelectorAll might be more appropriate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -73,13 +78,31 @@ export class DocInfo {
: sourceUrl;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep linkRels['canonical'] to be the value in the link tag

const links = doc.head.querySelectorAll('link');
for (let i = 0; i < links.length; i++) {
const link = links[i];
const rel = link.getAttribute('rel');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to handle the case where the rel has multiple mappings to the same href.

<link rel="sharelink canonical">

split(/\s+/)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@muxin
Copy link
Copy Markdown
Contributor Author

muxin commented Jan 25, 2017

@cramforce PTAL

function getLinkRels(doc) {
const linkRels = map();
if (doc.head) {
const links = doc.head.querySelectorAll('link');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can narrow down further by using link[rel].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (!isArray(linkRels[rel])) {
const firstHref = linkRels[rel];
linkRels[rel] = [];
linkRels[rel].push(firstHref);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can combine the above statements:

linkRels[rel] = [linkRels[rel]];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for simplifying this~ done

}

rels.split(/\s+/).forEach(rel => {
if (rel == 'prefetch' || rel == 'preload' || rel == 'preconnect' ||
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we hoist this to a const array, and do an #indexOf search?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done

import {map} from '../utils/object';
import {isArray} from '../types';

/** @private @const {Array<string>} */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{!Array<string>}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (rel == 'prefetch' || rel == 'preload' || rel == 'preconnect' ||
rel == 'dns-prefetch') {
return;
for (let i = 0; i < filteredLinkRels.length; i++) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this for loop, just the body.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

rels.split(/\s+/).forEach(rel => {
for (let i = 0; i < filteredLinkRels.length; i++) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if rel is truthy. This may produce empty strings if there is leading or trailing whitespace in a value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to adopt what @jridgewell suggested which will avoid the truth check

if (filteredLinkRels.indexOf(rel) != -1) {
  return;
}

}
}

if (!linkRels[rel]) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can assign the existing value to a variable and then avoid a few lookups of it further down.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (!isArray(linkRels[rel])) {
linkRels[rel] = [linkRels[rel]];
if (!isArray(value)) {
linkRels[rel] = [value];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to get super nit picky:

let value = linkRels[rel];
if (!isArray) {
  value = linkRels[rel] = [value];
}
value.push(href);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (!linkRels[rel]) {
const value = linkRels[rel];
if (!value) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please flip the conditionals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@muxin muxin merged commit e3ca900 into ampproject:master Jan 26, 2017
@muxin muxin deleted the link-rels branch January 26, 2017 01:42
jridgewell pushed a commit to jridgewell/amphtml that referenced this pull request Jan 31, 2017
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants