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

Image: support ImageSource with headers #2441

Closed

Conversation

kidroca
Copy link

@kidroca kidroca commented Nov 25, 2022

Details

Extend ImageLoader functionality to be able to work with image sources containing headers

We preserve the existing strategy that works with image.src for cases where the ImageSource
is just an uri with no headers

When headers are involved we use the fetch API to load the image

Why are there 2 ways to load images?
Because fetch or xhr does not work for

  • loading progressive JPEG images
  • loading crossorigin blobs

Cross origin image requests can still be made with headers (fetch) if the server is configured correctly

Fixed Issues

Test Strategy

  1. Verify existing Image functionality has no regressions

    • build web and examples: npm run dev -w react-native-web and npm run dev -w react-native-web-examples
    • open the examples page and go to Image: http://localhost:3000/image
    • see images are loading
    • take a screenshot and do the same from the master branch. You can switch back and forth and verify the image are loading the same way
  2. Verify Images with headers can be loaded

    • build web and examples: npm run dev -w react-native-web and npm run dev -w react-native-web-examples
    • open the examples page and go to Image: http://localhost:3000/image
    • modify sourceWithHeaders here packages/react-native-web-examples/pages/image/index.js and try to load images from a server that expects a GET request with a header
    • verify the image is loading on the examples page (near the bottom, labeled: "With Headers")

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 25, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e5a4597:

Sandbox Source
react-native-web-examples Configuration

@kidroca kidroca force-pushed the kidroca/feat/image-loader-headers branch 2 times, most recently from b6925b4 to dbabc48 Compare November 25, 2022 13:01
Prepare code to add a custom handler for loading images with headers

Removed the old `image.decode` change as it's covered by the
minimal browser versions supported here
The method is mostly used as cleanup
(e.g. useEffect cleanup, or releasing resources when component unmounts)

Simplified the `useEffect` for loading images a bit
Move the image loading effect here
Changed the original logic slightly for less nesting
Changed to cover cases where passing the same headers object was
starting new loads, as it was treated as a different value due to
referential equality
… reference

When the source object changed by reference, but stays structurally the same
we should do nothing and not trigger the loading effect again
Update types to match RN and actual code - we don't call `onLoadStart`
and `onLoadEnd` with any arguments
Use the same `nativeEvent` structure as in RN for the onLoad event

BREAKING CHANGE
`onLoad` was previously called with `nativeEvent` that was the browser Event object
from the image.onload handler
Since we can't spread or mutate the Event object to add `source` we have to
either add it under a new key or remove it
The browser Event does not expose very useful information, (no target, or size info),
so it seems best to replace `nativeEvent` with the same structure used in `react-native`
Since introducing the change to support headers changes to the original
changes are needed:
- support loading a default source with headers
- handle source object changes
- update uri resolving logic to handle blob URLs create by `URL.createObjectURL`
- move the URI/source resolving logic to the `ImageLoader`
`resolveBlobUri` causes difficulties for the logic that identifies
resolved source changes
It is not necessary to be applied there, but only on the hidden image
Previously loaded images used to be added to ImageUriCache
It seems the logic was accidentally removed here: necolas@f4e8b6b#diff-7cb74a3a32d73857be80350ecd1ea131d256bd5af11d2000e4fc2d03c2230584L361

And now the `ImageUriCache` is only updated by preload/getSize
@kidroca kidroca force-pushed the kidroca/feat/image-loader-headers branch 3 times, most recently from ddbcae4 to e5a4597 Compare November 25, 2022 13:24
@kidroca kidroca closed this Nov 25, 2022
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.

1 participant