-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Image][Performance] Fast Image with Auth Token Header Caching #12648
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
Changes from 12 commits
e06e498
2db5ed0
47feb3a
6df9720
c6227ab
b8b9a7a
508ba92
9020cb3
98c2590
3ce4d46
3c90aed
3253c96
760e0c6
c512849
c71095a
e8db548
b0b2e6d
973cbce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import React from 'react'; | ||
| import {Image} from 'react-native'; | ||
| import addEncryptedAuthTokenToURL from '../../libs/addEncryptedAuthTokenToURL'; | ||
|
|
||
| const RESIZE_MODES = { | ||
| contain: 'contain', | ||
| cover: 'cover', | ||
| stretch: 'stretch', | ||
| center: 'center', | ||
| }; | ||
|
|
||
| const FastImage = (props) => { | ||
| // eslint-disable-next-line | ||
| const {source, ...rest} = props; | ||
|
|
||
| // Check for headers - if it has them then we need to instead just add the | ||
| // encryptedAuthToken to the url and RNW's Image component headers do not work (or do they just not cache?) | ||
| if (typeof source === 'number' || props.source.headers == null) { | ||
| // eslint-disable-next-line | ||
| return <Image source={props.source} {...rest} />; | ||
| } | ||
| // eslint-disable-next-line | ||
| return <Image source={{uri: addEncryptedAuthTokenToURL(props.source.uri)}} {...rest} />; | ||
| }; | ||
|
|
||
| FastImage.displayName = 'FastImage'; | ||
| FastImage.propTypes = Image.propTypes; | ||
| FastImage.resizeMode = RESIZE_MODES; | ||
| export default FastImage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import RNFastImage from '@pieter-pot/react-native-fast-image'; | ||
|
|
||
| // eslint-disable-next-line | ||
| const FastImage = (props) => <RNFastImage {...props} />; | ||
|
|
||
| FastImage.displayName = 'FastImage'; | ||
| FastImage.propTypes = RNFastImage.propTypes; | ||
| FastImage.resizeMode = RNFastImage.resizeMode; | ||
| export default FastImage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| import React, {PureComponent} from 'react'; | ||
| import {View, Image} from 'react-native'; | ||
| import {View} from 'react-native'; | ||
| import PropTypes from 'prop-types'; | ||
| import {withOnyx} from 'react-native-onyx'; | ||
| import lodashGet from 'lodash/get'; | ||
| import Log from '../libs/Log'; | ||
| import styles from '../styles/styles'; | ||
| import makeCancellablePromise from '../libs/MakeCancellablePromise'; | ||
| import FullscreenLoadingIndicator from './FullscreenLoadingIndicator'; | ||
| import ONYXKEYS from '../ONYXKEYS'; | ||
| import chatAttachmentTokenHeaders from '../libs/chatAttachmentTokenHeaders'; | ||
| import FastImage from './FastImage'; | ||
|
|
||
| const propTypes = { | ||
| /** Url for image to display */ | ||
|
|
@@ -16,11 +20,25 @@ const propTypes = { | |
|
|
||
| /** Callback fired when the image has been measured. */ | ||
| onMeasure: PropTypes.func, | ||
|
|
||
| /** Does the image require an authToken? */ | ||
| isAuthTokenRequired: PropTypes.bool, | ||
|
|
||
| /* Onyx props */ | ||
| /** Session object */ | ||
| session: PropTypes.shape({ | ||
| /** An error message to display to the user */ | ||
| encryptedAuthToken: PropTypes.string, | ||
| }), | ||
|
Beamanator marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const defaultProps = { | ||
| style: {}, | ||
| onMeasure: () => {}, | ||
| isAuthTokenRequired: false, | ||
| session: { | ||
| encryptedAuthToken: false, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -39,71 +57,33 @@ class ImageWithSizeCalculation extends PureComponent { | |
|
|
||
| this.imageLoadingStart = this.imageLoadingStart.bind(this); | ||
| this.imageLoadingEnd = this.imageLoadingEnd.bind(this); | ||
| this.onError = this.onError.bind(this); | ||
| this.imageLoadedSuccessfuly = this.imageLoadedSuccessfuly.bind(this); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| this.calculateImageSize(); | ||
| onError() { | ||
| Log.hmmm('Unable to fetch image to calculate size', {url: this.props.url}); | ||
| } | ||
|
|
||
| componentDidUpdate(prevProps) { | ||
| if (prevProps.url === this.props.url) { | ||
| return; | ||
| } | ||
|
|
||
| this.calculateImageSize(); | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| if (!this.getImageSizePromise) { | ||
| return; | ||
| } | ||
|
|
||
| this.getImageSizePromise.cancel(); | ||
| imageLoadingStart() { | ||
| this.setState({isLoading: true}); | ||
| } | ||
|
|
||
| /** | ||
| * @param {String} url | ||
| * @returns {Promise} | ||
| */ | ||
| getImageSize(url) { | ||
| return new Promise((resolve, reject) => { | ||
| Image.getSize(url, (width, height) => { | ||
| resolve({width, height}); | ||
| }, (error) => { | ||
| reject(error); | ||
| }); | ||
| }); | ||
| imageLoadingEnd() { | ||
| this.setState({isLoading: false}); | ||
| } | ||
|
|
||
| calculateImageSize() { | ||
| if (!this.props.url) { | ||
| imageLoadedSuccessfuly(event) { | ||
| if (!lodashGet(event, 'nativeEvent.width', false) || !lodashGet(event, 'nativeEvent.height', false)) { | ||
| // Image didn't load properly | ||
| return; | ||
| } | ||
|
|
||
| this.getImageSizePromise = makeCancellablePromise(this.getImageSize(this.props.url)); | ||
| this.getImageSizePromise.promise | ||
| .then(({width, height}) => { | ||
| if (!width || !height) { | ||
| // Image didn't load properly | ||
| return; | ||
| } | ||
|
|
||
| this.props.onMeasure({width, height}); | ||
| }) | ||
| .catch((error) => { | ||
| Log.hmmm('Unable to fetch image to calculate size', {error, url: this.props.url}); | ||
| }); | ||
| } | ||
|
|
||
| imageLoadingStart() { | ||
| this.setState({isLoading: true}); | ||
| } | ||
|
|
||
| imageLoadingEnd() { | ||
| this.setState({isLoading: false}); | ||
| this.props.onMeasure({width: event.nativeEvent.width, height: event.nativeEvent.height}); | ||
| } | ||
|
|
||
| render() { | ||
| const headers = this.props.isAuthTokenRequired ? chatAttachmentTokenHeaders() : undefined; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it might be better to forward |
||
| return ( | ||
| <View | ||
| style={[ | ||
|
|
@@ -112,15 +92,21 @@ class ImageWithSizeCalculation extends PureComponent { | |
| this.props.style, | ||
| ]} | ||
| > | ||
| <Image | ||
| <FastImage | ||
| style={[ | ||
| styles.w100, | ||
| styles.h100, | ||
| ]} | ||
| source={{uri: this.props.url}} | ||
| resizeMode="contain" | ||
| fallback | ||
|
thomas-coldwell marked this conversation as resolved.
Outdated
|
||
| source={{ | ||
| uri: this.props.url, | ||
| headers, | ||
| }} | ||
| resizeMode={FastImage.resizeMode.contain} | ||
| onLoadStart={this.imageLoadingStart} | ||
| onLoadEnd={this.imageLoadingEnd} | ||
| onError={this.onError} | ||
| onLoad={this.imageLoadedSuccessfuly} | ||
| /> | ||
| {this.state.isLoading && ( | ||
| <FullscreenLoadingIndicator | ||
|
|
@@ -134,4 +120,6 @@ class ImageWithSizeCalculation extends PureComponent { | |
|
|
||
| ImageWithSizeCalculation.propTypes = propTypes; | ||
| ImageWithSizeCalculation.defaultProps = defaultProps; | ||
| export default ImageWithSizeCalculation; | ||
| export default withOnyx({ | ||
| session: {key: ONYXKEYS.SESSION}, | ||
| })(ImageWithSizeCalculation); | ||
|
Comment on lines
+122
to
+124
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As pointed out in another comment - this is unused |
||
Uh oh!
There was an error while loading. Please reload this page.