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(common): add built-in Imgix loader #46171

Merged
merged 1 commit into from
Jun 2, 2022

Conversation

kara
Copy link
Contributor

@kara kara commented May 27, 2022

This commit adds a built-in Imgix loader for the
NgOptimizedImage directive. If you provide the
desired Imgix hostname, an ImageLoader will be
generated with the correct options.

Usage looks like this:

providers: [
  provideImgixLoader('https://some.imgix.net')
]

It sets the "auto=format" flag by default, which
ensures that the smallest image format supported
by the browser is served.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

Does this PR introduce a breaking change?

  • Yes
  • No

@kara kara marked this pull request as ready for review May 27, 2022 21:19
@pullapprove pullapprove bot requested a review from AndrewKushnir May 27, 2022 21:19
@kara kara requested review from pkozlowski-opensource and AndrewKushnir and removed request for AndrewKushnir May 27, 2022 21:19
@AndrewKushnir AndrewKushnir added area: common Issues related to APIs in the @angular/common package PullApprove: disable target: feature This PR is targeted for a feature branch (outside of main and semver branches) labels May 30, 2022
@ngbot ngbot bot modified the milestone: Backlog May 30, 2022
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

@kara thanks for the PR 👍 I've left a few comments.

I really like the idea of splitting the code of the directive into multiple files (as it grew quite a lot) and I'm thinking it'd be great to package everything under the same folder:

  • packages/common/src/directives/ng_optimized_image/index.ts -- would contain public API exports
  • packages/common/src/directives/ng_optimized_image/ng_optimized_image.ts -- directive code itself
  • packages/common/src/directives/ng_optimized_image/image_loaders/... -- folder for all image loader related code

With this structure, in followup PRs, we can also extract:

  • asserts + assert-related helpers
  • the LCPImageObserver service

WDYT?

function assertValidPath(path: unknown) {
const isString = typeof path === 'string';

if (!isString || path.trim() === '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider adding an assert to check whether https is used as a protocol (so that the final URL works for both http and https without security warnings)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the fence with this one. Might be nice but also may be out of scope for directive's concerns. WDYT @pkozlowski-opensource @khempenius?

packages/common/src/image_loaders/imgix_loader.ts Outdated Show resolved Hide resolved
packages/common/src/image_loaders/imgix_loader.ts Outdated Show resolved Hide resolved
packages/common/test/directives/ng_optimized_image_spec.ts Outdated Show resolved Hide resolved
packages/common/test/image_loaders/image_loader_spec.ts Outdated Show resolved Hide resolved
@depyronick
Copy link

should Angular really add a paid & third party service to its core?

Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a comment

Choose a reason for hiding this comment

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

LGTM, there are minor cleanup comments from @AndrewKushnir to take care of before merging.

@kara kara force-pushed the imgix branch 4 times, most recently from 7f02e5a to fd125bd Compare June 2, 2022 22:46
This commit adds a built-in Imgix loader for the
NgOptimizedImage directive. If you provide the
desired Imgix hostname, an ImageLoader will be
generated with the correct options.

Usage looks like this:

```ts
providers: [
  provideImgixLoader('https://some.imgix.net')
]
```

It sets the "auto=format" flag by default, which
ensures that the smallest image format supported
by the browser is served.

This change also moves the IMAGE_LOADER, ImageLoader,
and ImageLoaderConfig into a new directory that will
be shared by all built-in image loaders.
@kara
Copy link
Contributor Author

kara commented Jun 2, 2022

should Angular really add a paid & third party service to its core?

@depyronick To be clear, we're not adding any third party service in this PR. Adding a built-in loader just means that Angular knows how to format image URLs for that service (e.g. width flag is "w" vs "width"), and if you're not using it, it will be tree-shaken away. The idea is that we want to provide good out-of-the-box DX for frequently used image CDNs to make things easier for Angular developers. This is the first PR of several, so we're not singling out Imgix by any means.

Also worth noting you don't even need to use a CDN with the image directive if you don't want to. The ImageLoader API allows you to construct a URL for a CDN that's not built-in or for your own self-hosted images.

@AndrewKushnir AndrewKushnir merged commit 7b9569d into angular:image-directive Jun 2, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: common Issues related to APIs in the @angular/common package PullApprove: disable target: feature This PR is targeted for a feature branch (outside of main and semver branches)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants