2

Commenting the scenario just in case of XY problem:
I need to request some json files from my server, but I want to cache them somehow to avoid requesting again.

I can create a json file using download api but there are two problems with this:

  1. I don't know how to specify a custom folder (I think I could solve this using chrome.downloads.download API with saveAs = true)
  2. I don't know how to read that file (maybe not possible for security reasons?) https://developer.chrome.com/extensions/downloads

I was thinking maybe is possible to write in my chrome extension folder, because we can read files from there, but I couldn't find how to do it, I've found a comment saying is not possible, but is from 2011, maybe there is some way to solve this now?

I could try also saving it with chrome.storage, but my json are really big, 3MB each one and I have more than 100, not sure if chrome.storage can manage that without problems, it says 5MB here: https://developer.chrome.com/apps/storage#property-local but we can add unlimitedStorage permission to avoid that limit, the problem is I'm not sure how safe will be this, and maybe there is a better solution.

5
  • 1
    Nothing's really changed in the past 5 years, the API is mostly frozen. In a Chrome-only extension you can use the old HTML5 FileSystem API and switch to the new FileSystem API when it's ready. As for the quota there's probably navigator.webkitPersistentStorage. You may also want to compress JSONs using LZStringUnsafe or a Zip/LZMA library (maybe even compile one into WASM) and store as a UTF-16 string. Commented Aug 24, 2019 at 20:35
  • If you go the downloads route, to read arbitrary files from the user file system the user will have to enable file access switch on the details page of your extension in chrome://extensions and of course your extension should have file://*/* permission or <all_urls>, so you can use the standard XHR/fetch with a standard file:// URL to your file. Commented Aug 24, 2019 at 20:38
  • @wOxxOm can you elaborate more about reading files from disk with Chrome Extension? I was searching in Google and everybody says it's impossible for security reasons Commented Aug 24, 2019 at 22:01
  • @wOxxOm maybe compressing the json in some other string could do the trick with chrome.storage, I will try with LZStringUnsafe Commented Aug 24, 2019 at 22:04
  • Uhm? Provided you've enabled the switch and added the permission as described above it's a standard XHR or fetch with a URL like 'file:///C:/path/foo.json' Commented Aug 25, 2019 at 5:29

1 Answer 1

2

Since your primary purpose for writing files is to cache a network request and you control the web server, I'd recommend leveraging the browser's HTTP cache rather than trying to create your own caching layer.

To get started, make sure that your JSON files are behing served with a Cache-Control header. This header tells the browser what caching strategy to use for your resources. The below MDN article is a great resource for learning more about cache control directives. Once you know what caching strategy you want to use, you'll need to consult your web server's documentation for details on how configure your web server to use these settings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.