-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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][Performance] Fast Image with Auth Token Header Caching #12648
Merged
Beamanator
merged 18 commits into
Expensify:main
from
margelo:@thomas/fast-image-caching
Nov 23, 2022
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e06e498
Pass encryptedAuthToken to image via header
Beamanator 2db5ed0
Stop sending authToken in URL
Beamanator 47feb3a
Clean up lint errors
Beamanator 6df9720
Fix header param passed to image source
Beamanator c6227ab
Merge branch 'main' of github.com:Expensify/App into beaman-passAuthT…
Beamanator b8b9a7a
Update header
Beamanator 508ba92
feat: Add fast-image to thumbnails & avatar
thomas-coldwell 9020cb3
feat: Add fast image to attachments view
thomas-coldwell 98c2590
Update src/components/ImageWithSizeCalculation.js
thomas-coldwell 3ce4d46
feat: Util function for getting chat attachment headers
thomas-coldwell 3c90aed
fix: Use RNW's Image component directly in FastImage wrapper
thomas-coldwell 3253c96
Merge branch 'main' into @thomas/fast-image-caching
thomas-coldwell 760e0c6
fix: Remove XCode 14 generated code
thomas-coldwell c512849
fix: Attachment modal image caching
thomas-coldwell c71095a
fix: Image layout shift
thomas-coldwell e8db548
fix: Remove fallback prop
thomas-coldwell b0b2e6d
fix: Use lifecycles rather than hooks
thomas-coldwell 973cbce
fix: Remove XCode 14 generate line
thomas-coldwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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', | ||
}; | ||
|
||
class FastImage extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
imageSource: undefined, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.configureImageSource(); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (prevProps.source === this.props.source) { | ||
return; | ||
} | ||
this.configureImageSource(); | ||
} | ||
|
||
configureImageSource() { | ||
const source = this.props.source; | ||
let imageSource = source; | ||
if (typeof source !== 'number' && source.headers != null) { | ||
imageSource = {uri: addEncryptedAuthTokenToURL(source.uri)}; | ||
} | ||
this.setState({imageSource}); | ||
if (this.props.onLoad == null) { | ||
return; | ||
} | ||
const uri = typeof imageSource === 'number' | ||
? Image.resolveAssetSource(imageSource).uri | ||
: imageSource.uri; | ||
Image.getSize(uri, (width, height) => { | ||
this.props.onLoad({nativeEvent: {width, height}}); | ||
}); | ||
} | ||
|
||
render() { | ||
// eslint-disable-next-line | ||
const { source, onLoad, ...rest } = this.props; | ||
|
||
// eslint-disable-next-line | ||
return <Image {...rest} source={this.state.imageSource} />; | ||
} | ||
} | ||
|
||
FastImage.propTypes = Image.propTypes; | ||
FastImage.resizeMode = RESIZE_MODES; | ||
export default FastImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason this line is confusing me - how are we NOT adding the encrypted auth token to the URL for native here? I just tested Android and saw that the image request has the encrypted auth token in the correct header, NOT in the URL - but this line makes me think we're putting the
encryptedAuthToken
in the URL ALWAYS - can someone explain why I'm confused? 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is purely for the file download - both web and native get the encrypted auth token added to the URL here if required for the attachment. Attachments could be a PDF or image or some other file so I wanted to make sure that still used the
encryptedAuthToken
param here so all files download correctly.As for the actual image request the source URL (without any auth token query param) and the
this.props.isAuthTokenRequired
gets passed down to theImageView
so that it can correctly add the auth token in the correct way for web (as a query param) and native (as a header token).I hope that clears it up a bit 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Beamanator
We can update
fileDownload
to also work with headers.Not sure whether any backend handling would need to be updated
making
fileDownload
work with headers is not as important as images, because it's used a lot less oftenon the other hand the logic would be cleaner and allow automatic cache usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, we only currently use auth Tokens to get chat attachments, and I'm pretty sure all of that is working - passing the auth token in the header as opposed to the URL. If there's other places we pass
encryptedAuthToken
in a URL, we will probably have to modify the backend for those casesGood call, I thinkkk the benefits are useful enough to try to implement that on the next PR 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @kidroca @Beamanator , just going through your comments now 🙌 Just to confirm have we decided here to just use the
encryptedAuthToken
query param for file download then and set this up to use headers in a subsequent PR?