question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using storage.get with download:true option for a JSON file in React Native seems to cache result

See original GitHub issue

Describe 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:

  1. Create a JSON file on S3
  2. Use the app to fetch the content (storage.get) and verify content
  3. Use storage.put to update that file content
  4. Verify new content is present in S3 console
  5. 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:closed
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pperroncommented, Nov 19, 2020

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 !

1reaction
weicommented, Nov 18, 2020

With @samicey’s help I can reproduce this issue.

By default, the HTTP Headers returned by S3 looks something like follows:

HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: *
Last-Modified: Wed, 18 Nov 2020 16:53:16 GMT
Access-Control-Allow-Methods: GET, HEAD, PUT, POST, DELETE
Access-Control-Expose-Headers: x-amz-server-side-encryption, x-amz-request-id, x-amz-id-2, ETag
Date: Wed, 18 Nov 2020 17:49:49 GMT
Content-Length: 14
Accept-Ranges: bytes
ETag: "XXXXXXXX"
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2: XXXXXXXXxX
Server: AmazonS3
x-amz-request-id: XXXXXXXXXX

Note there is no Cache-Control or Expires headers. When those headers are missing AND Last-Modified header is present, browsers calculates what’s called Heuristic freshness and sets its own ttl in cache. e.g. Firefox docs

This results in subsequent requests getting cached by default as seen:

image

As Harry mentioned in https://github.com/aws-amplify/amplify-js/issues/7095#issuecomment-728355794 Doing something like

Storage.get(filename, { download: true, cacheControl: 'no-cache' })

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fetch data from local JSON file on react native?
Use this import data from './customData.json';.
Read more >
Load and Render JSON Data into React Components
In this guide, we will work on a code example to load the JSON data from a file and render it inside a...
Read more >
Luracast Restler Callback Parameter For Crossdomain Ajax
awsamplify / amplifyjs Public Using storage.get with download:true option for a JSON file in React Native seems to cache result #6413 Using.
Read more >
Web server
the cache (temporary Internet files) is activated in your web browser. Choose the. "Automatically" option in the cache settings of your web ...
Read more >
Using Images in React Native
A few hours ago, I was working on a piece of React Native code that required dynamic image exports off the file system....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found