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

🚀 [Story interactive] Rewrite Image URL to Cached URL for Image Quizzes and Polls #35375

Merged
merged 15 commits into from
Aug 2, 2021

Conversation

Brandons42
Copy link
Contributor

Rewrite image URLs of image quizzes and polls on the AMP cache to use the cached URLs

Closes #35109

/cc @mszylkowski
/cc @processprocess

@amp-owners-bot amp-owners-bot bot requested a review from gmajoulet July 23, 2021 15:31
@amp-owners-bot
Copy link

amp-owners-bot bot commented Jul 23, 2021

Hey @gmajoulet! These files were changed:

extensions/amp-story-interactive/0.1/amp-story-interactive-abstract.js
extensions/amp-story-interactive/0.1/amp-story-interactive-img-poll.js
extensions/amp-story-interactive/0.1/amp-story-interactive-img-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-binary-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-img-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-img-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-results.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive.js
extensions/amp-story/1.0/amp-story-open-page-attachment.js
extensions/amp-story/1.0/utils.js

Hey @mszylkowski! These files were changed:

extensions/amp-story-interactive/0.1/amp-story-interactive-abstract.js
extensions/amp-story-interactive/0.1/amp-story-interactive-img-poll.js
extensions/amp-story-interactive/0.1/amp-story-interactive-img-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-binary-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-img-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-img-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-poll.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-quiz.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive-results.js
extensions/amp-story-interactive/0.1/test/test-amp-story-interactive.js

Hey @newmuis! These files were changed:

extensions/amp-story/1.0/amp-story-open-page-attachment.js
extensions/amp-story/1.0/utils.js

@mszylkowski mszylkowski self-requested a review July 23, 2021 17:20
Copy link
Contributor

@mszylkowski mszylkowski left a comment

Choose a reason for hiding this comment

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

Let's not use a specific function that sets up the images, I think it's cleaner to keep the image setup inside the attachContent function as before. Also, we can move the call to maybeMakeProxyUrl to the parseOptions for the image attr so any images have this fix. Then we can get @gmajoulet review on this since he has some more context

url,
urlService.getSourceOrigin(loc.href)
);
return loc.origin + '/i/s/' + resolvedRelativeUrl.replace(/https?:\/\//, '');
Copy link
Contributor

Choose a reason for hiding this comment

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

These URLs support a bunch of useful parameters that we should add to the URL, like max width etc. Please check the "URL path" documentation here: https://amp.dev/documentation/guides-and-tutorials/learn/amp-caches-and-cors/amp-cache-urls/

I think specifying a width here would be useful and save some bytes

Copy link
Contributor

Choose a reason for hiding this comment

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

Dynamic resize is expensive, and sacrifices AVIF encoding (because that's too expensive to do on the fly). Using a regular image URL will likely be faster due to better caching, faster response times, and better encoding formats. Using an oversized image is a publisher fault.

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely using oversized images are not great for components that show the images in small regions. When passing these parameters do the images get resized every time, or would they only be resized on the first request to that resource at that resolution? I think for these types of images we'd only want to ask for the small image if we can, and not the original sized asset

Copy link
Contributor

Choose a reason for hiding this comment

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

When passing these parameters do the images get resized every time, or would they only be resized on the first request to that resource at that resolution?

They're resized every time. The persistence layer never stores resizes, only originals. The local-cache may keep an resized item, but it's an extremely small LRU competing with literal billions of other responses.

I think for these types of images we'd only want to ask for the small image if we can, and not the original sized asset

What size is the original asset? Is the publisher aware this image is being shown in a small area?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for chiming in Justin, let's keep /i/s and no resizing then.

Comment on lines 333 to 335
export const maybeMakeProxyUrl = (url, pageEl) => {
const urlService = Services.urlForDoc(pageEl);
const loc = pageEl.getAmpDoc().win.location;
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that it's moved to utils.js I think we should make it easier to re-use, and accept url, ampdoc as parameters.
urlForDoc(ampdoc) should work, and both classes using this helper should be able to call this.getAmpDoc().

Copy link
Contributor

@mszylkowski mszylkowski left a comment

Choose a reason for hiding this comment

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

There's a couple of ways to get the AmpDoc of an element, you cna check out Services.ampdoc(this.element) for instance. The maybeMakeProxyUrl then should get passed in the AmpDoc itself and not the element

extensions/amp-story/1.0/utils.js Outdated Show resolved Hide resolved
extensions/amp-story/1.0/utils.js Outdated Show resolved Hide resolved
@@ -44,6 +45,7 @@ describes.realWin(
const ampStoryPollEl = win.document.createElement(
'amp-story-interactive-binary-poll'
);
ampStoryPollEl.getAmpDoc = () => new AmpDocSingle(win);
Copy link
Contributor

Choose a reason for hiding this comment

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

If we use this.getAmpDoc(), we might not need to mock this (or at least mock it directly on the AmpStoryInteractiveBinaryPoll instead of it's element)

extensions/amp-story/1.0/utils.js Show resolved Hide resolved
@mszylkowski mszylkowski merged commit a648a87 into ampproject:main Aug 2, 2021
westonruter added a commit to westonruter/amphtml that referenced this pull request Aug 3, 2021
…tok-validation

* 'main' of github.com:ampproject/amphtml: (72 commits)
  build: run amp lint --fix to address import order of jixie (ampproject#35513)
  ✨ [amp-analytics] Add Custom Browser Event Tracker (ampproject#35193)
  babel: teach amp mode transformer about #core/mode (ampproject#35477)
  🚮 Remove experiment `amp-consent-granular-consent` (ampproject#35508)
  ♻️ Enable auto-sorting+grouping within src/ and 3p/ (ampproject#35454)
  🐛  [amp-render] fix root-element stripping from amp-render with amp-bind (ampproject#35449)
  ✅ [Story interactive] Add Example Story for Detailed Results Component (ampproject#35450)
  🐛 Fix error on bento example (ampproject#35490)
  🐛 amp-story-grid-layer: Fix AMP invalidation error in documentation (ampproject#35503)
  🐛 Fix code formatting (ampproject#35499)
  ✅ buildDom: add tests for amp-fit-text and amp-layout (ampproject#35494)
  ♻️ Remove unused imports (ampproject#35435)
  ✨ amp-connatix-player: iframe domain based on a property (ampproject#35179)
  Updated document with use cases of remote config (ampproject#35496)
  AMP.goBack: update documentation (ampproject#29290)
  🏗 Allow the bundle-size job to run even if the builds were skipped (ampproject#35492)
  build-system: improve terser/esbuild integration (ampproject#35466)
  🧪 [Story performance] Disable animations on first page to 50% (ampproject#35476)
  📖 [Amp story] [Page attachments] Amp.dev Docs for New Page Attachment Features ampproject#34883 (ampproject#35338)
  🚀 [Story interactive] Rewrite Image URL to Cached URL for Image Quizzes and Polls (ampproject#35375)
  ...
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.

[Story interactive] Rewrite image URL for image components to AMP cache images
5 participants