James Simpson
James Simpson

Reputation: 13678

Load local images from external site in iframe within Electron app

Let's say I have a website that I want to turn into a desktop application using either an iframe or webview within an Electron app. Let's also assume that this website has a lot of images on it that I would like to cache in the Electron app so that they only have to download those images once.

Is there a way to access those local files (that are bundled in the Electron app) from the remote site that has been loaded in the iframe/webview?

Upvotes: 1

Views: 1654

Answers (2)

James Simpson
James Simpson

Reputation: 13678

Unfortunately, there wasn't a very straightforward way to make this work. However, we managed to get a slightly hacky system setup that seems to do the job:

  1. Convert all assets to base64 data URI's and save in a JSON file with the asset URL as the key.
  2. From inside the iframe/webview, request those assets from the app using postMessage or ipc.
  3. Receive that request and send the JSON object back to the webview/iframe.
  4. When loading the assets, check if the URL is in the object and use the base64 data URI; otherwise, load as normal.

Upvotes: 2

Revln9
Revln9

Reputation: 847

by default electron cache most of the http requests , check this to understand how it works https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentsloadurlurl-options

Something interesting https://medium.com/@philipp.schaechtele/asset-caching-with-service-worker-c40dcda43842

Hope it helps

Upvotes: 0

Related Questions