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

feat: integrate js-core into react-imgix #776

Closed
wants to merge 25 commits into from
Closed

Conversation

ericdeansanchez
Copy link
Contributor

@ericdeansanchez ericdeansanchez commented Apr 1, 2021

The purpose of this PR is to integrate @imgix/js-core into
react-imgix. First, targetWidths has been reimplemented in terms of
the js-core client. Second, buildSrc has been implemented in terms of
ImgixClient.buildSrcSet. Lastly, a temp fix was made for the
Background component. Prior to this PR, the flow of the code/params
caused duplicate param key-value pairs to be written into query strings.

Ideally, we want to avoid getting into this kind of situation
altogether. However, doing that will require a bit more reworking,
refactoring, and (possibly || probably) breaking changes.

Closes #763

@ericdeansanchez ericdeansanchez requested a review from a team as a code owner April 1, 2021 18:38
@commit-lint
Copy link

commit-lint bot commented Apr 1, 2021

Features

  • use @imgix/js-core for buildURL and targetWidths (ce729bf)
  • impl buildSrc in terms of ImgixClient.buildSrcSet (7a018bd)

Styles

Tests

  • buildSrc: js-core uses newline as delimeter, not space (f01b0ed)
  • Background: duplicate param keys are not allowed (4a13e0f)

Bug Fixes

  • temp fix to de-dupe h & w params in Background (576416d)

Code Refactoring

  • use object spread over assign (7ccce55)
  • create helpers, clean up (c443031)
  • use object spread over assign (104a2c5)
  • moving some furniture around (51a0864)
  • remove unused code (8d4cec6)
  • abstract building childprops into function (4bded78)
  • pull buildChildProps into constructUrl.js (9d2018d)
  • remove shouldComponentUpdate HOC (e9336ff)
  • remove shallowUntilTarget (9eb59ee)
  • extend React.PureComopnent to get shallow behavior (395aa01)
  • remove unused code (538b7cc)
  • gather and de-dup with buildChildProps (4ec8e0f)

Performance Improvements

  • use Object.assign over spread (72b1fd8)

Build System

  • increase karma timeouts (05778c8)

Contributors

ericdeansanchez

Commit-Lint commands

You can trigger Commit-Lint actions by commenting on this PR:

  • @Commit-Lint merge patch will merge dependabot PR on "patch" versions (X.X.Y - Y change)
  • @Commit-Lint merge minor will merge dependabot PR on "minor" versions (X.Y.Y - Y change)
  • @Commit-Lint merge major will merge dependabot PR on "major" versions (Y.Y.Y - Y change)
  • @Commit-Lint merge disable will desactivate merge dependabot PR
  • @Commit-Lint review will approve dependabot PR
  • @Commit-Lint stop review will stop approve dependabot PR

src/react-imgix.jsx Outdated Show resolved Hide resolved
Copy link
Contributor

@luqven luqven left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏼

src/constructUrl.js Show resolved Hide resolved
src/react-imgix-bg.jsx Show resolved Hide resolved
src/react-imgix.jsx Outdated Show resolved Hide resolved
src/react-imgix.jsx Outdated Show resolved Hide resolved
src/targetWidths.js Show resolved Hide resolved
Copy link
Contributor

@frederickfogerty frederickfogerty left a comment

Choose a reason for hiding this comment

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

Overall I think this is really great and reduces the complexity of this library. Nice work! 🎉

  • DPR_QUALITY and DPR_QUALITY_VALUES should be able to be removed from constants.js because I think they're no longer used
  • I think the changes to use {...object} rather than Object.assign are nice stylistically, but they were introduced for performance reasons, and we shouldn't change these back unless we're sure that there is no change to performance. There was a 30% increase in performance and it would be a shame to lose these gains. Context: perf: remove object spreading #423
  • The title should be updated to not include a "."

package.json Show resolved Hide resolved
src/HOCs/shouldComponentUpdateHOC.jsx Outdated Show resolved Hide resolved
import extractQueryParams from "./extractQueryParams";
import { config } from "./common";
const PACKAGE_VERSION = require("../package.json").version;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change this to using common/constants.js, must have slipped through the cracks.

Comment on lines +143 to +144
const [scheme, rest] = src.split("://");
const [domain, ...pathComponents] = rest.split("/");
Copy link
Contributor

Choose a reason for hiding this comment

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

An improvement here, if you want, could be to use the built-in URL interface to do this extraction. You know, cos, edge cases.

https://developer.mozilla.org/en-US/docs/Web/API/URL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Won't this add encoding to the path though? Or maybe I don't understand how I'd use the built-in interface?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I think you're right


function compactParamKeys(longOptions, width, height) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think having width and height here as parameters is too coupled. I think it would be better to push this to the consumer with either:

const params = compactParamKeys({...longOptions, w: width, h: height});

or

const params = {...compactParamKeys(longOptions), w: width, h: height};

return /^\d+(\.\d+)?:\d+(\.\d+)?$/.test(aspectRatio);
}

function buildChildProps(obj, src, attributeConfig, width, height, refType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this function relates more to the components where it is called, so at least it should be located in that file.

It seems like a bit of a smell to be passing this around - seems like it could go wrong in the future if this function was to incorrectly assign to obj. I wonder if it's better if it's possible to make this pure, or just to deal with this logic being duplicated in the separate components. It's not super complicated so I'm not sure how much benefit there is to extract it into a common function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think it smells a bit too, but I wanted to try to extract/distill some of the structure of what's going on here. I'll look into refactoring this.

src/constructUrl.js Show resolved Hide resolved
Comment on lines +89 to +93
const allParams = { ...params, ...imgixParams, dpr };
const srcOptions = {
...params,
fit: "crop",
...imgixParams,
...(disableLibraryParam ? {} : { ixlib: `react-${PACKAGE_VERSION}` }),
...allParams,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the fix related to this comment?

Lastly, a temp fix was made for the Background component. Prior to this PR, the flow of the code/params caused duplicate param key-value pairs to be written into query strings.

}

export default targetWidths();
export default ImgixClient.targetWidths();
Copy link
Contributor

Choose a reason for hiding this comment

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

Haha, nice! 👏

Comment on lines +65 to +68
expect(srcset.split(",\n")[0].split(" ")).toHaveLength(2);
const aSrcFromSrcSet = srcset.split(",\n")[0].split(" ")[0];
expect(aSrcFromSrcSet).toContain(src);
const aWidthFromSrcSet = srcset.split(", ")[0].split(" ")[1];
const aWidthFromSrcSet = srcset.split(",\n")[0].split(" ")[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

There's probably little value testing this stuff in react-imgix anymore since it's now implemented and tested by @imgix/js-core

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 agree. I wanted to at least demo "hey, this works kind of like it did before." I wonder what work I could do to distill down the test suite now.

@ericdeansanchez ericdeansanchez changed the title feat: integrate js-core. into react-imgix feat: integrate js-core into react-imgix Apr 9, 2021
@ericdeansanchez
Copy link
Contributor Author

Closing in favor of #780

@ericdeansanchez ericdeansanchez deleted the integration branch April 14, 2021 00:36
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.

<Background> generating parameters twice for width and height
4 participants