diff --git a/CHANGELOG.md b/CHANGELOG.md
index 45d553cf..41fe37ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
- BREAKING: removed umd build
- BREAKING: Img can now accept a ref
+- fix: image download is canceled on unmount
+- feat: `imgPromise` can now receive an object as a second argument. An abort controller signal will be passed as `ob.signal`. This can be used to cancel the image download or other work on unmount. Please note `imgPromise()` should not reject when the abort signal is triggered.
# 4.1.0
diff --git a/README.md b/README.md
index 97c477d6..45c44363 100644
--- a/README.md
+++ b/README.md
@@ -65,7 +65,7 @@ export default function MyComponent() {
- `srcList`: a string or array of strings. `useImage` will try loading these one at a time and returns after the first one is successfully loaded
-- `imgPromise`: a promise that accepts a url and returns a promise which resolves if the image is successfully loaded or rejects if the image doesn't load. You can inject an alternative implementation for advanced custom behaviour such as logging errors or dealing with servers that return an image with a 404 header
+- `imgPromise`: a function that accepts a url and an object of other options and returns a promise which resolves if the image is successfully loaded or rejects if the image doesn't load. Can be used to inject an alternative implementation for advanced custom behavior such as logging errors or dealing with servers that return an image with a 404 header. The object will contain an abort signal which can be used to cancel the image download or other work on unmount. Please note `imgPromise()` should not reject when the abort signal is triggered.
- `useSuspense`: boolean. By default, `useImage` will tell React to suspend rendering until an image is downloaded. Suspense can be disabled by setting this to false.
diff --git a/dev/app.tsx b/dev/app.tsx
index bddb5b9d..bfeac5b3 100644
--- a/dev/app.tsx
+++ b/dev/app.tsx
@@ -164,6 +164,48 @@ const ReuseCache = ({renderId}) => {
)
}
+const CancelOnUnmount = () => {
+ const [src] = useState(
+ `/delay/2000/https://picsum.photos/200?rand=${Math.random()}`,
+ )
+ const [networkCalls, setNetworkCalls] = useState(-1)
+ const [shouldShow, setShouldShow] = useState(true)
+
+ useEffect(() => {
+ setTimeout(() => {
+ const entires = performance.getEntriesByName(src)
+ setNetworkCalls(entires.length)
+ setShouldShow(false)
+ }, 500)
+ })
+
+ return (
+
+
Unmounted component should cancel download
+
+ {networkCalls < 0 && ❓ test pending}
+ {networkCalls === 0 && ✅ test passed}
+ {networkCalls > 0 && ❌ test failed.}
+