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

Generate optimized Image URLs using configuration from GraphQL #1267

Merged
merged 8 commits into from
Jun 5, 2019

Conversation

tjwiebell
Copy link
Contributor

@tjwiebell tjwiebell commented May 23, 2019

Description

Made changes necessary to source media URL from GraphQL, and made it so it was backwards compatible with the image optimization middleware that would not have access to this configuration value. This should mean backing instances that aren't using /pub as their document root, not require zero additional steps to get Venia up and running. This additionally supports media URLs that are completely external of the Magento backing system, which can be more common with larger instances (eg. an Amazon S3 bucket, or a CDN).

Related Issue

Closes #567

Verification Steps

onboard image opt w/ /pub as document root
  1. Use default Magento backend and spin up Venia
  2. With DevTools open, navigate to home page
  3. Inspect image requests and verify that they are served and have X-Image-Compressed-By: PWA Studio Staging Server header and that they are optimized
  4. Navigate around rest of application and verify no broken images or broken links
onboard image opt w/ / as document root
  1. Spin up local Magento backend using / and document root
  2. Run all the same steps as above (should observe /pub in all image requests now)
backend image opt w/ /pub as document root
  1. Use Fastly enabled backend (https://master-7rqtwti-xjmpcdjpe6kzm.us-4.magentosite.cloud/), IMAGE_OPTIMIZING_ORIGIN=backend, and spin up Venia
  2. With DevTools open, navigate to home page
  3. Inspect image requests and verify that they don't have X-Image-Compressed-By: PWA Studio Staging Server header and that they are optimized by Fastly instead (should be webp, some Fastly headers)
  4. Navigate around rest of application and verify no broken images or broken links

How Have YOU Tested this?

Ran manual tests above and jest

Screenshots / Screen Captures (if appropriate):

Proposed Labels for Change Type/Package

  • major (e.g x.0.0 - a breaking change)
  • minor (e.g 0.x.0 - a backwards compatible addition)
  • patch (e.g 0.0.x - a bug fix)

I'm considering this a patch because it is backwards compatible, and developers previously using variables to fix bug will notice no change.

Checklist:

  • I have updated the documentation accordingly, if necessary.
  • I have added tests to cover my changes, if necessary.

- Update tests to reflect functionality change with absolute URLs
@vercel
Copy link

vercel bot commented May 23, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

Latest deployment for this branch: https://venia-git-feature-graphql-media-urls.magento-research1.now.sh


if (type) {
if (mediaBases.has(type)) {
// If URL is relative and has a supported type, prepend media base onto path
if (!isAbsolute && mediaBases.has(type)) {
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 made the decision to remove functionality that tries to inject media bases into absolute URLs. Logically, I couldn't think of a use case where we receive an absolute URL that doesn't already have the media base in it. If I'm missing something, let me know, and I will re-implement.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately I don't know enough to be able to answer this confidently.

Ping @zetlen @bbatsche for a sanity check here.

@@ -30,18 +30,12 @@ test('prepends media path for product images', () => {
expect(makeUrl(relativePath, { type: 'image-product' })).toBe(
`${productBase}${relativePath}?${defaultParams}`
);
expect(makeUrl(absoluteUrls[2], { type: 'image-product' })).toBe(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test changes all related to removed functionality. See https://github.com/magento-research/pwa-studio/pull/1267/files#r287155267

@tjwiebell tjwiebell added version: Minor This changeset includes functionality added in a backwards compatible manner. version: Patch This changeset includes backwards compatible bug fixes. and removed version: Minor This changeset includes functionality added in a backwards compatible manner. labels May 24, 2019
@vercel vercel bot requested a deployment to staging May 30, 2019 15:12 Abandoned
@supernova-at supernova-at self-assigned this May 30, 2019
Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

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

Can drop some conditionals 👍

packages/venia-concept/src/util/__tests__/makeUrl.spec.js Outdated Show resolved Hide resolved
packages/venia-concept/src/util/makeUrl.js Show resolved Hide resolved

if (type) {
if (mediaBases.has(type)) {
// If URL is relative and has a supported type, prepend media base onto path
if (!isAbsolute && mediaBases.has(type)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately I don't know enough to be able to answer this confidently.

Ping @zetlen @bbatsche for a sanity check here.

packages/venia-concept/src/util/makeUrl.js Outdated Show resolved Hide resolved
# but if your Magento instance does something different, they're configurable
# here.
# MAGENTO_BACKEND_MEDIA_PATH_PRODUCT="/media/catalog/product"
# MAGENTO_BACKEND_MEDIA_PATH_CATEGORY="/media/catalog/category"
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we dropping support for these entirely? If so I think that means this has to be a major version.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes and yes!

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 would argue this isn't a major, and is fully backwards compatible for developers that were previously using them. To resolve the docroot bug, you would prefix these values with /pub, and update the proxy pattern in venia-upward.yml to '^/(graphql|rest|pub/media)(/|$)'.

An upgrade makes them obsolete, but nothing breaks. I could see it going both ways, if upward config or environment variables are considered API (ie. you used them in your custom app code).

Copy link
Contributor

Choose a reason for hiding this comment

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

If nothing breaks I'm okay with that then. I think the next version is already going to be a major so it may be moot anyway.

packages/venia-concept/src/util/makeUrl.js Show resolved Hide resolved
packages/venia-concept/src/util/makeUrl.js Outdated Show resolved Hide resolved
packages/venia-concept/src/util/makeUrl.js Outdated Show resolved Hide resolved
@supernova-at
Copy link
Contributor

Code looks good, running verification steps now

@supernova-at
Copy link
Contributor

@tjwiebell and I are going to pair on the verification steps

@dpatil-magento dpatil-magento merged commit 03b5d45 into develop Jun 5, 2019
@supernova-at supernova-at deleted the feature/graphql-media-urls branch July 25, 2019 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
version: Patch This changeset includes backwards compatible bug fixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Venia does not use store configuration base URLs
4 participants