Using storage.get with download:true option for a JSON file in React Native seems to cache result
See original GitHub issueDescribe the bug
Using storage.get with download:true option for a JSON file in React Native seems to cache result:
var result = await Storage.get('profile.json', { level: 'private', download: true }); var user_profile = await new Response(result.Body).json();
“user_profile” variable will contain the JSON stored in the S3. If I update that ‘profile.json’ with a:
Storage.put('profile.json', JSON.stringify(Userdata), { level: 'private', contentType: 'application/json' })
and in my app I call back the Storage.get on that file, I will get the old content instead of the new saved one. If I open S3 with the AMAZON console and check the json file content, it shows the new content. So each new call to storage.get will return me old content even if content is updated on S3.
To Reproduce Steps to reproduce the behavior:
- Create a JSON file on S3
- Use the app to fetch the content (storage.get) and verify content
- Use storage.put to update that file content
- Verify new content is present in S3 console
- Use the app to fetch the S3 again (storage.get) and content will show old values instead of new one
Expected behavior The second storage.get call should return content stored in S3 instead of cached value.
What is Configured? Using these libs: “aws-amplify”: “^3.0.17”, “aws-amplify-react-native”: “^4.2.0”, “aws-sdk”: “^2.690.0”, “react-native”: “0.61.5”,
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (1 by maintainers)
Hi, tried last night adding the “cacheControl: ‘no-cache’” entry to:
Storage.get(filename, { download: true, cacheControl: 'no-cache' })
and worked OK, data doesn’t seems to be caches anymore. Thanks @wei for the info 😃
That solve my issue !
With @samicey’s help I can reproduce this issue.
By default, the HTTP Headers returned by S3 looks something like follows:
Note there is no
Cache-Control
orExpires
headers. When those headers are missing ANDLast-Modified
header is present, browsers calculates what’s called Heuristic freshness and sets its own ttl in cache. e.g. Firefox docsThis results in subsequent requests getting cached by default as seen:
As Harry mentioned in https://github.com/aws-amplify/amplify-js/issues/7095#issuecomment-728355794 Doing something like
will get S3 to return the appropriate
Cache-Control
headers which eliminates this issue.Alternatively, as @virtualstyle is doing, signing a presigned url each time will also bypass cache as it requests a new resource url every time.