Skip to content

Commit

Permalink
refactor: WebP feature detection using the load utility (#48)
Browse files Browse the repository at this point in the history
## Summary

<!-- Please summarize your changes. -->

<!-- Please link to any applicable information (forum posts, bug
reports, etc.). -->
Incorporated the `load` utility function for image loading in the WebP
feature detection mechanism.


## Checks

<!-- For completed items, change [ ] to [x]. -->

<!-- If you leave this checklist empty, your PR will very likely be
closed. -->

Please check the following:

- [x] I have written documents and tests, if needed.

---------

Co-authored-by: Jonghyeon Ko <[email protected]>
  • Loading branch information
tooooo1 and manudeli authored Sep 25, 2023
1 parent 67a0cd8 commit 96e6eef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-swans-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fepack/image": patch
---

refactor: WebP feature detection using the load utility
File renamed without changes.
11 changes: 5 additions & 6 deletions packages/image/src/checkWebPSupport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { load } from ".";

/**
* Checks for various WebP features support in the browser.
* @see https://developers.google.com/speed/webp/faq?in_your_own_javascript
Expand All @@ -18,12 +20,9 @@ type Feature = keyof typeof kTestImages;
* @param feature - The WebP feature to check for.
*/
const checkWebPFeatureSupport = (feature: Feature) =>
new Promise<boolean>((resolve) => {
const image = new Image();
image.onload = () => resolve(image.width > 0 && image.height > 0);
image.onerror = () => resolve(false);
image.src = "data:image/webp;base64," + kTestImages[feature];
});
load("data:image/webp;base64," + kTestImages[feature])
.then((image) => image.width > 0 && image.height > 0)
.catch(() => false);

const features = Object.keys(kTestImages).filter((key): key is Feature =>
Object.prototype.hasOwnProperty.call(kTestImages, key),
Expand Down

0 comments on commit 96e6eef

Please sign in to comment.