Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocInfo: replace metaTags with viewport in API #26687

Merged
merged 2 commits into from
Feb 27, 2020

Conversation

mdmower
Copy link
Contributor

@mdmower mdmower commented Feb 9, 2020

  • Add getMeta to AmpDoc
  • Replace metaTags with viewport in DocInfo API and propagate change
  • Update documentLoaded message to viewer to include viewport property,
    but still provide metaTags.viewport for backwards compatibility
  • Update amp-doc-viewer documentation
  • Update unit tests with phony document to include head

@mdmower
Copy link
Contributor Author

mdmower commented Feb 9, 2020

Alternatives to this PR:

  • AmpDoc.getMeta and AmpDoc.get/setMetaByName could move out of AmpDoc and live in DocInfo instead
  • DocInfo.metaTags could be removed from DocInfo entirely


// Retain only the first meta content value for a given name, consistent
// with getMetaByName.
if (metaMap[name] === undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If metaMap[name] !== undefined), should we emit user().warn('AmpDoc', 'Only the first <meta> tag with a given name is retained');?

'amp-consent-blocking'
];

let content = ampdoc.getMetaByName('amp-consent-blocking');

Choose a reason for hiding this comment

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

Nice! I see a lot of other dedupe opportunities when searching for "meta[name=" in the codebase. Would you mind filing a cleanup issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This? #26652

Choose a reason for hiding this comment

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

🥇

Copy link
Contributor

Choose a reason for hiding this comment

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

🥇Thanks! much cleaner

}

// Retain only the first meta content value for a given name, consistent
// with getMetaByName.

Choose a reason for hiding this comment

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

Not sure this is safe to do since there may be code that's relying on the existing "concat" behavior (instead of only retaining the first meta content).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is safe, actually. There are very few uses of DocInfo.metaTags, and the array output is never used (in fact, in one place, the array behavior isn't even considered and I was surprised type checking didn't report an error).

Choose a reason for hiding this comment

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

Thanks for checking.

@dreamofabear
Copy link

There are very few uses of DocInfo.metaTags

Appears so. The only other one I saw was in link-rewriter-manager.js which probably should use AmpDoc.getMetaByName() anyways.

I'd be cool with this alternative:

DocInfo.metaTags could be removed from DocInfo entirely

@dreamofabear
Copy link

dreamofabear commented Feb 11, 2020

Actually looks like we send metaTags to the viewer in the "documentLoaded" message:

this.viewer_.sendMessage(
'documentLoaded',
dict({
'title': doc.title,
'sourceUrl': getSourceUrl(this.ampdoc.getUrl()),
'serverLayout': doc.documentElement.hasAttribute('i-amphtml-element'),
'linkRels': documentInfo.linkRels,
'metaTags': documentInfo.metaTags,
}),
/* cancelUnsent */ true
);

Added in #14137. Let me follow up internally to see if this is actually used.

@dreamofabear
Copy link

It's only used for meta[name=viewport], so it's safe to remove if we send only the content for "viewport" in the "documentLoaded" viewer message in resources-impl.js.

We should also update amp-doc-viewer-api.md to reflect this change.

@mdmower mdmower requested a review from dreamofabear February 12, 2020 07:00
@mdmower mdmower changed the title DocInfo: Use AmpDoc getMeta for meta name/content DocInfo: replace metaTags with viewport in API Feb 12, 2020
@mdmower
Copy link
Contributor Author

mdmower commented Feb 20, 2020

It's only used for meta[name=viewport], so it's safe to remove if we send only the content for "viewport" in the "documentLoaded" viewer message in resources-impl.js.

We should also update amp-doc-viewer-api.md to reflect this change.

Let me know if the latest version of this PR matches your description, or if you had something else in mind. Thanks!

@dreamofabear
Copy link

Oh sorry for the delay, will take a look soon! 👍

@@ -602,7 +602,7 @@ export class ResourcesImpl {
'sourceUrl': getSourceUrl(this.ampdoc.getUrl()),
'serverLayout': doc.documentElement.hasAttribute('i-amphtml-element'),
'linkRels': documentInfo.linkRels,
'metaTags': documentInfo.metaTags,
'viewport': documentInfo.viewport,

Choose a reason for hiding this comment

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

Ah, sorry I meant we still need metaTags in the message contents, but it's OK if it only contains meta[name=viewport].

I.e. I think this should be:

'metaTags': {'viewport': documentInfo.viewport},
'viewport': documentInfo.viewport,

Feel free to drop a TODO (or new issue) to rename this once the callers have been migrated to use viewport instead of metaTags.

Copy link
Contributor Author

@mdmower mdmower Feb 21, 2020

Choose a reason for hiding this comment

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

I'm happy to do this, but I'm curious where this request to retain DocumentInfo.metaTags['viewport'] is coming from? After this PR, if you grep for metaTags in this repository, you won't find a single hit. So apparently there's a consumer of this API outside of AMPHTML?

Ohhh, oops. I see now. It's not that DocumentInfo.metaTags needs to be retained, it's that the documentLoaded message sent in src/service/resources-impl.js needs to avoid an API breaking change (between the document and the viewer). Got it, sorry, should have looked closer.

@mdmower
Copy link
Contributor Author

mdmower commented Feb 21, 2020

FYI, it looks like the bundle size test on Travis is not going to complete unless I rebase, but all code comments will be wiped out if I force push. I'll wait for a "go ahead" before rebasing. Until then, code comments threads can continue.

- Add getMeta to AmpDoc
- Replace metaTags with viewport in DocInfo API and propagate change
- Update 'documentLoaded' message to viewer to include viewport
  property, but still provide metaTags.viewport for backwards
  compatibility
- Update amp-doc-viewer documentation
- Update unit tests with phony document to include head
@mdmower
Copy link
Contributor Author

mdmower commented Feb 23, 2020

This is ready for review by an OWNER of extensions/amp-viewer-integration/amp-doc-viewer-api.md.

@dreamofabear
Copy link

@jridgewell Can you approve amp-doc-viewer-api.md for owners?

src/consent.js Outdated
.toUpperCase()
.replace(/\s/g, '')
.split(',');
content = content.toUpperCase().replace(/\s/g, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: for faster execution

Suggested change
content = content.toUpperCase().replace(/\s/g, '');
content = content.toUpperCase().replace(/\s+/g, '');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied.

* @return {!Object<string, string>}
*/
getMeta() {
const metaMap = map();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we cache this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commit 555d668 takes a stab at this. Let me know if you agree with the implementation.

/** @override */
getMeta() {
const copy = map();
for (const k in this.meta_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Object.assign?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, adopted your suggestion by way of map(opt_initial)

}
return metaTags;
function getViewport(doc) {
const viewportEl = doc.head.querySelector('meta[name="viewport"]');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this being specialized to viewport metas?

Choose a reason for hiding this comment

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

It's the only meta that's currently being used.

- Cache name/content pairs
- Minor optimizations
@mdmower mdmower merged commit 88e75b8 into ampproject:master Feb 27, 2020
@mdmower mdmower deleted the pr-docinfo branch February 27, 2020 02:56
mdmower added a commit to mdmower/amphtml that referenced this pull request Feb 27, 2020
Cached meta tags feature requires stubbing AmpDoc.getMeta to ensure old
meta tags are reused in future tests.
robinvanopstal added a commit to jungvonmatt/amphtml that referenced this pull request Feb 27, 2020
* master: (54 commits)
  inabox-resources: Minor test improvement (ampproject#26916)
  DocInfo: replace metaTags with viewport in API (ampproject#26687)
  🐛 SwG now uses AMP sendBeacon interface (ampproject#26970)
  🏗 Allow array destructuring on preact hooks (ampproject#26901)
  Gulp Dep Check: fail on unused entries (ampproject#26981)
  Update no-import lint rule to forbid sub-paths (ampproject#26531)
  🐛 amp-ad type blade - fix bladeOnLoad callback (ampproject#26627)
  📖 Clarify when max-age is required (ampproject#26956)
  ♻️ Consolidate players as .i-amphtml-media-component (ampproject#26967)
  Add Preact Enzyme tests (ampproject#26529)
  Fixes `update_tests` flag on `gulp validator` (ampproject#26965)
  📦 Update dependency google-closure-library to v20200224 (ampproject#26986)
  🏗 Transform aliased configured components (ampproject#26541)
  ✨ InaboxResources: Observe intersections for some elements' viewportCallbacks (ampproject#26942)
  variable substitutions: Support allowlist lookup in AmpDocShadow (ampproject#26731)
  cl/297197875 Revision bump for ampproject#26877 (ampproject#26982)
  Json fix (ampproject#26971)
  📦 Update dependency mocha to v7.1.0 (ampproject#26976)
  Add documentation for amp-access-scroll (ampproject#26782)
  make controls always shown in amp for email (ampproject#25714)
  ...
rcebulko pushed a commit that referenced this pull request Feb 27, 2020
Cached meta tags feature requires stubbing AmpDoc.getMeta to ensure old
meta tags are reused in future tests.
robinvanopstal added a commit to jungvonmatt/amphtml that referenced this pull request Mar 2, 2020
* master:
  Launch `amp-next-page` v2 & clean up experiment (ampproject#27035)
  ✨Implement Display Locking on amp-accordion (ampproject#25867)
  📖<amp-next-page> documentation and validation (ampproject#26841)
  Improve visibility event tests (ampproject#26847)
  amp-geo: Fall back to API for country (ampproject#26407)
  ✨ Add customization options to <amp-story-quiz> (ampproject#26714)
  navigation: Minor test improvements (ampproject#26926)
  ♻️ Alias video.fullscreen action for symmetry with interface names (ampproject#27017)
  ✨ Implements `amp-analytics` support for `amp-next-page` (ampproject#26451)
  ✨ amp-video-iframe integration: global jwplayer() singleton by default (ampproject#26969)
  Fix unit tests broken by ampproject#26687 (ampproject#27000)
  Filter redirect info from emails (ampproject#27012)
  📖 Make amp-bind doc valid, fix amp-form stories filter (ampproject#27003)
  url-replacements: Minor test improvement (ampproject#26930)
  viewport: Minor test improvement (ampproject#26931)
  amp-consent fullscreen warning (ampproject#26467)
  dep-check: USE CAPS FOR IMPORTANCE (ampproject#26993)
  fix img url (ampproject#26987)

# Conflicts:
#	extensions/amp-next-page/amp-next-page.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants